从android中的自定义适配器获取复选框的ID?

时间:2013-12-23 12:09:13

标签: java android arrays checkbox android-arrayadapter

我有一个Custom ArrayList,如下所示。

public class sendivitesadapter extends ArrayAdapter<Item>{
    private Context context;
    private ArrayList<Item> items;
    private qrusers qrusers;
    private LayoutInflater vi;


    public sendivitesadapter(Context context,ArrayList<Item> items) {
        super(context, 0,items);

        this.context= context;
        this.qrusers =(qrusers) context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return super.getCount();
    }

    @Override
    public Item getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            final Item i = items.get(position);

            if (i != null) {
                if(i.isSection()){
                    SectionItem si = (SectionItem)i;
                    v = vi.inflate(R.layout.checkboxlist, null);

                    v.setOnClickListener(null);
                    v.setOnLongClickListener(null);
                    v.setLongClickable(false);

                    final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                    sectionView.setText(si.getTitle());

                }else{
                    sendItem ei = (sendItem)i;
                    v = vi.inflate(R.layout.checkboxlist, null);
                    final TextView title = (TextView)v.findViewById(R.id.contactname);
                    final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
                    final CheckBox checkBox=(CheckBox)v.findViewById(R.id.checboxlist);


                    if (title != null) 
                        title.setText(ei.contactname);
                    if(subtitle != null)
                        subtitle.setText(ei.companyname);

                }
            }
            return v;
        }

它看起来像是图像。

enter image description here

我的java文件如下。

@Override
    protected void onPostExecute(String result) {
        JSONArray jarray;

        try {
            jarray= new JSONArray(result);

            name= new String[jarray.length()];
            company=new String[jarray.length()];
            for (int i=0;i<jarray.length();i++){



                JSONObject jobj = jarray.getJSONObject(i);
                name[i]=    jobj.getString("Name");
                company[i]=jobj.getString("Company");

                items.add(new sendItem(name[i], company[i], checkBox));

                adapter  = new sendivitesadapter(qrusers.this,items);
                listView.setAdapter(adapter);

现在我从webservice获取名称,我将它放在listview中,如上所示。 每个名字都有一个USerID。所以我的问题是每当用户以任何顺序检查复选框并单击添加用户时我想要数组中已选中复选框的UserID。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

听起来它是View.setTag()的好候选人。您可以将每个CheckBox上的标记设置为用户的ID [创建时,或指定名称和公司值]。然后,在OnClickOnChecked类型的事件中,您可以调用view.getTag()来检索当前复选框的ID。

答案 1 :(得分:0)

您需要使用OnCheckedChangeListener来获取已检查的CheckBox ID。这个SO会帮助你 - How to handle onCheckedChangeListener for a RadioGroup in a custom ListView adapter。您需要根据需要修改onCheckedChangeListener。

答案 2 :(得分:0)

在适配器中设置复选框中的位置,如

checkBox.setTag(position);

而且我认为你必须在点击添加用户按钮时添加选中的用户。所以点击那个按钮写下面的代码。

public void onClick(View v) {
    // TODO Auto-generated method stub
    String categoryArray = "";
    String[] categoryId;
    if(v == AddUser){
        int count = 0;

        for(int i = 0; i < listViewRightSlideMenu.getChildCount(); i ++){
            RelativeLayout relativeLayout = (RelativeLayout)listViewRightSlideMenu.getChildAt(i);
            CheckBox ch = (CheckBox) relativeLayout.findViewById(R.id.checkBoxCategory); //use same id of check box which you used in adapter
            if(ch.isChecked()){
                count++;
                categoryArray = categoryArray+ch.getTag()+",";

            }
        }
        if(categoryArray.length() > 0) {
        categoryArray = categoryArray.substring(0, categoryArray.length() - 1);
        String[] array = categoryArray.split(",");
        categoryId = new String[array.length];
        for(int i = 0; i< array.length; i++) {
            categoryId[i] = listCategory.get(Integer.valueOf(array[i])).getId();
        }

        for(int i = 0; i < categoryId.length; i++){
            String a = categoryId[i];
            System.out.println("category id is: "+a);
        }
        System.out.println("array position: "+categoryId);

    }
}