2011-10-21 6 views
0

私はチェックボックスを含むリストビューを作成しています。 私はコードを参照していますが、一部を理解できません。はlistviewのcursoradapterとcheckboxについて理解できません

refferenceのADDR:以下のコードでAndroid: CursorAdapter, ListView and CheckBox

、THERは、2つのチェックボックスです。 1.最終チェックボックスcBox =(CheckBox)inView.findViewById(R.id.bcheck); 2. CheckBox cb =(CheckBox)v.findViewById(R.id.your_checkbox_id);

私はインフレータのレイアウトから得ている最初のものを理解しています。 しかし、2番目のものは、チェックボックスID(R.id.your_checkbox_id)がどこから来るのかわかりません。 誰かが私に理解を助けることができますか?

ありがとうございました!

public class MyDataAdapter extends SimpleCursorAdapter { 
    private Cursor c; 
    private Context context; 
    private ArrayList<String> list = new ArrayList<String>(); 
    private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>(); 

    // itemChecked will store the position of the checked items. 

    public MyDataAdapter(Context context, int layout, Cursor c, String[] from, 
     int[] to) { 
      super(context, layout, c, from, to); 
      this.c = c; 
      this.context = context; 

      for (int i = 0; i < this.getCount(); i++) { 
       itemChecked.add(i, false); // initializes all items value with false 
      } 
    } 

    public View getView(final int pos, View inView, ViewGroup parent) { 

     if (inView == null) { 
      LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inView = inflater.inflate(R.layout.your_layout_file, null); 
     } 

     final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck); // your 
     // CheckBox 
     cBox.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       CheckBox cb = (CheckBox) v.findViewById(R.id.your_checkbox_id); //what is this checkbox? 
       if(cb.isChecked()) { 
        itemChecked.set(pos, true); 
        // do some operations here 
       } 
       else if (!cb.isChecked()) { 
        itemChecked.set(pos, false); 
        // do some operations here 
       } 
      } 
     } 
    } 

答えて

1

は、私はそれだけであなたが直接、以下のように試すことができ、checked.Itは、私が思うあなたがclicked.Butチェックボックスの正しい状態(オンまたはオフ)を保存するために使用されているチェックボックスを知ることで、だと思います。 (私が思う、私はそれを試していないが、私は動作するはずです!)

をそれが好き使用してみてください:

public View getView(final int pos, View inView, ViewGroup parent) { 

     if (inView == null) { 
      LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inView = inflater.inflate(R.layout.your_layout_file, null); 
     } 

     final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck); // your 
     // CheckBox 
     cBox.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       if(cBox.isChecked()) { 
        itemChecked.set(pos, true); 
        // do some operations here 
       } 
       else{ 
        itemChecked.set(pos, false); 
        // do some operations here 
       } 
      } 
     } 
    } 
+1

私は実際に...そうではない正確にgetViewメソッド..しかしnewViewとbindViewカスタムcusorアダプタを使用しています。私は上記のコードをbindViewに入れました。しかし、私は下にスクロールすると、そのチェックボックスが消えているのを感じています... – beginners

+0

なぜこれをgetViewに入れませんでしたか?それだけでなければなりません。 – Hiral

+0

oh。私はそれもbindViewで動作すると思った。私はむしろgetViewで記録したいと思います。ありがとうございます – beginners

関連する問題