How to get all selected list items using checkbox

I use Checkbox for each list item, and now I would like to get the name of all selected list items when I click the button.

I published the Adapter and Activity class code because later I need to map the selected list items to another action in the ListView.

But for now, I just want to see all the selected list items when I click the button

Adapter:

  @Override public View getView(final int position, View convertView, ViewGroup parent) { // convert view = design .................... holder.tvName.setText(actorList.get(position).getName()); holder.checkBox.setOnCheckedChangeListener(null); holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ // add into arraylist selectedname.add(actorList.get(position).getName()); }else{ // remove from arraylist selectedname.remove(actorList.get(position).getName()); } } }); return v; } 
+5
source share
6 answers

The code below works fine for me, so you can try this

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/select_btn" android:text="Select"/> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview"/> 

list_row.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="4dp" android:orientation="horizontal" > <CheckBox android:id="@+id/cbBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" > </CheckBox> <TextView android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_gravity="center_vertical" android:ellipsize="marquee" /> 

Service

 import java.io.Serializable; public class Service implements Serializable { private static final long serialVersionUID = 1L; private String name; private boolean selected; public Service() { // TODO Auto-generated constructor stub } public Service(String name) { super(); this.name = name; } public String getName() { return name; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } public void setName(String name) { this.name = name; } 

}

ServiceAdapter

 import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class ServiceAdapter extends BaseAdapter { ArrayList<Service> actorList; LayoutInflater vi; Context context; public ServiceAdapter(Context context,ArrayList<Service> objects) { this.context= context; this.vi = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.actorList = objects; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // convert view = design final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = vi.inflate(R.layout.list_row, null); holder.tvName = (TextView) convertView.findViewById(R.id.textView1); holder.checkBox = (CheckBox) convertView.findViewById(R.id.cbBox); convertView.setTag(holder); } else holder = (ViewHolder) convertView.getTag(); holder.tvName.setText(actorList.get(position).getName()); holder.checkBox.setChecked(actorList.get(position).isSelected()); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isSelected = ((CheckBox)v).isChecked(); actorList.get(position).setSelected(isSelected); } }); return convertView; } static class ViewHolder { public TextView tvName; public CheckBox checkBox; } @Override public int getCount() { // TODO Auto-generated method stub return actorList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return actorList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public ArrayList<Service> getSelectActorList(){ ArrayList<Service> list = new ArrayList<>(); for(int i=0;i<actorList.size();i++){ if(actorList.get(i).isSelected()) list.add(actorList.get(i)); } return list; } 

}

Mainactivity

 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listView = (ListView)findViewById(R.id.listview); listView.setAdapter(new ServiceAdapter(this,sampleData())); Button btn = (Button)findViewById(R.id.select_btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList<Service> actorList = ((ServiceAdapter)listView.getAdapter()).getSelectActorList(); Toast.makeText(MainActivity.this,""+actorList.size(),Toast.LENGTH_LONG).show(); } }); } public ArrayList<Service> sampleData(){ ArrayList<Service> dataList = new ArrayList<>(); for(int i=0;i<30;i++){ Service servic = new Service(); servic.setName("Test"+i); dataList.add(servic); } return dataList; } 

}

+5
source

It can help you.

 ArrayList<String> selectedname = new ArrayList<String>(); @Override public View getView(final int position, View convertView, ViewGroup parent) { // convert view = design View v = convertView; if (v == null) { holder = new ViewHolder(); v = vi.inflate(Resource, null); holder.tvName = (TextView) v.findViewById(R.id.textView1); holder.checkBox = (CheckBox) v.findViewById(R.id.cbBox); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.tvName.setText(actorList.get(position).getName()); checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ // add into arraylist selectedname.add(actorList.get(position).getName()); }else{ // remove from arraylist selectedname.remove(actorList.get(position).getName()); } } }); return v; } static class ViewHolder { public TextView tvName; public CheckBox checkBox; } 

pass this selected selectedname List to another activity. on Button Click the list of transitions between Activity, for example

+2
source

create a new arraylist element and insert a checkbox into it

 ArrayList<String> checkedSelection = new ArrayList<String>(); holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ // add into arraylist checkedSelection.add(holder.checkBox.getText().toString()); }else{ // remove from arraylist checkedSelection.remove(position); } } }); 

and when you click the button, send arraylist to another activity and load listview

0
source

To do this, do as shown below.

Im MyAdapter creates a class level variable

 public boolean[] checked; 

and write onCheckedChanged event

 @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checked[position] = select.isChecked(); } 

You can now access the boolean array as shown below

 ListAdapter adapter = listView.getAdapter(); boolean[] check =((MyAdapter) adapter).checked; 

Now you can retrieve marked items

Hope this is helpful

Edit:

Below line

 holder.checkBox = (CheckBox) v.findViewById(R.id.cbBox); 

Add a listener like this

 holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checked[position] = select.isChecked(); } }); 
0
source

To do this, you need to maintain the value of Checkbox in the class Actor bean

I updated your getView() method of your adapter class. Take a look.

 @Override public View getView(final int position, View convertView, ViewGroup parent) { // convert view = design View v = convertView; ViewHolder holder; if (v == null) { holder = new ViewHolder(); v = vi.inflate(Resource, null); holder.tvName = (TextView) v.findViewById(R.id.textView1); holder.checkBox = (CheckBox) v.findViewById(R.id.cbBox); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.tvName.setText(actorList.get(position).getName()); holder.checkBox.setOnCheckedChangeListener(null); holder.checkBox.setChecked(actorList.get(position).getStatus()); holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { actorList.get(position).setStatus(isChecked); } }); return v; } 

Be sure to add getStatus() and setStatus(boolean) to the bean class.

0
source

The best way to do this is:

Make model with boolean isChecked as property

 public Class CheckModel{ String name; Boolean isChecked; public CheckModel(String name,Boolean isChecked){ this.name = name; this.isChecked = isChecked; } //setter and getter here } 

When adding a model to the arraylist isChecked to false set by default

  arrayList.add(new CheckModel("title",false)); 

now in the getView () function in Adapter change the isChecked property to true onChecked

arrayList.get(position).setChecked(true);

Finally, if you want the name of all test items to do this

 for(int i = 0; i<arrayList.size;i++){ if(arrayList.get(i).getisChecked()){ //retreive the name }else{ //ignore } } 
0
source

All Articles