I am using Checkbox
with each and every list item, and now i would like to get name of all the selected list items on button click.
I have posted code of Adapter
and Activity
class as well, because later i have to show selected list items into another activity into ListView.
But for now i just want to see all the selected list items on button click
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;
}
In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.
The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!
The below code working 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With