2013-12-23 80 views
0

我有一个自定义ArrayList如下。从android中的自定义适配器获取复选框的ID?

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

现在我从web服务,我感到diplaying它在ListView如上图所示名称。 随着每个名字我得到一个USEID。所以我的问题是,每当用户检查任何顺序的复选框,并点击添加用户,我希望在数组中检查复选框的用户名。我怎样才能做到这一点?

回答

1

听起来像是View.setTag()的一个好选择。您可以将每个CheckBox上的标签设置为用户的ID(当您创建该标签或指定名称和公司值时)。然后,在OnClick或OnChecked类型事件中,您可以调用view.getTag()来检索当前选中框的ID。

+0

你能告诉我代码吗?我无法做到。 – user2699728

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

    } 
} 
+0

什么是listViewRightSlideMenu? – user2699728

+0

listview其中包含复选框 –

+0

和什么是listCategory这一步? – user2699728