user2699728
user2699728

Reputation: 377

Get the ID of checkbox from custom adapter in android?

I have a Custom ArrayList as follows.

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

and it looks like following image.

enter image description here

My java file is as follows.

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

Now I get the names from webservice which I am diplaying it in a listview as shown above. With every name I get a USerID. So my question is whenever the user checks the checkbox in any sequence and click on add user I want the UserID of the checked checkboxes in array. How can I achieve this?

Upvotes: 0

Views: 580

Answers (3)

Matt
Matt

Reputation: 3847

Sounds like it's a good candidate for View.setTag(). You could set the tag on each CheckBox to the id of the user [when you create it, or assign the Name and Company values]. Then in an OnClick or OnChecked type event, you can call view.getTag() to retrieve the id of the currently checked box.

Upvotes: 1

Avinash Kumar Pankaj
Avinash Kumar Pankaj

Reputation: 1720

In your adapter set the position in check box like

checkBox.setTag(position);

And as i think you have to add checked user on click of Add User button. So on click of that button write following code.

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

    }
}

Upvotes: 0

Pradip
Pradip

Reputation: 3177

You need to use OnCheckedChangeListener to get the cheched CheckBox ID. This SO will help you- How to handle onCheckedChangeListener for a RadioGroup in a custom ListView adapter . You need to modify the onCheckedChangeListener according to your need.

Upvotes: 0

Related Questions