2012-07-31 34 views
1

ListView正在讓我瘋狂。我在StackOverflow上搜索了很多關於這方面的信息,但我無法解決這個問題。無法獲取ListView中單擊的CheckBox的相應值

問題是我有一個ListView的自定義適配器,其中我正在充氣CheckBox

下面的代碼:

public class settingadapter extends BaseAdapter{ 

    private Activity recievedactivity; 

    private String[] recieveddescription; 
    private String[] recusername; 
    private String allitems=""; 
    private String[] itemchecked; 

    private static LayoutInflater inflator=null; 


    public settingadapter(Activity recactivity,String[] username, String[] recdescription){ 
     recievedactivity=recactivity; 
     recusername=username; 
     recieveddescription=recdescription; 

     inflator=(LayoutInflater)recievedactivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 



    } 
    public settingadapter(Activity recactivity,String[] username, String[] recdescription ,String all){ 
     recievedactivity=recactivity; 
     recusername=username; 
     recieveddescription=recdescription; 
     allitems=all; 
     inflator=(LayoutInflater)recievedactivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    } 


    public int getCount() { 
     // TODO Auto-generated method stub 
     return recusername.length; 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
    static class ViewHolder{ 
     TextView usernameTextView; 
     TextView statusTextView; 
     CheckBox checkView; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final ViewHolder viewHolder; 

     if(convertView== null) 
     { 
      viewHolder=new ViewHolder(); 

      convertView=inflator.inflate(R.layout.item, null); 

      viewHolder.usernameTextView=(TextView)convertView.findViewById(R.id.textView1); 
      viewHolder.statusTextView=(TextView)convertView.findViewById(R.id.text); 
      viewHolder.checkView=(CheckBox)convertView.findViewById(R.id.checkBox1); 





      convertView.setTag(viewHolder); 


     }else{ 
      viewHolder=(ViewHolder)convertView.getTag(); 
     } 
      viewHolder.usernameTextView.setText(recusername[position]); 
      viewHolder.statusTextView.setText(recieveddescription[position]); 
      //Add listener to the checkbox 


      System.out.println("testing alpha"+recusername[position]); 

      if(allitems.equals("All")){ 
       viewHolder.checkView.setChecked(true); 
      } 
      else if (allitems.equals("---Select User---")){ 
       viewHolder.checkView.setChecked(false); 
      } 
      else 
      return convertView; 
      //image.setBackgroundResource(R.drawable.check); 



      return convertView; 



    } 

} 
在此

現在我怎麼得到它CheckBox被點擊,我怎樣才能得到相應的文字也?

回答

0

請檢查這些代碼

在ANS的人已加入onclick事件和獲取標籤

這裏。 Android how to identify item in listview with checkbox

的onclick不相同的代碼複選框 http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

希望它會幫助你:)

+0

感謝ü這麼多阿維納什·薩胡...這是一個真正的幫助代碼雖然米無法投票這個答案低聲譽... thnk你soooooooooooooooooooooooo非常多....在最後的android國王的岩石.. – moni 2012-08-06 06:38:56

+0

@Moni:你歡迎:) – 2012-08-06 07:02:51