1

I used textview and checkbox as items of listView. My code that i wrote for listview worked properly. But when I have added checkbox into the listview onclicklistener event don't work now. I use this code in xml file:

My xml code:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView_Item_Listview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:gravity="right"
        android:padding="3dp"
        android:textSize="25sp" />

</LinearLayout>

My java code:

private class Adapter_collection extends ArrayAdapter<String> {

    public Adapter_collection(Context context, int resource, int textViewResourceId,
            String[] name_collection_tbl_collection) {
        super(context, resource, textViewResourceId, name_collection_tbl_collection);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.listview_items, parent, false);
        TextView txt_item_list_collection = (TextView) row.findViewById(R.id.textView_Item_Listview);
        CheckBox checkBox=(CheckBox)row.findViewById(R.id.checkBox_Item_Listview);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked ) {
            Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });
        txt_item_list_collection.setText(name_collection_tbl_collection[position]);
        return row;
    }

}


list_collections.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent goTo_SubjectsActivity = new Intent(Collection_List_Activity.this, Subjects_Activity.class);
            startActivity(goTo_SubjectsActivity);
            Intent_values.id_collection = id_tbl_collection[arg2];
        }
    });
hmahdavi
  • 2,250
  • 3
  • 38
  • 90

2 Answers2

2

The possible Causes are if you have touchable elements in your List_item,it will block the list_item Touch,so onItemclickListener on your ListView Wont Work. if you want to implement onClickListener to your ListView you can add

android:descendantFocusability="blocksDescendants"

in you list_item parent that is inside LinearLayout

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • Rajan KS: I set android: focusable="false" and android: focusableInTouchMode="false" for TextView and CheckBox.It solved. Thank you for answering – hmahdavi Sep 28 '15 at 13:44
1

Try to assign the onclicklistener to the textview, instead of the list.

    private class Adapter_collection extends ArrayAdapter<String> {

...

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.listview_items, parent, false);
            TextView txt_item_list_collection = (TextView) row.findViewById(R.id.textView_Item_Listview);

            //add this
            txt_item_list_collection.setOnClickListener(new OnClickListener() { ... });

            txt_item_list_collection.setText(name_collection_tbl_collection[position]);
            return row;
        }

    }
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • Thank you for answering. I want to implement setOnCheckedChangeListener () for checkbox and get the position of checkboxes in this method. I implement like your answer But get an error for scope of position.Can you guide me – hmahdavi Sep 28 '15 at 14:15
  • 1
    You can get the reference to the CheckBox the same way I did for the textview by using `findViewById`, then just add a listener to it. – James Wierzba Sep 28 '15 at 14:18
  • That is code, not an error message? B.t.w. please post code in your question as an edit, I can't read it here! – James Wierzba Sep 28 '15 at 14:21
  • I change the Adapter_collection class . The error exist in this line : Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show(); – hmahdavi Sep 28 '15 at 16:15