如何从字符串数组android中设置标签复选框?

时间:2013-12-27 06:51:58

标签: android arrays checkbox android-arrayadapter

我有一个活动,在同一个活动中,我有一个带复选框的数组适配器类。 如您所见,我从webservice获取数据。因此,在解析json数据后,我得到字符串数组uid []中的userID。现在我有sendinvitesadapter中的复选框。我必须使用字符串数组uid []设置标签复选框。因此,当用户点击第一个复选框时,它应该从uid []中分配第一个值,依此类推...... 我怎样才能做到这一点。我知道可以使用set tag和get标签完成。但我面临着错误。它只从uid []获取最后一个值,并且同样被赋予所有复选框。

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

2 个答案:

答案 0 :(得分: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]);
          }
});

答案 1 :(得分: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;
    }




   }