How to check CheckBox ListView when clicking on an item?

How to check a CheckBox ListView when an item is clicked?

I have a ListView with CheckBox, TextView, Button.

Here I want to select some rows of ListView and so use CheckBox. If I click on a row, I want its corresponding CheckBox to be checked and get a RowID for the ListView item.

If I click the "I" button, I want to open a pop-up window or another screen (Activity), but the CheckBox should not be checked.

How to do it? Please, help.

Thanks in advance.

Edit:

enter image description here

This is what my layout looks like.

The code is simple using an adapter in 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;
    }

}
+4
source share
3 answers
  • checkbox , "convertView" getView() .

  • .

  • - getTag onClick , CheckCheck true .

:

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

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

, . , .. , .

+2

:

 @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
        }
    });
   ........

  }

onListItemClick:

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

};

, . , . , .

+1

setChoiceMode() ListView Java-, , CHOICE_MODE_SINGLE CHOICE_MODE_MULTIPLE . ListView ListActivity getListView(). android: choiceMode XML.

android.R.layout.simple_list_item_1 list ArrayAdapter,

    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));
      }
    }
0
source

Source: https://habr.com/ru/post/1544111/


All Articles