4

How to check the CheckBox of ListView when item clicked?

I have a ListView with CheckBox, TextView, Button.

Here i want to select multiple rows of ListView and so used CheckBox. If i click on a row, i want to make its corresponding CheckBox to be checked and get the RowID of the clicked item of ListView.

If i click on Button, i want to open a pop-up or another Screen(Activity) but CheckBox must not be checked.

How to do this? Please help.

Thanks in advance.

Edit:

enter image description here

This is how my layout looks.

Code is simple using Adapter to the ListView.

   public class Adapter extends BaseAdapter{

    private Context context;
    Button btnView;
    CheckBox box;       
    ArrayList<String> altextView1;
    ArrayList<String> altextView2;


    public Adapter(Context c, Button view, CheckBox checkBox, ArrayList<String> textView1, ArrayList<String> textView2) {

        this.context=c;
        this.btnView=view;
        this.box=checkBox;
        this.altextView1=textView1;
        this.altextView2=textView2;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return altextView1.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub

        View view=arg1;
        view=LayoutInflater.from(context).inflate(R.layout.list7, null);

        TextView tv1=(TextView)view.findViewById(R.id.tV1);
        TextView tv2=(TextView)view.findViewById(R.id.tV2);
        btnView=(Button)findViewById(R.id.btnView);
        box=(CheckBox)findViewById(R.id.cB);

        tv1.setText(altextView1.get(arg0));
        tv2.setText(altextView2.get(arg0));


        return view;
    }

}
Sravani
  • 528
  • 1
  • 8
  • 13
  • any working code you have? – Rod_Algonquin Jun 11 '14 at 07:30
  • Go through[This Andswer](http://stackoverflow.com/questions/23512344/how-to-show-installed-app-list-in-a-listview-without-checkbox-getting-unchecked/23577344#23577344) i hope this will help you :) – Pankaj Arora Jun 11 '14 at 07:33
  • example: http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html – Opiatefuchs Jun 11 '14 at 07:34
  • This thread might also be helpful http://stackoverflow.com/questions/5417339/android-listview-with-checkbox-and-all-clickable –  Jun 11 '14 at 07:35

3 Answers3

2
  1. Set checkbox object as tag to your row view that might be your 'convertView' in getView() method of your adapter.

  2. Write on click listener on your row view.

  3. Inside that click-listener to row view getTag from view that's parameter in onClick method and cast it to checkbox and then setChecked to true for that checkbox object.

code might look like this,

        convertView.setTag(yourCheckBoxObject);
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v.getTag();
                cb.setChecked(true);
            }
        });

You need to follow this thread to learn it.. just read whole thread and try to understand and then implement .. this is the best help you can get. Getting an issue while checking the dynamically generated checkbox through list view

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • @Sravani then directly pass the checkbox object..simple.! – MKJParekh Jun 11 '14 at 13:15
  • yes, got it. But when i click again, its not unchecked again? Where to change the logic? – Sravani Jun 12 '14 at 04:15
  • And after selecting item, iam unable to get the item position. Also when i scroll the list down, previously selected checkbox is unchecked. – Sravani Jun 12 '14 at 04:34
  • @Sravani you need to follow this thread to learn it.. just read whole thread and try to understand and then implement .. this is the best help you can get. http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7738854#7738854 – MKJParekh Jun 12 '14 at 11:00
  • Yes it works, you might be getting problem while scrolling, once click it gets checked and then scroll it gets unchecked.. if this is the problem.. you should maintain one array for selected values @YogGuru – MKJParekh Feb 01 '16 at 09:55
  • @MKJParekh It works after your suggestion.. thank you u.. You save my life :) – Yog Guru Feb 01 '16 at 11:54
1

In your custom adaptor do like this

 @Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

    Button textView = (Button) layoutInflater.inflate(R.layout.btn, null);
    textView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                       //start activity or open pop-up
        }
    });
   ........

  }

And in onListItemClick callback :

protected void onListItemClick(ListView l, android.view.View v, int position, long id) {
   // using view v get check box and make it checked
   CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkbx);

};

Hope this will work.Sorry I have not tested. Please let me know the result.

Adarsh Gowda
  • 196
  • 2
  • 13
0

First, you will need to call setChoiceMode() on the ListView in Java code to set the choice mode, supplying either CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE as the value. You can get your ListView from a ListActivity via getListView(). You can also declare this via the android:choiceMode attribute in your layout XML.

Then, rather than use android.R.layout.simple_list_item_1 as the layout for the list rows in your ArrayAdapter constructor, you will need to use either

    android.R.layout.simple_list_item_single_choice 

or

    android.R.layout.simple_list_item_multiple_choice 

for single-choice or multiple-choice lists, respectively.

    <?xml version="1.0" encoding="utf-8"?>
    <ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:choiceMode="multipleChoice"
    />

and java code could be

    package com.commonsware.android.checklist;

    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;

    public class ChecklistDemo extends ListActivity {
    private static final String[] items={"lorem", "ipsum", "dolor",
    "sit", "amet","consectetuer", "adipiscing", "elit", "morbi", "vel",
    "ligula", "vitae", "arcu", "aliquet", "mollis",
    "etiam", "vel", "erat", "placerat", "ante",
    "porttitor", "sodales", "pellentesque", "augue", "purus"};

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_multiple_choice,items));
      }
    }
Vishal
  • 7
  • 9