简体   繁体   English

具有自定义列表视图的复选框

[英]Checkbox with Custom listview

I am using cursoradapter to get the values from database and to display it in listview.I am able to show the database contents in listview also onclick of perticular item I am getting value of clicked Item.Now I want checkbox also so that oncheck I should get the values of checked Items ( In this case both checkbox click and listitemclick both should work) is it possible? 我正在使用cursoradapter从数据库中获取值并将其显示在listview中。我能够在listview中显示数据库内容,同时也显示perticular项的onclick我获得了被单击项的值。现在我还想要复选框,以便oncheck我应该获取选中项目的值(在这种情况下,复选框click和listitemclick都应该起作用)是否可以? how to do it? 怎么做?

private void displayListView() {
        final 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.rownew,
                cursor,
                columns,    
                to);
        View v = getLayoutInflater().inflate(R.layout.customdialog, null);
        ListView listView = (ListView) v.findViewById(R.id.listChildren);
        final EditText etChild = (EditText) v.findViewById(R.id.etChild);
        listView.setAdapter(dataAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                name = ((TextView) view.findViewById(R.id.name)) .getText().toString();
                BirtDate = ((TextView) view.findViewById(R.id.birthdate)) .getText().toString();
                Log.d("*************", name);

                Info=name+ " " +BirtDate;
                Log.d("nameeeeeeeeeeeeeeee",Info);
                etChild.setText(new StringBuilder().append(Info));
                topaste=etChild.getText().toString();
                    etChild.setText(new StringBuilder().append(Info1));
                //                  topaste1=etChild.getText().toString();
                //              }


            }
        })

My rownew.xml 我的rownew.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#FFFFFF"
    android:gravity="center"
    android:padding="6dp" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/birthdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="@string/text"
        android:textStyle="bold" >
    </TextView>


</LinearLayout>

My Customdialog.xml 我的Customdialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/etChild"
         android:hint=""
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
    </EditText>

    <ListView
        android:id="@+id/listChildren"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" >
    </ListView>
  </LinearLayout>

You could extend SimpleCursorAdapter to add a click listener for your checkbox. 您可以扩展SimpleCursorAdapter来为复选框添加点击侦听器。 Here is a snippet of the code I use: 这是我使用的代码片段:

public class MultiSelectCursorAdapter extends SimpleCursorAdapter {

  public MultiSelectCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final int fposition = position;
    View view = super.getView(position, convertView, parent);
    CheckBox checkbox = (CheckBox) view.findViewById(R.id.list_checkbox);
    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d("CHECKER", "Something happened: " + fposition + " was clicked " + isChecked);
            // TODO: store id of checked items in your model
            }

    });

    return view;
    }
}

In your list row layout you need to set android:focusable="false" in order to allow clicks on list items and clicks on the checkbox of each list item. 在列表行布局中,您需要设置android:focusable="false"以便允许单击列表项以及单击每个列表项的复选框。

<CheckBox
        android:id="@+id/list_checkbox"
        android:focusable="false" 
        ... >
</CheckBox>

Finally you need a model or data structure to store all checked items of your list, eg an array or list that stores identifiers for all checked items. 最后,您需要一个模型或数据结构来存储列表中所有选中的项,例如,存储所有选中项的标识符的数组或列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM