我找不到从列表中选择项目的方法,每个项目都由三个文本字段组成

问题描述 投票:1回答:1

我看过数十个示例和教程,并且尝试重复相同的步骤,但是不起作用。

在我的活动中,我有四个onClickListeners可以很好地工作,并且与布局顶部的四个按钮相关。此外,当我尝试在同一布局的底部为ListView的项目设置onItemClickListener时,就会出现问题。这个ListView的每个项目都由三个textViews组成,我已经为它们编写了一个自定义适配器。它们完美显示,但是当我单击它们时,什么也没有发生(永远不会到达onItemClick指令)。

这是布局,问题出在最后的“ ingredients_list_view”:(顺便说一句,最后一项@android:id / list”之所以存在,仅是因为否则我的活动会遇到编译错误,我还没有弄清楚原因-否则我不需要它,并且它根本不显示或不做任何事情)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/label_recipe_name"
        android:layout_gravity="top"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textCapCharacters"
        android:id="@+id/editTextTitle"
        android:layout_gravity="top"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/label_servings"
        android:layout_gravity="top"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textCapCharacters"
        android:id="@+id/editTextServings"
        android:layout_gravity="top"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/label_cake_size"
        android:layout_gravity="top"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textCapCharacters"
        android:id="@+id/editTextSize"
        android:layout_gravity="top"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/btnEditDelete"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/button_edit"
                android:id="@+id/btnEdit"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/button_delete"
                android:id="@+id/btnDelete"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0"
            android:id="@+id/btnSaveCancel"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/button_save"
                android:id="@+id/btnSave"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/button_cancel"
                android:id="@+id/btnCancel"/>

        </LinearLayout>
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ingredients_list_view"
        android:divider="#48C"
        android:dividerHeight="1dp"
        android:layout_weight="1" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:id="@android:id/list"/>

</LinearLayout>

这是listView的每个项目的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/ingredient_list_item"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="22sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/quantity_list_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="22sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/unit_list_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="22sp"
        android:textStyle="bold" />
</LinearLayout>

...这些是主要活动的相关部分:


public class Recipe extends ListActivity implements View.OnClickListener {

    public static final String TABLE = "Recipes";

    public static final String KEY_ID = "recipeID";
    public static final String KEY_title = "title";
    public static final String KEY_servings = "servings";
    public static final String KEY_size = "size";

    public int recipeID;
    public String title;
    public String servings;
    public String size;

    Button btnEdit, btnDelete, btnSave, btnCancel;
    LinearLayout btnEditDelete, btnSaveCancel;
    EditText editTextTitle;
    EditText editTextServings;
    EditText editTextSize;

    int _Recipe_Id;
    int editMode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_view);
        redraw();
    }

    @Override
    public void onClick(View view) {
// These work perfectly fine (except for the last one)
        RecipeDBOps repo = new RecipeDBOps(this);
        ArrayList<HashMap<String, String>> recipeList = repo.getRecipeList(); // so far not used
        if (view == findViewById(R.id.btnDelete)) {
            MixDBOps.delete(_Recipe_Id);
            RecipeDBOps.delete(_Recipe_Id);
            finish();
        }
        else if (view == findViewById(R.id.btnEdit)) { // Changes the fields to "editable" and switches button
            setEditMode();
        }
        else if (view == findViewById(R.id.btnCancel)) {
            finish();
        }
        else if (view == findViewById(R.id.btnSave)){
            Recipe recipe = RecipeDBOps.getRecipeById(_Recipe_Id);
            recipe.title = editTextTitle.getText().toString();
            recipe.servings = editTextServings.getText().toString();
            recipe.size = editTextSize.getText().toString();
            RecipeDBOps.update (recipe);
            finish();
        }
        else if (view == findViewById(R.id.ingredients_list_view)){
            System.out.println("OK2"); // Tried, but doesn't work
        }
    }

    public void setWeight (LinearLayout ll, LinearLayout.LayoutParams visibility){...}

    public void setReadOnlyMode(){...}

    public void setEditMode(){...}

    public void redraw(){
        editTextTitle = (EditText) findViewById(R.id.editTextTitle);
        editTextServings = (EditText) findViewById(R.id.editTextServings);
        editTextSize = (EditText) findViewById(R.id.editTextSize);
        btnEdit = (Button) findViewById(R.id.btnEdit);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnSave = (Button) findViewById(R.id.btnSave);
        btnCancel = (Button) findViewById(R.id.btnCancel);
        btnEditDelete = (LinearLayout) findViewById(R.id.btnEditDelete);
        btnSaveCancel = (LinearLayout) findViewById(R.id.btnSaveCancel);

        setReadOnlyMode();

        //
        // Populate recipe fields
        //
        Intent intent = getIntent();
        _Recipe_Id = intent.getIntExtra("recipe_ID", 0);
        editMode = intent.getIntExtra("editMode", 0);
        RecipeDBOps repo = new RecipeDBOps(this);
        Recipe recipe;
        recipe = repo.getRecipeById(_Recipe_Id);

        if (recipe.title != null) {
            editTextTitle.setText(recipe.title);
            editTextServings.setText(recipe.servings);
            editTextSize.setText(recipe.size);
        }

        //
        // Populate recipe and ingredient fields
        //
        ArrayList<Entry> arrayOfEntries = new ArrayList<Entry>();
        EntryAdapter adapter = new EntryAdapter(this, arrayOfEntries);
        ListView listView = (ListView) findViewById(R.id.ingredients_list_view);
        listView.setAdapter(adapter);
////////////////////////////////////////////////////////////////////////////////////
        // This is the part that does not work
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ListView clickedObj = (ListView) parent.getItemAtPosition(position);
            // Once I am able to activate this onItemClick I will write the rest of the code to deal with clickdObj 
            System.out.println("OK1");
        }});
////////////////////////////////////////////////////////////////////////////////////


        HashMap<String, String> h;
        MixDBOps repoMix = new MixDBOps(this); // repo represents the DB
        ArrayList<HashMap<String, String>> entryList = repoMix.getMixById(_Recipe_Id); // recipeList is the output of the SQL query
        Entry[] ent = new Entry[entryList.size()];

        for (int i = 0; i < entryList.size(); i++) {
            h = entryList.get(i);
            ent[i] = new Entry();
            ent[i].recipe_name = recipe.title;
            ent[i].ingredient_name = h.get("ingredient_name"); // temporarily replaced by spinner
            ent[i].quantity= Integer.parseInt(h.get("quantity"));
            ent[i].unit_name= h.get("unit_name");
            adapter.add(ent[i]);
        }

        btnEdit = (Button) findViewById(R.id.btnEdit);
        btnEdit.setOnClickListener(this);

        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnDelete.setOnClickListener(this);

        btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(this);

        btnCancel = (Button) findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(this);

        if (editMode == 1){
            setEditMode();
        }
    }
}

有人可以帮助我吗?

java android listview onitemclicklistener onitemclick
1个回答
0
投票

在列表视图项目的每个编辑文本中添加两个属性

© www.soinside.com 2019 - 2024. All rights reserved.