Android 自定义ListView子项复选框[Hashmap<;String,boolean>;]
链接: 因此,我按照上面的教程链接使用以下方法添加子项:Android 自定义ListView子项复选框[Hashmap<;String,boolean>;],android,checkbox,android-listview,simpleadapter,Android,Checkbox,Android Listview,Simpleadapter,链接: 因此,我按照上面的教程链接使用以下方法添加子项: static final ArrayList list=new ArrayList() 为子项添加字符串 现在,我想在现有字符串中添加一个布尔复选框,我使用 static final ArrayList DAMN=new ArrayList() my private void listViewItem()的示例: 我只能得到的问题是我的复选框被选中或取消选中,而不是ID和名称。 除非我发表评论 //setListAdapter(adapt
static final ArrayList list=new ArrayList()代码>
为子项添加字符串
现在,我想在现有字符串中添加一个布尔复选框,我使用
static final ArrayList DAMN=new ArrayList()代码>
my private void listViewItem()的示例:
我只能得到的问题是我的复选框被选中或取消选中,而不是ID和名称。
除非我发表评论
//setListAdapter(adapter1)
只有我可以获取我的学生名和学生名,但不勾选复选框。
如何获得这两个结果?我无法给出全部代码,但首先,请执行以下操作:
首先为您的数据创建数据保持器,即:字符串和布尔值等。如果要使用复合自定义对象作为数据保持器,而不是原始字符串和布尔值
public class DataHolder{
private String studentNname;
private Boolean attended;
//add getters and setters etc.
}
现在像这样实现您的适配器
public class MyCustomAdapter extends BaseAdapter{
//populate this data using constructor or a setter or any other way
private List<DataHolder> data;
private LayoutInflater inflater = ....
//get the inflater form the fragment or the activity which ever you are using;
//implement overridden and abstract methods
//for counts of items return the size of HashMap
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
convertView = inflater.inflate(R.layout.<your list item layout xml file>, null);
}
DataHolder dataHolder = this.getItem(position);
//now you have the convertView (your list item view)
//and the data to be show in that view (dataHolder object)
//populate the convertView with the data from the dataHolder
return convertView;
}
}
公共类MyCustomAdapter扩展了BaseAdapter{
//使用构造函数、setter或任何其他方式填充此数据
私人名单数据;
私人充气机=。。。。
//从碎片或您正在使用的活动中获取充气器;
//实现重写和抽象方法
//对于项目计数,返回HashMap的大小
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=充气机充气(右布局,空);
}
DataHolder DataHolder=此.getItem(位置);
//现在您有了convertView(列表项视图)
//以及要在该视图中显示的数据(dataHolder对象)
//使用来自数据持有者的数据填充convertView
返回视图;
}
}
最后,创建此适配器的实例,向其提供数据,即:List并将此实例传递给listView的setAdapter。您需要一个自定义适配器和自定义逻辑来处理此问题。创建自己的列表适配器,并在其getView方法中根据逻辑执行所需操作。查看google,了解自定义列表适配器的示例。如何将复选框的“Hashmap”和“Hashmap”组合在一起?有可能吗?@Nazgul是的,如果您使用自定义适配器。普通适配器只有一个数据源,通常是列表或映射。您可以在适配器中同时使用这两个HashMap,并且假定您对这两个HashMap使用相同的键,您可以在自定义适配器的getView方法中从它们中提取布尔值和字符串值。我是否已经接近它了?我还需要在代码中添加什么?我不能得到它,你能帮我简单的代码吗?谢谢@Nazgul投票人能告诉我为什么这里是-1吗?该方法是一种非常可扩展和实用的方法。
public class DataHolder{
private String studentNname;
private Boolean attended;
//add getters and setters etc.
}
public class MyCustomAdapter extends BaseAdapter{
//populate this data using constructor or a setter or any other way
private List<DataHolder> data;
private LayoutInflater inflater = ....
//get the inflater form the fragment or the activity which ever you are using;
//implement overridden and abstract methods
//for counts of items return the size of HashMap
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
convertView = inflater.inflate(R.layout.<your list item layout xml file>, null);
}
DataHolder dataHolder = this.getItem(position);
//now you have the convertView (your list item view)
//and the data to be show in that view (dataHolder object)
//populate the convertView with the data from the dataHolder
return convertView;
}
}