Search code examples
androidlistviewsimplecursoradapter

android listview row id mapped to CursorAdapter row within Anonymous inner class


I have a SimpleCursorAdapter class, it gets various data from DB and displays in ListView. It implements an onclicklistener that sends a messageGuid String to another activity. All this works fine.

In the row of the ListView i have added a CheckBox and set an onCheckChangeListener to it. At the moment when the checkbox is checked, the messageGuid is always the last one in the cursor.

I need to find a way to get the listview row id of the row which hold the checkbox that has been checked. I can then get the correct cursor row and then in turn the correct messageGuid.

I've commented what i would like within the onCheckedChanged method.

Thanks in advance Matt.

private class MyAdapter extends SimpleCursorAdapter implements OnItemClickListener {


    Cursor c;
    String messageGuid;


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


        }

        @Override
        public
        View getView(int position, View convertView, ViewGroup parent) {
            Log.e(TAG, "inside myadapter getview for messages");
            View v = super.getView(position, convertView, parent);
            if(v == null)
                return null;

            c = (Cursor)getItem(position);

            Log.e(TAG, "(Cursor)getItem(position) = " + c + "position = " + position);

            v.setTag(c);


            //other code removed, not relevant



            String messageSender = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_SENDER));
            String isRepliedTo = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_REPLIED));
            String isStandAlone = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_IS_STANDALONE));




            ((TextView)v.findViewById(R.id.messagecreatedat)).setText(formattedMessCreatedAt );
            ((TextView)v.findViewById(R.id.messagetext)).setText(messageText);
            ((TextView)v.findViewById(R.id.messagesender)).setText(messageSender);

            //#003F87 = blue

            ((TextView)v.findViewById(R.id.messagecreatedat)).setTextColor(Color.parseColor("#003F87"));
            ((TextView)v.findViewById(R.id.messagesender)).setTextColor(Color.parseColor("#003F87"));
            ((TextView)v.findViewById(R.id.messagetext)).setTextColor(Color.parseColor("#FF0000"));




            CheckBox cb = ((CheckBox)v.findViewById(R.id.list_checkbox));
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    //i'd like to something like below where i can specify the row which contains
                    //the checkbox that has been check and map that to the row in the cursor.
                    // So if the checkbox in the 2nd row in the listview has been clicked then the messageGuid from the 2nd row in the cursor is found

                    //c.moveToPosition(the row position of the listview which holds the checkbox that has been clicked );

                    messageGuid = null; 
                    messageGuid = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_GUID));

                    if(isChecked == true){

                        Log.e(TAG, "checkBox true and guid = " + messageGuid);
                    }else{
                        Log.e(TAG, "checkBox false and guid = " + messageGuid);
                    }  

                }
            });


            return v;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos,
                long id) {

            Cursor itemCursor = (Cursor) view.getTag();

            String messageGuid = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_GUID));



            String messageText = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_TEXT));
            String messageCreatedAt = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_CREATED_AT));
            String messageSender = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_SENDER));
            String messageReplied = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_REPLIED));
            String messageSeen = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_SEEN));
            String isStandAlone = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_IS_STANDALONE));

            Intent i = new Intent(ViewMessagesActivity.this, ReplyToMessageActivity.class);
            i.putExtra("guid", messageGuid);
            i.putExtra("message", messageText);
            i.putExtra("createdat", messageCreatedAt);
            i.putExtra("sender",  messageSender);
            i.putExtra("messagereplied", messageReplied);
            i.putExtra("messageseen", messageSeen);
            i.putExtra("isstandalone", isStandAlone);

            startActivity(i);



        }



    }// end of adapter

Solution

  • Make position final and use that on onCheckedChanged

    or

    make cb final

    Add before cb.setOnCheckedChangeListener

    cb.setTag(position);
    

    And in public void onCheckedChanged you can retrieve the position

    int pos = (Integer) cb.getTag();