简体   繁体   English

Android光标适配器获取复选框和ListView的数据

[英]Android cursor adapter get checkbox and data of listview

I have this code that used cursor adapter: 我有使用游标适配器的以下代码:

   public class TemplateActivity extends Activity {

        Button btnSort, btnDel;

        private ListViewAdapter listAdapter;
        private RetailerDatabaseHelper dbHelper;
        private ListView listView;

        private static final String TAG = TemplateActivity.class.getSimpleName();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_template);

            btnSort = (Button) findViewById(R.id.btnSort);
            btnDel = (Button) findViewById(R.id.btnDelete);

            dbHelper = new RetailerDatabaseHelper(this);
            listView = (ListView) findViewById(R.id.listViewData);

            listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d(TAG, "clicked on item: " + position);


                }
            });

            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    listAdapter = new ListViewAdapter(TemplateActivity.this, dbHelper.getAllData());
                    listView.setAdapter(listAdapter);
                }
            });

my problem is that how can i get the value of the data from the listview when a data is clicked. 我的问题是单击数据时如何从列表视图中获取数据的值。

Here is my adapter for this: 这是我的适配器:

public class ListViewAdapter extends CursorAdapter {

    public ListViewAdapter (Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // when the view will be created for first time,
        // we need to tell the adapters, how each item will look
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.custom_dialog_box, parent, false);

        return retView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // here we are setting our data
        // that means, take the data from the cursor and put it in views

        TextView textViewPersonName = (TextView) view.findViewById(R.id.checkBox1);
        textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

        TextView textViewPersonPIN = (TextView) view.findViewById(R.id.number);
        textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
    }
}

another thing. 另一件事。 how will i implement here that when i checked a checkbox and press a button it will get the data of the checkbox? 我将如何在这里实现,当我选中一个复选框并按下一个按钮时,它将获取该复选框的数据? thank you all for the help. 谢谢大家的帮助。

I haven't worked on ListViewAdapter but i have the same scenario as yours. 我没有在ListViewAdapter上工作,但与您的情况相同。 I have used CustomAdapter class extending Baseadapter and using custom layout. 我使用了CustomAdapter类扩展Baseadapter并使用自定义布局。 By this way you can set OnClicklistners for every view individually(check box,button,total layout... ). 通过这种方式,您可以分别为每个视图(复选框,按钮,总布局...)设置OnClicklistners。

The easiest way to do it in OnItemClickListener is this: OnItemClickListener最简单的方法是:

Cursor c = ((ListViewAdapter)l.getAdapter()).getCursor();
c.moveToPosition(position);

You can then use the c.getLong(0); 然后可以使用c.getLong(0); to get the id (assuming you fetched the id column as the first column which is generally the case). 获取ID(假设您通常将id列作为第一列获取)。

Second question :(Get the checked items list) : 第二个问题:(获取检查项目清单)
You can write a setOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener) for the Checkbox in your bindView() method of your Adapter , then if item is checked then add the item to the list, if it is unchecked then remove the item from the list. 你可以写一个setOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)CheckboxbindView()你的方法Adapter如果项目被选中,那么该项目添加到列表中,如果它没有被选中,然后从列表中删除的项目,然后。

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

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