0

I am having a activity and in same activity I have a array adapter class with checkbox. As you can see I get the data from webservice. So after parsing the json data I get the userIDs in string array uid[]. Now I have the checkbox in sendinvitesadapter. I have to set tag the checkbox with string array uid[]. So that when user clicks on first checkbox it should get assigned with first value from uid[] and so on... How Can I do this. I know it can be done using set tag and get tag. but I am facing errors. Its only taking the last value from uid[] and same is assinged to all of the checkboxes.

public class qrusers extends ActivityGroup{
    EditText usersearch;
    ArrayList<Item> items = new ArrayList<Item>();
    ListView listView;
    static InputStream is = null;
    final ArrayList<String> list = new ArrayList<String>();
    CheckBox checkBox;
    String result;

    Button addusers;
    private String[] name,email,phone,date,company,combine;
    public static String[] uid;
    CheckBox qrcheckBox;
    sendivitesadapter sendadapter;
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

    }
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.qrusers);
    usersearch=(EditText)findViewById(R.id.usersearch);
    listView=(ListView)findViewById(R.id.qruserlist);
    addusers=(Button)findViewById(R.id.addusers);


    addusers.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


        }

    });

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()){

        saveThread saveTask= new saveThread();
        saveTask.execute(new String[] { "url"});
    }

}
public class sendivitesadapter extends ArrayAdapter<Item>{
    private Context context;
    private ArrayList<Item> items;
    private qrusers qrusers;
    private LayoutInflater vi;
    private String[] array;
    qrusers qrus;

    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(final 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);
                     checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
                    //checkBox.setTag(position);
                    //items.addAll(uid);


                //    Log.e("IDDDDDDD", text);
                    //checkBox.setTag("12");
                    checkBox.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String s= (String)v.getTag();
                            Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
                        }
                    });
                    if (title != null) 
                        title.setText(ei.contactname);
                    if(subtitle != null)
                        subtitle.setText(ei.companyname);

                }
            }
            return v;
        }




}










public void replaceContentView(String id, Intent newIntent) {
    View view = getLocalActivityManager().startActivity(id,
            newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();
    this.setContentView(view);
}   
class saveThread extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... urls) {
        Context context;
        String response = "";

        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse response1;

            try {
                response1 = client.execute(httpGet);
                HttpEntity entity = response1.getEntity();

                is = entity.getContent();

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"), 8);

                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                result = sb.toString();
                System.out.println(result);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
        return result;

}

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

        try {
            jarray= new JSONArray(result);

            name= new String[jarray.length()];
            company=new String[jarray.length()];
            uid=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");
                uid[i]=jobj.getString("UserID");
                System.out.println(uid[i]);



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

                sendadapter  = new sendivitesadapter(qrusers.this,items);
                listView.setAdapter(sendadapter);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onPostExecute(result);
    }
}
}
9
  • is ur all ids are unique ? Commented Dec 27, 2013 at 6:54
  • Yes ...I get the id from webservice. Commented Dec 27, 2013 at 6:56
  • Try using set id for checkboxes, get ids to get values Commented Dec 27, 2013 at 6:57
  • I cant , bcoz its a string array not int. Commented Dec 27, 2013 at 6:59
  • What u want set whole array to one check box Commented Dec 27, 2013 at 7:01

2 Answers 2

1
// try to changed this peace of code 
1. pass uid array to adapter
@Override
protected void onPostExecute(String result) {
            JSONArray jarray;

            try {
                jarray= new JSONArray(result);

                name= new String[jarray.length()];
                company=new String[jarray.length()];
                uid=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");
                    uid[i]=jobj.getString("UserID");
                    System.out.println(uid[i]);



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

                }
                sendadapter  = new sendivitesadapter(qrusers.this,items,uid);
                listView.setAdapter(sendadapter);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            super.onPostExecute(result);
}

2.assign udid array as local array
public sendivitesadapter(Context context,ArrayList<Item> items,String[] uIds) {
            super(context, 0,items);

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

}
3.use uid loacl array
checkBox.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
              Log.e("IDDDDDDDDDDDDDDDDDDDDDD", uIds[position]);
          }
});
Sign up to request clarification or add additional context in comments.

3 Comments

one more question, Now can I can store the checked values of checkbox in a string array?
Actually I want to send the values to webservice, so if two of the checkboxes are selected it should send the id to webservice
oky declare one hashmap in adapter and give key as id and value as boolean value true or false intial hashmap have false value in each key afterword when checkbox check change reflection on each id like checkbox checked then put value as true other wise false...
0
public class sendivitesadapter extends ArrayAdapter<Item>{
private Context context;
private ArrayList<Item> items;
private qrusers qrusers;
private LayoutInflater vi;
private String[] array;
qrusers qrus;

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(final 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);
                 checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
            //////////////////////here use the setTag() method////////////////////
                checkBox.setTag(uid[position]);
             ///////////////////////////
                checkBox.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String s= (String)v.getTag();
                        Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
                    }
                });
                if (title != null) 
                    title.setText(ei.contactname);
                if(subtitle != null)
                    subtitle.setText(ei.companyname);

            }
        }
        return v;
    }




   }

1 Comment

I want to set tag the uid[] to settag method. So how to set tag instead of position?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.