0

チェックボックスを含むリストビューを作成しています。コードを参照してフォローしようとしていますが、一部を理解できません。

参照アドレス: Android: CursorAdapter、ListView、および CheckBox

以下のコードでは、2 つの CheckBox があります。1.最終チェックボックス cBox = (CheckBox) inView.findViewById(R.id.bcheck); 2. CheckBox cb = (CheckBox) v.findViewById(R.id.your_checkbox_id);

インフレータ レイアウトから取得している最初の 1 つを理解しています。しかし、2番目に、CheckBox 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 
              }
          }
      }
   }
4

1 に答える 1

1

どのチェックボックスがチェックされているかを知るためだけだと思います.クリックしたチェックボックスの正しいステータス(チェックされているかどうか)を保存するために使用されます.しかし、私は、以下のように直接試すことができると思います.でも私は働くべきだと思います!)

次のように使用してみてください。

  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 
              }
          }
      }
   }
于 2011-10-21T04:26:26.907 に答える