如何在按钮点击时选择自定义列表视图的所有复选框?

时间:2016-03-10 07:12:09

标签: android listview

我希望在点击“selectAll”按钮时选择所有复选框。我有自定义列表视图,带有复选框和textView。下面我把我的代码和屏幕截图作为参考。

screen shot of listView

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lvMain"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

            <Button
                android:id="@+id/btnSelectAll"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:text="Select All" >
            </Button>
        </LinearLayout>

list_item.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/cbItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#000000">
        </CheckBox>

        <TextView
            android:id="@+id/tvItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000">
        </TextView>

    </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
ListView lvMain;
String[] name = { "Jenis", "Pratik", "Jaydeep", "Hiren", "Himansu",
        "yagnik", "Atul", "Prakas", "Nihal", "Darshan", "Chetan", "Sagar",
        "Nikhil", "Sanket", "Rahul", "Jigar" };
Button btnSelectAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lvMain = (ListView) findViewById(R.id.lvMain);
    btnSelectAll = (Button) findViewById(R.id.btnSelectAll);
    ListAdapter adapter = new ListAdapter(getApplicationContext(), name);
    lvMain.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}
}

ListAdapter.java

package com.example.checkallcheckbox;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
    Context mContext;
    String[] name;
    private static LayoutInflater inflater = null;
    public ListAdapter(Context c, String[] name) {
        // TODO Auto-generated constructor stub
        mContext = c;
        this.name = name;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return name.length;
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    public class ViewHolder {
        TextView tvName;
        CheckBox cbName;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView
                    .findViewById(R.id.tvItem);
            viewHolder.cbName = (CheckBox) convertView
                    .findViewById(R.id.cbItem);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvName.setText(name[position]);
        return convertView;
    }
}

3 个答案:

答案 0 :(得分:2)

传递一个标志并更新适配器

     ListAdapter adapter = new ListAdapter(getApplicationContext(), name,false);
        lvMain.setAdapter(adapter);

     ListAdapter adapter = new ListAdapter(getApplicationContext(), name,true);
        adapter.notifyDataSetChanged();

     public ListAdapter(Context c, String[] name,Boolean flag) {
            // TODO Auto-generated constructor stub
            mContext = c;
            this.name = name;
    this.flag=flag
            inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }


 @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView
                    .findViewById(R.id.tvItem);
            viewHolder.cbName = (CheckBox) convertView
                    .findViewById(R.id.cbItem);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvName.setText(name[position]);

if(flag==true)
{
cbName .setChecked(true);
}
        return convertView;
    }

答案 1 :(得分:0)

public class MainActivity extends Activity {
ListView lvMain;
boolean isSelectAll = false;

On Button click make isSelectAll value as true and call notifydatasetchage on adapter.

    public boolean isSelectALL(){
       return isSelectAll;
    }

在适配器类中,获取acivity类的实例

if(activity.isSelectALL()){
    viewHolder.cbName.setChecked(true)
}else{
    viewHolder.cbName.setChecked(false)
}

希望这会对你有所帮助。

答案 2 :(得分:0)

您的代码看起来应该是这样的。检查状态应该来自ArrayList&lt;&gt;或包含数据的Array []。当一个复选框通过调用people.get(position).setChecked(true / false);

来切换状态时,记得更新对象的选中状态。
class Person{
    String name;
    boolean checked;
    public Person(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public boolean isChecked(){
        return checked;
    }
    public void setChecked(boolean checked){
        this.checked=checked;
    }
}

class PersonsAdapter extends BaseAdapter{
    LayoutInflater inflater;
    ArrayList<Person> persons;
    public PersonsAdapter(Context context, ArrayList<Person> persons){
        this.persons=persons;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView
                    .findViewById(R.id.tvItem);
            viewHolder.cbName = (CheckBox) convertView
                    .findViewById(R.id.cbItem);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvName.setText(persons.get(position).getName());
        viewHolder.cbName.setChecked(persons.get(position).isChecked());
    }

}

和点击监听器

       selectAllButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for(Person person:persons){
                    person.setChecked(true);
                }
                adapter.notifyDataSetChanged();
            }
        });