0

我正在制作一个包含复选框的列表视图。我指的是代码并尝试遵循,但无法理解其中的一部分。

参考地址:Android:CursorAdapter、ListView 和 CheckBox

在下面的代码中,有两个 CheckBox。1. final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck); 2. CheckBox cb = (CheckBox) v.findViewById(R.id.your_checkbox_id);

我理解第一个,它来自充气机布局。但第二个,我不明白 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 回答