简体   繁体   中英

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. I have used CustomAdapter class extending Baseadapter and using custom layout. By this way you can set OnClicklistners for every view individually(check box,button,total layout... ).

The easiest way to do it in OnItemClickListener is this:

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

You can then use the c.getLong(0); to get the id (assuming you fetched the id column as the first column which is generally the case).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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