【问题标题】:can't understand about cursoradapter and checkbox in listview无法理解 listview 中的 cursoradapter 和 checkbox
【发布时间】:2011-12-12 07:02:47
【问题描述】:

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

参考地址:Android: CursorAdapter, ListView and 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 
              }
          }
      }
   }

【问题讨论】:

    标签: android listview checkbox android-cursoradapter


    【解决方案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 
                  }
              }
          }
       }
    

    【讨论】:

    • 我实际上使用自定义 cusor 适配器.. 所以不完全是 getView.. 但 newView 和 bindView.. 我把上面的代码放在 bindView 中。但是当我向下和向后滚动时,我遇到该复选框正在消失...
    • 但是你为什么不把这个放到getView中呢?它应该只在那里。
    • 哦。我认为它也可以在 bindView 中工作。我宁愿用 getView 重新编码。谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    相关资源
    最近更新 更多