Java 如何将listview与复选框一起使用,并在单击复选框时执行某些任务?
如何在listview中使用复选框。在这里,我想对复选框单击执行一些任务。我不知道如何使用复选框选择特定的id或listitem值 只需覆盖阵列适配器的Java 如何将listview与复选框一起使用,并在单击复选框时执行某些任务?,java,android,kindle-fire,Java,Android,Kindle Fire,如何在listview中使用复选框。在这里,我想对复选框单击执行一些任务。我不知道如何使用复选框选择特定的id或listitem值 只需覆盖阵列适配器的getview方法。并使用viewholder获取customlist的复选框 示例代码: public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; Log.v("ConvertView", St
getview
方法。并使用viewholder
获取customlist的复选框
示例代码:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.your_layout, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
}
});
}
有关简要教程参考链接
我希望这将对您有所帮助。这可以通过使用compound按钮来完成。OnCheckChangedListener
。您可以将此侦听器设置为内联,如下所示:
//Create a CheckBox object from your CheckBox id that is in the ListView in your xml layout file
CheckBox foo = (CheckBox) findViewById(R.id.foo);
foo.setOnCheckedChangedListener( new CompoundButton.OnCheckedChangedListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked = true){
// perform some operations when checked
} else {
// perform some operations when unchecked
}
});
有关Android复选框和复合按钮(复选框超类)的文档可在以下位置找到:
Android文档非常有用,有时很难通过它找到你想要的东西。希望这能回答你的问题 试试这个,
列表适配器:-
public class CheckBoxInListAdapter extends BaseAdapter{
private Context context=null;
private List<ArrayListObjects> searchArrayList=null;
private LayoutInflater mInflater=null;
private ViewHolder holder=new ViewHolder();
public CheckBoxInListAdapter(Context context, List<ArrayListObjects> mData)
{
this.context=context;
searchArrayList = mData;
mInflater = LayoutInflater.from(context);
}
public int getCount()
{
return searchArrayList.size();
}
public Object getItem(int position)
{
return searchArrayList.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.sample_layout,null);
holder=new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textView);
holder.itemNormalChecked=(CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(searchArrayList.get(position).getItemName());
holder.itemNormalChecked.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(searchArrayList.get(position).isItemChecked())
{
searchArrayList.get(position).setItemChecked(false);
}
else
{
searchArrayList.get(position).setItemChecked(true);
}
}
});
if(searchArrayList.get(position).isItemChecked())
{
holder.itemNormalChecked.setChecked(true);
}
else
{
holder.itemNormalChecked.setChecked(false);
}
return convertView;
}
static class ViewHolder
{
TextView textView=null;
CheckBox itemNormalChecked=null;
}
}
然后在你的活动中,
ArrayList<ArrayListObjects> arrayListObjects=new ArrayList<ArrayListObjects>();
for(int i=0;i<15;i++)
{
arrayListObjects.add((new ArrayListObjects()).setItemName("value -"+String.valueOf(position);
}
listView.setAdapter(new CheckBoxInListAdapter(activityName.this,arrayListObjects);
ArrayList arrayListObjects=new ArrayList();
对于(int i=0;i)来说,我将展示你的代码。到目前为止,你尝试了什么。这是android中非常基本的问题。在谷歌搜索中,你会得到很多答案。。。
public class ArrayListObjects {
private boolean isItemChecked=false;
private String itemName="null";
public boolean isItemChecked() {
return isItemChecked;
}
public void setItemChecked(boolean isItemChecked) {
this.isItemChecked = isItemChecked;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
ArrayList<ArrayListObjects> arrayListObjects=new ArrayList<ArrayListObjects>();
for(int i=0;i<15;i++)
{
arrayListObjects.add((new ArrayListObjects()).setItemName("value -"+String.valueOf(position);
}
listView.setAdapter(new CheckBoxInListAdapter(activityName.this,arrayListObjects);
for(int i=0;i<listView.getCount();i++)
{
ArrayListObjects obj=(ArrayListObjects)listView.getItemAtPosition(i);
if(obj.isItemChecked())
{
Log.e("checked items - ",obj.getItemName());
}
}