如何在 Android 中计算 ListView 上的已检查项目
Posted
技术标签:
【中文标题】如何在 Android 中计算 ListView 上的已检查项目【英文标题】:How to count Checked Items on ListView in Android 【发布时间】:2016-02-28 21:08:19 【问题描述】:亲爱的堆垛机,你好,
我遇到了一个小问题,希望您能帮我解决这个问题。让我们切入正题。
我的代码应该做什么? -> 我的代码应该设置一个带有 CheckBoxes 的 ListView,然后我应该能够检查框,然后计算按下按钮后检查了多少框。 然后我应该将对象传递给下一个 Activity,但这不应该是这个问题的一部分,我只是添加这个作为上下文,它可能对解决方案很重要。
我的代码没有做什么? -> 我的代码不计算盒子。 listView.getCheckedItemIds().length)); 行只返回 0。
在重新阅读 ListView、List、Adapter、View 等文档并尝试了一些不同的方法(和教程,一些来自 ***)之后,我不知道如何处理或从哪里开始
这是代码 --------------------> 主类
package com.example.christian.listviewwithcheckbox_model;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener
int checkCounter = 0;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
private Button mButton;
private TextView mCounter;
public void onCreate(Bundle icicle)
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.my_list);
adapter = new MyAdapter(this, getModel());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
mCounter = (TextView) findViewById(R.id.counter_text);
mButton = (Button) findViewById(R.id.count_button);
mButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
int count = 0;
int size = list.size();
for (int i=0; i<size; i++)
if (list.get(i).isSelected())
count++;
mCounter.setText(String.format("%d", count));
);
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString() + " " + isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
private String isCheckedOrNot(CheckBox checkbox)
if (checkbox.isChecked())
return "is checked";
else
return "is not checked";
private List<Model> getModel()
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("php"));
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
return list;
-------------> 适配器类
package com.example.christian.listviewwithcheckbox_model;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends ArrayAdapter<Model>
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list)
super(context, R.layout.row, list);
this.context = context;
this.list = list;
static class ViewHolder
protected TextView text;
protected CheckBox checkbox;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder viewHolder = null;
if (convertView == null)
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
-------------------> 模型类
package com.example.christian.listviewwithcheckbox_model;
public class Model
private String name;
private boolean selected;
public Model(String name)
this.name = name;
public String getName()
return name;
public boolean isSelected()
return selected;
public void setSelected(boolean selected)
this.selected = selected;
好吧,那是我的问题,很抱歉在上一个帖子之后这么快就打开了一个帖子,但此时我遇到了很多麻烦,需要一个解决方案。 提前感谢您,祝您有美好的一天。 埃默尔
编辑:问题已由 Hoang Nguyen 和 Hardy 解决,非常感谢 上面的代码已经更新,现在显示工作代码。
【问题讨论】:
【参考方案1】:您使用自定义复选框,所以不能使用方法getCheckedItemIds()
,请参考doc和ListView Mode
对于您的问题,可以像这样轻松更改按钮单击方法:
mButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
int count = 0;
int size = list.size();
for (int i=0; i<size; i++)
if (list.get(i).isSelected())
count++;
mCounter.setText(String.format("%d", count));
);
【讨论】:
【参考方案2】:把你的下面几行
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
上面
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
);
所以您的代码最终将如下所示:-
if (convertView == null)
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
);
【讨论】:
在我的情况下它正在崩溃 onCheckedChanged,请帮助以上是关于如何在 Android 中计算 ListView 上的已检查项目的主要内容,如果未能解决你的问题,请参考以下文章
Android -- 每日一问:如何解决ScrollView嵌套中一个ListView的滑动冲突?