Home » Java » java – I can't find a way to select items from a list, each made of three text fields-Exceptionshub

java – I can't find a way to select items from a list, each made of three text fields-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have looked at tens of examples and tutorials, and I have tried to repeat the same steps, but it is not working.

In my activity, I have four onClickListeners that work perfectly fine and are related to four buttons on the top part of my layout. The problem arises when, in addition, I try to set an onItemClickListener for the items of the ListView in the bottom part of the same layout. Each item of this ListView is made of three textViews, and I have written a custom adapter for them. They show up perfectly, but when I click on them nothing happens (the onItemClick instructions are never reached).

This is the layout, the problem is with the “ingredients_list_view” towards the end:
(by the way, the very last item, @android:id/list”, is there only because otherwise I get a compile error in my activity, I haven’t yet figured out why – otherwise I don’t need it, and it is not supposed to show or do anything at all)

<?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>

This is the layout for each item of the 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>

… and these are the relevant sections of the main activity:


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();
        }
    }
}

Is anybody able to help me?

How to&Answers:

add two attribute in each edit text of item of list view

    android:focusable="false"
    android:clickable="false"