自定义ListView Subitem复选框[Hashmap <string,boolean>] </string,boolean>

时间:2014-12-07 08:42:24

标签: android checkbox android-listview simpleadapter

链接:ListView MyExample

所以我按照上面的教程链接使用:

添加子项
  

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

为子项添加字符串。

现在,我想在现有的String中添加一个布尔复选框,我使用

  

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

我的私有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);'

在我的onCreate上,我包括适配器的SimpleAdapter(String,String)和(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);

我只能检查或取消选中我的复选框,而不是ID和名称。 除非我评论 //setListAdapter(adapter1)只有我可以获取我的Student_name和Student_name但没有勾选复选框。 我如何得到这两个结果?

1 个答案:

答案 0 :(得分:-1)

我不能给你整个代码,但首先,做一些这样的事情:

首先为您的数据创建DataHolder,即:字符串和Booeleans等。想知道是否使用复合自定义对象作为数据持有者而不是原始字符串和布尔值。

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

}

最后创建一个这个适配器的实例,为它提供数据,即:List并将此实例传递给listView的setAdapter。