mercredi 25 mai 2016

Get selected item from listView using custom CursorAdapter and checkbox

I have a listView (4 columns and checkbox) that fills by a custom CursorAdapter from my database:

public class NotifyAdapter extends CursorAdapter {
public static final String TAG = NotifyAdapter.class.getSimpleName();
NotifyActivity notifyActivity;
ListView listView;
Cursor c;

public NotifyAdapter(Context context, Cursor c, ListView listView, int flags) {
    super(context, c, 0);
    this.listView = listView;
    this.c = c;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_item_5col_notifications_send, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {


    TextView tv_name = (TextView) view.findViewById(R.id.tv_notifications_full_name);
    ImageView iv_sms_notify = (ImageView) view.findViewById(R.id.iv_sms_notify);
    ImageView iv_email_notify = (ImageView) view.findViewById(R.id.iv_email_notify);
    ImageView iv_call_notify = (ImageView) view.findViewById(R.id.iv_call_notify);

    final int id = cursor.getInt(cursor.getColumnIndex(DBHelper.ID_COLUMN));
    String name = cursor.getString(cursor.getColumnIndex(DBHelper.NAME_COLUMN));
    int sms_notify_checked = cursor.getInt(cursor.getColumnIndex(DBHelper.SMS_NOTIFICATION_COLUMN));
    int email_notify_checked = cursor.getInt(cursor.getColumnIndex(DBHelper.EMAIL_NOTIFICATION_COLUMN));
    int call_notify_checked = cursor.getInt(cursor.getColumnIndex(DBHelper.CALL_NOTIFICATION_COLUMN));

    tv_name.setText(name);
    if (sms_notify_checked == 1) {
        iv_sms_notify.setImageResource(R.drawable.ic_check_black_18dp);
    } else iv_sms_notify.setImageResource(R.drawable.ic_close_black_18dp);

    if (email_notify_checked == 1) {
        iv_email_notify.setImageResource(R.drawable.ic_check_black_18dp);
    } else iv_email_notify.setImageResource(R.drawable.ic_close_black_18dp);

    if (call_notify_checked == 1) {
        iv_call_notify.setImageResource(R.drawable.ic_check_black_18dp);
    } else iv_call_notify.setImageResource(R.drawable.ic_close_black_18dp);


    final CheckBox cb_notify = (CheckBox) view.findViewById(R.id.chb_person_notify);
    int position = listView.getPositionForView(cb_notify);
    cb_notify.setTag(position);

    cb_notify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int position = (Integer) cb_notify.getTag();
            c = (Cursor) getItem(position);
            c.getLong(id);

            if (cb_notify.isChecked()) {
                notifyActivity.getCheckedItemsIdArray().add(id);
                Log.d(TAG, "clicked on item: " + id);

            }
        }

    });

And activity with listview:

public class NotifyActivity extends MenuActivity implements View.OnClickListener {

public static final String TAG = NotifyActivity.class.getSimpleName();
ListView listView;
Button btn_send_message;
Button btn_cancel_send_message;
CheckBox cb_notify;
Intent start_respond_activity_intent;
Intent intent;
DBHelper dbHelper;
SQLiteDatabase database;
NotifyAdapter notifyAdapter;
ArrayList checkedItemsIdArray;


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


    getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.white));
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.tv_title_toolbar);
    TextView toolbarTitle_small = (TextView) toolbar.findViewById(R.id.tv_small_title_toolbar);
    toolbarTitle.setText(getRelationshipValue());
    toolbarTitle_small.setText("NOTIFY");
    toolbar.setBackgroundColor(getResources().getColor(R.color.yellow));


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.yellow));
    }
    btn_send_message = (Button) findViewById(R.id.btn_send_message);
    btn_cancel_send_message = (Button) findViewById(R.id.btn_cancel_send_message);

    btn_send_message.setOnClickListener(this);
    btn_cancel_send_message.setOnClickListener(this);

    listView = (ListView) findViewById(R.id.lv_notifications_send);

    dbHelper = new DBHelper(this);
    database = dbHelper.getWritableDatabase();


    Cursor cursor = database.query(DBHelper.TABLE_NAME,
            null,
            DBHelper.RELATIONSHIP_COLUMN + " = ?", new String[]{getRelationshipValue()},
            null, null, null, null);
    notifyAdapter = new NotifyAdapter(this, cursor, listView,0);

    listView.setAdapter(notifyAdapter);

    checkedItemsIdArray = new ArrayList();
    cb_notify = (CheckBox) findViewById(R.id.chb_person_notify);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor c = (Cursor) parent.getAdapter().getItem(position);
            cb_notify.setChecked(true);
            long checkedId = id;
            checkedItemsIdArray.add(checkedId);
            cb_notify.setChecked(true);

            Log.d(TAG, "clicked on item: " + checkedId);

        }
    });

}

@Override
protected void onStart() {
    super.onStart();

    Cursor cursor = database.query(DBHelper.TABLE_NAME,
            null,
            DBHelper.RELATIONSHIP_COLUMN + " = ?", new String[]{getRelationshipValue()},
            null, null, null, null);
    notifyAdapter.changeCursor(cursor);


}

public ArrayList getCheckedItemsIdArray(){
    return checkedItemsIdArray;
}

private String getRelationshipValue() {
    Intent intent = getIntent();
    String relationship_value = intent.getStringExtra("Relationship");
    return relationship_value;
}


@Override
public void onClick(View v) {
    start_respond_activity_intent = new Intent(this, RespondMapActivity.class);
    switch (v.getId()) {
        case R.id.btn_send_message:
            startActivity(start_respond_activity_intent);
        case R.id.btn_cancel_send_message:
            startActivity(start_respond_activity_intent);
    }
}

I need to get the id (according to my database) of all listview's rows that were selected by checkbox or by clicking on these rows. I think I need to store the id in the ArrayList to work with it in the future. But the selection doesn't work in my code. Where are my mistakes?




Aucun commentaire:

Enregistrer un commentaire