项目单击时如何检查ListView的复选框? [英] How to check the CheckBox of ListView when item clicked?
问题描述
如何在单击项目时检查ListView的复选框?
How to check the CheckBox of ListView when item clicked?
我有一个带CheckBox,TextView,Button的ListView.
I have a ListView with CheckBox, TextView, Button.
在这里,我想选择ListView的多行,然后使用CheckBox.如果我单击一行,则要对其对应的CheckBox进行检查,并获取ListView的单击项的RowID.
Here i want to select multiple rows of ListView and so used CheckBox. If i click on a row, i want to make its corresponding CheckBox to be checked and get the RowID of the clicked item of ListView.
如果我单击Button,我想打开一个弹出窗口或另一个Screen(活动),但是不能选中CheckBox.
If i click on Button, i want to open a pop-up or another Screen(Activity) but CheckBox must not be checked.
如何执行此操作?请帮忙.
How to do this? Please help.
谢谢.
这是我的布局外观.
使用Adapter到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;
}
}
推荐答案
-
将复选框对象设置为行视图的标签,该行视图可能是适配器的getView()方法中的"convertView".
Set checkbox object as tag to your row view that might be your 'convertView' in getView() method of your adapter.
在行视图上点击侦听器.
Write on click listener on your row view.
在该单击侦听器中,从onClick方法中作为参数的视图中行视图getTag并将其强制转换为复选框,然后对该复选框对象设置Checked为true.
Inside that click-listener to row view getTag from view that's parameter in onClick method and cast it to checkbox and then setChecked to true for that checkbox object.
代码可能看起来像这样
convertView.setTag(yourCheckBoxObject);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v.getTag();
cb.setChecked(true);
}
});
您需要按照此线程来学习它..只需阅读整个线程并尝试理解然后实施..这是您可以获得的最佳帮助. 在检查通过列表视图动态生成的复选框
这篇关于项目单击时如何检查ListView的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!