繁体   English   中英

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

[英]Get the ID of checkbox from custom adapter in android?

我有一个自定义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;
        }

它看起来像下面的图像。

在此处输入图片说明

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

现在,我从网络服务中获取名称,该名称正如上所述在列表视图中显示。 每个名字我都会得到一个USerID。 所以我的问题是,每当用户以任何顺序选中复选框并单击添加用户时,我都希望数组中选中的复选框的UserID。 我该如何实现?

听起来这是View.setTag()的不错的View.setTag() 您可以将每个CheckBox上的标签设置为用户的ID(在创建标签时,或分配名称和公司值)。 然后,在OnClickOnChecked类型事件中,可以调用view.getTag()来检索当前复选框的ID。

您需要使用OnCheckedChangeListener来获取经检查的CheckBox ID。 该SO将为您提供帮助- 如何在自定义ListView适配器中为RadioGroup处理onCheckedChangeListener 您需要根据需要修改onCheckedChangeListener。

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

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

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM