【问题标题】:To implemennt the checkbox for customized listview实现自定义列表视图的复选框
【发布时间】:2012-10-31 17:51:25
【问题描述】:

我已经编写了代码以从数据库中获取值并将该值绑定到列表视图。为此,我现在根据要求使用了自定义列表视图,我想要列表中每个项目的复选框。如何做到这一点

image=(ImageView)findViewById(R.id.image);
        note=(ImageButton)findViewById(R.id.note);
        tick=(ImageButton)findViewById(R.id.tick);
        cross=(ImageButton)findViewById(R.id.cross);
        Intent intent = getIntent();
        Bitmap photo = (Bitmap) intent.getParcelableExtra("photooo");
        image.setImageBitmap(photo);
        if(photo!=null)
        {
            dbHelper = new RecordsDbAdapter(this);
            dbHelper.open();
            displayListView();
        }

    }
    private void displayListView() {
        Cursor cursor = dbHelper.fetchAllRecords();
        String[] columns = new String[] {
                RecordsDbAdapter.KEY_NAME,
                RecordsDbAdapter.KEY_BIRTHDAY,

        };
        int[] to = new int[] {
                R.id.name,
                R.id.birthdate,
        };
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.row,
                cursor,
                columns,
                to);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(dataAdapter);
    }
}

【问题讨论】:

    标签: android database listview checkbox


    【解决方案1】:
    • 使用 LayoutInflater 将复选框添加到列表视图
    • 使用 SetTag 方法后将 Tagvalue 添加到复选框
    • 列表视图中每个项目中的此标记值单独复选框 以下代码示例:`

    LayoutInflater inflater = getLayoutInflater();

     convertView = inflater.inflate(R.layout.home1, null);
    

    ViewHolder holder1 = new ViewHolder(); holder1.text = (TextView) convertView.findViewById(R.id.textView1);

       holder1.ch=(CheckBox)convertView.findViewById(R.id.checkBox1);
      holder1.ch .setTag(position);
    

    【讨论】:

      【解决方案2】:

      就像你之前所做的那样,转到 R.layout.row 的 XML 文件并输入以下代码:

      <CheckBox
          android:id="@+id/checkbox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:text="checkbox" />
      

      然后像这样编辑您的代码:--

          private void displayListView() {
          Cursor cursor = dbHelper.fetchAllRecords();
          String[] columns = new String[] {
                  RecordsDbAdapter.KEY_NAME,
                  RecordsDbAdapter.KEY_BIRTHDAY,
                  RecordsDbAdapter.KEY_CHECKBOX
      
          };
          int[] to = new int[] {
                  R.id.name,
                  R.id.birthdate,
                  R.id.checkbox
          };
          dataAdapter = new SimpleCursorAdapter(
                  this, R.layout.row,
                  cursor,
                  columns,
                  to);
          ListView listView = (ListView) findViewById(R.id.list);
          listView.setAdapter(dataAdapter);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多