2013-12-23 45 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