选择列表视图在android的所有复选框 [英] Selecting all checkboxes of listview in android
问题描述
我有自定义arrayadapter的列表视图,其内部包括复选框5次,在这里,我要实现的选择列表视图中所有的复选框,并取消所有复选框,我试着用 getchildat()
的方法的OnCreate()
, onresume()
和 onpostcreate
还,但不做出运气给我。我AP preciate,如果你能告诉我的解决方案。
i am having custom arrayadapter for listview, which has 5 views inside including checkbox, here i want to implement the select all checkboxes of listview and deselect all checkboxes, i tried with getchildat()
method in the oncreate()
, onresume()
, and onpostcreate
also but does not made luck to me. i appreciate if you can tell me the solution.
@Override
public void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
Button selectall=(Button) findViewById(R.id.allselect);
selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int count = listview.getCount();
System.out.println("the count is "+count);
for (int i = 0; i < listview.getLastVisiblePosition() - listview.getFirstVisiblePosition(); i++) {
RelativeLayout itemLayout = (RelativeLayout)listview.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.chkbx);
cb.setChecked(true);
}
}
});
}
一个ArrayAdapter类
Arrayadapter class
@SuppressWarnings("unchecked")
public MultipleLeadSyncAdapter(Context context, String[] values,String[] values1,List<Model> qrcode ,String[] values3,String[] values4,String[] values5) {
super(context, R.layout.multipleselectlist, values1);
this.context = context;
this.values = values;
this.values1 = values1;
this.list=qrcode;
this.values3=values3;
this.values4=values4;
this.eventid=values5;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder =null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.multipleselectlist, parent, false);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.multileadfirst_name);
holder.textView3 = (TextView) convertView
.findViewById(R.id.multileadcompany_name);
holder.rate=(RatingBar) convertView.findViewById(R.id.multileadlistrating);
holder.chkbox = (CheckBox) convertView.findViewById(R.id.chkbx);
holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(holder);
convertView.setTag(R.id.chkbx,holder.chkbox);
convertView.setTag(R.id.multileadfirst_name,holder.textView1);
convertView.setTag(R.id.multileadcompany_name,holder.textView3);
convertView.setTag(R.id.multileadlistrating,holder.rate);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder = (ViewHolder) convertView.getTag();
holder.textView1.setText(values[position]);
if(values3[position].equals(" ")||values3[position].equals("null")||values3[position].equals("")){
}else{
String[] temp=values3[position].split(" ");
}
holder.textView3.setText(values1[position]);
holder.rate.setRating(Float.valueOf(values4[position]).floatValue());
String s = values[position];
String ss = values1[position];
System.out.println(s + ss );
holder.chkbox.setTag(position);
//holder.textView3.setText(list.get(position).getName());
holder.chkbox.setChecked(list.get(position).isSelected());
String igurl=values[position];
System.out.println("the imagurl is "+igurl);
return convertView;
}
class ViewHolder {
String qrcode,Boothid;
TextView textView1, textView2, textView3,textView4,textView5;
RatingBar rate;
ImageView imageView;
CheckBox chkbox;
public String getQrcode() {
return qrcode;
}
public void setQrcode(String qrcode) {
this.qrcode = qrcode;
}
public String getBoothID() {
return Boothid;
}
public void setBoothID(String Boothid) {
this.Boothid = Boothid;
}
}
}
推荐答案
A 的ListView
的数据只是一个presentation。该数据是由适配器
给予的ListView
。这是说,我可以解释你做了什么错误的:
A ListView
is just a presentation of data. The data is given to the ListView
by an Adapter
. This being said, I can explain what you did wrong:
在你的 OnClickListener
,你是(联合国)检查一个查看
的的ListView
本身。这意味着数据没有被修改和的ListView
从而重新presents这些数据,不会显示想要的行为。
Inside your OnClickListener
, you are (un)checking a View
of the ListView
itself. This means the data is not being changed and the ListView
which represents this data, does not display the wanted behavior.
有一个简单的解决办法是增加一个布尔值,你的型号
-object这表明选中状态:
A simple solution would be adding a boolean to your Model
-object which indicates the checked-state:
Model {
boolean isChecked = false;
public void setChecked(boolean checked){
isChecked = checked;
}
public boolean isChecked(){
return isChecked;
}
}
当你点击selectall'按钮,你将简单的切换这个布尔并通知适配器
,他的数据已经与方法改变 notifyDataSetChanged
。这将在轮到他更新的ListView
本身。在伪code:
Whenever you click the 'selectall'-button, you will simple toggle this boolean and notify the Adapter
that his data has changed with the method notifyDataSetChanged
. This will on his turn update the ListView
itself. In pseudocode:
onClick(..) {
foreach (model inside my adapter){
model.setChecked(true);
}
listview.getAdapter().notifyDataSetChanged()
}
最后但并非最不重要的,在 getView()
-method你的适配器
里面,你可以,如果一见型号
检查或不并据此采取行动。
Last but not least, in the getView()
-method inside your Adapter
you can see if a Model
is checked or not and act accordingly.
getView(...){
if (listOfModel.get(position).isChecked){
// do stuff, probably check a CheckBox
} else {
// uncheck...
}
祝你好运!
这篇关于选择列表视图在android的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!