自定义的ListView子项的复选框[&的Hashmap LT;字符串,布尔>] [英] Custom ListView Subitem checkbox [Hashmap <String,boolean>]

查看:170
本文介绍了自定义的ListView子项的复选框[&的Hashmap LT;字符串,布尔>]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接:的ListView < A HREF =htt​​p://i.imgur.com/ErHtV0m.jpg相对=nofollow> myexample中

于是我按照上面教程链接使用加分项:


  

静态最后的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT;名单=新的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT;();


添加字符串的子项目。

现在,我想一个布尔复选框添加到现有的字符串,我用


  

静态最后的ArrayList&LT;&HashMap的LT;字符串,布尔值&GT;&GT;该死=新的ArrayList&LT;&HashMap的LT;字符串,布尔值&GT;&GT;();


我的私人无效的ListViewItem的示例():

 私人无效ListViewItem的(){
    HashMap的&LT;字符串,字符串&GT;哈希=新的HashMap&LT;字符串,字符串&GT;();
    hash.put(Student_name,约书亚);
    hash.put(student_id数据,111111464);
    list.add(散);
    HashMap的&LT;字符串,布尔值&GT;嘘=新的HashMap&LT;字符串,布尔值&GT;();
    boo.put(参加,真正的);
    DAMN.add(BOO);'

在我的onCreate,我既包括SimpleAdapter的适配器(字符串,字符串)和(字符串,布尔值)


  

SimpleAdapter适配器=新SimpleAdapter(
                  对此,OMG,R.layout.tolist_row,
                  新的String [] {Student_name,student_id数据},
                  新的INT [] {R.id.StudenName,R.id.studentID});
  SimpleAdapter适配器1 =新SimpleAdapter(这一点,该死的,R.layout.tolist_row1,
                  新的String [] {参加},
                  新的INT [] {R.id.attend});


  ListViewItem的();
    setListAdapter(适配器);
    setListAdapter(适配器1);

我只能得到问题是我的复选框选中或取消选中,但没有编号和名称。
除非我的评论
// setListAdapter(适配器1)只有我能得到我的Student_name和Student_name但没有复选框选中。
我如何兼得的结果?


解决方案

我不能给你整个code,但一开始,做这样的事情:

首先创建DataHolder为您的数据即:字符串和Booeleans等的主意,如果要使用复合自定义对象为持有人的数据,而不是原始字符串和布尔值

 公共类DataHolder {
    私人字符串studentNname;
    私人布尔出席;    //添加getter和setter方法​​等。}

现在实现你的适配器是这样的。

 公共类MyCustomAdapter延伸BaseAdapter {    //使用构造函数或setter方法​​或任何其他方式填充这个数据
    私人列表&LT; D​​ataHolder&GT;数据;    私人LayoutInflater气筒= ....
    //获取充气形成片段或正在使用的有史以来的活动;
    //实现覆盖和抽象方法
    //物品计数返回的HashMap的大小    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        如果(convertView == NULL){
            convertView = inflater.inflate(R.layout&LT;您的列表项目布局xml文件&gt;中NULL);
        }
        DataHolder dataHolder = this.getItem(位置);
        //现在你有convertView(您的列表项视图)
        //和数据被显示在该视图(dataHolder对象)
        //填充从时,DataHolder的数据convertView        返回convertView;
    }}

最后,创建该适配器的一个实例,它提供数据即:列表和这个实例传递给的ListView的setAdapter

Link : ListView MyExample

So I followed the tutorial link above to add subitem using :

static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

to add String for subitem.

Now, I wanted to add a boolean checkbox to the existing String, I use

static final ArrayList<HashMap<String,Boolean>> DAMN=new ArrayList<HashMap<String,Boolean>>();

Example of my private void listViewItem():

    private void listViewItem(){
    HashMap<String,String> hash=new HashMap<String, String>();
    hash.put("Student_name","Joshua");
    hash.put("Student_ID","111111464");
    list.add(hash);
    HashMap<String,Boolean>boo=new HashMap<String,Boolean>();
    boo.put("attend",true);
    DAMN.add(boo);'

On my onCreate, I include both SimpleAdapter for adapter ( String,String) and (String, Boolean)

SimpleAdapter adapter=new SimpleAdapter( this,OMG,R.layout.tolist_row, new String[]{"Student_name","Student_ID"}, new int[] {R.id.StudenName,R.id.studentID}); SimpleAdapter adapter1=new SimpleAdapter(this,DAMN,R.layout.tolist_row1, new String[] {"attend"}, new int[] {R.id.attend});

    listViewItem();
    setListAdapter(adapter);
    setListAdapter(adapter1);

The problem I can only get is my checkbox checked or unchecked but not the ID and name. Unless I comment //setListAdapter(adapter1) only I can get my Student_name and Student_name but without the checkbox ticked. How do I have both result ?

解决方案

I cannot give you the whole code but for a start, do something like this:

First create DataHolder for your data i.e: Strings and Booeleans etc. idea if to use a composite custom object as data holder instead of raw Strings and booleans.

public class DataHolder{
    private String studentNname;
    private Boolean attended;

    //add getters and setters etc.

}

Now implement your adapter like this.

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;
    } 

}

Finally create an instance of this adapter, provide it the data i.e: List and pass this instance to setAdapter of listView.

这篇关于自定义的ListView子项的复选框[&的Hashmap LT;字符串,布尔&GT;]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆