2012-02-28 62 views
1

在我的情況下,我有兩個文本視圖和一個複選框在列表視圖。我正在使用ArrayAdapter類來設置textviews中的數據。Android的一覽表與複選框

當我選擇任何複選框並在列表視圖中滾動時,某些複選框會自動選中。我不明白爲什麼下面發生這種情況是我的代碼,請查看代碼並給出解決方案以解決此問題。

public class Contacts_NumberActivity extends Activity { 
    /** Called when the activity is first created. */ 
    String nameList[] = { "U", "A", "B", "C", "D", "E", "F", "G", "H", 
      "I", "J", "L", "M", "N", "O", "P", "Q" }, numberList[] = { "1", 
      "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", 
      "5", "6", "7" }; 
    ListView contactList; 
    boolean[] checked; 
    ContactHolder holder = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     contactList = (ListView) findViewById(R.id.contact_list); 

     contactList.setAdapter(new ContactAdapter(this, R.layout.list_item, 
       nameList, numberList)); 

    } 

    public class ContactAdapter extends ArrayAdapter<String> { 

     Context context; 
     int layoutResourceId; 
     String data[] = null; 
     String data1[] = null; 

     public ContactAdapter(Context context, int layoutResourceId, 
       String[] data, String[] data1) { 
      super(context, layoutResourceId, data); 
      this.layoutResourceId = layoutResourceId; 
      this.context = context; 
      this.data = data; 
      this.data1 = data1; 
     } 

     @Override 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      View row = convertView; 

      if (row == null) { 
       LayoutInflater inflater = ((Activity) context) 
         .getLayoutInflater(); 
       row = inflater.inflate(layoutResourceId, parent, false); 

       holder = new ContactHolder(); 
       holder.name = (TextView) row.findViewById(R.id.name); 
       holder.number = (TextView) row.findViewById(R.id.number); 
       holder.chkbox = (CheckBox) row.findViewById(R.id.checkBox1); 

       holder.chkbox 
         .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

          @Override 
          public void onCheckedChanged(
            CompoundButton buttonView, boolean isChecked) { 

           holder.chkbox.setChecked(isChecked); 
           System.out.println("Checked possition= " 
             + isChecked); 
          } 

         }); 

       row.setTag(holder); 
      } else { 
       holder = (ContactHolder) row.getTag(); 
      } 

      holder.name.setText(data[position]); 
      holder.number.setText(data1[position]); 

      return row; 
     } 

     public class ContactHolder { 
      TextView name, number; 
      CheckBox chkbox; 
     } 
    } 
} 
+0

我想打印你的listview.please問題檢查你的listview它可能有數據重複,這就是爲什麼你感覺自動複選框得到選擇.check並讓我知道, – OnkarDhane 2012-02-28 07:04:33

+0

這是發生在我單擊任何複選框並滾動列表時,下面的複選框也被選中。 – user03 2012-02-28 08:59:18

+0

檢查下面的鏈接它可以幫助你http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php http://developmentality.wordpress.com/2010/11/05/of-rubber -stamps-and-checkboxes-why-your-listview-is-broken/ – 2012-04-17 13:27:14

回答

0

你的代碼有沒有問題,但問題是,因爲ArrayAdapter的getView功能 這是在地呼籲,要重新初始化和複用表的聚焦行爲您正在使用轉換,這是造成分頁的視圖檢查如果在屏幕上,你先檢查複選框,然後在頁面的所有屏幕獲得第一個複選框選中,

得到這個,你應該使用的,而不是在這裏使用的適配器列表視圖佈局解決的是一個提示可以幫助你

LinearLayout parent = new LinearLayout(this); 
parent.setOrientation(VERTICAL); 
parent.setLayoutParams(new LayoutParams(list_width,list_height); 

for(number of rows in your list) 
{ 
LinearLayout row= new LinearLayout(this); 
row.setOrientation(Horizontal); 
row.setLayoutParams(new LayoutParams(row_width,row_height); 

TextView text= new TextView (this); 
text.setText("your text"); 
text.setLayoutParams(new LayoutParams(text_width,text_height); 
row.addView(text); 
. 
.//other view you want to add as check box etc 
. 
. 
parent.AddView(row); 
} 

this.addView(parent); 

hope this will help you 

注意:但如果你有大量的行,它可能會導致內存問題