无法理解列表视图中的游标适配器和复选框

发布于 2024-12-11 03:57:37 字数 2181 浏览 1 评论 0原文

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

参考地址:Android: CursorAdapter, ListView and CheckBox

在下面的代码中,有两个复选框。 1. 最终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 
              }
          }
      }
   }

i'm making a listview containing checkbox.
i'm referring a code and try to fowllow, but can't understand one part.

refference addr : Android: CursorAdapter, ListView and CheckBox

in the below code, ther are two CheckBoxes.
1. final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck);
2. CheckBox cb = (CheckBox) v.findViewById(R.id.your_checkbox_id);

i understand first one, which is getting from inflater layout.
but second one, i dont understand where the CheckBox id(R.id.your_checkbox_id) comes from..
could anybody help me to understand?

thanks!

 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 
              }
          }
      }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧故 2024-12-18 03:57:37

我认为,这只是为了知道哪个复选框被选中。它用于保存您单击的复选框的正确状态(选中或未选中)。但我认为,您可以直接尝试如下。(我没有尝试过)但我认为我应该工作!)

尝试使用它,如:

  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 
              }
          }
      }
   }

I think,it is just to know which checkbox is checked.It is used to save the correct status(checked or unchecked) of the checkbox you clicked.But i think,you can directly try as below.(I didn't try it but i should work,i think!)

Try using it like:

  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 
              }
          }
      }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文