android listview行id映射到Anonymous内部类中的CursorAdapter行



我有一个SimpleCursorAdapter类,它从DB中获取各种数据并显示在ListView中。它实现了一个onclicklistener,它向另一个活动发送messageGuid字符串。所有这些都很好。

在ListView的行中,我添加了一个CheckBox,并为其设置了一个onCheckChangeListener。在选中该复选框的那一刻,messageGuid总是光标中的最后一个。

我需要找到一种方法来获取包含已选中复选框的行的listview行id。然后我可以获得正确的光标行,然后依次获得正确的messageGuid。

我已经在onCheckedChanged方法中评论了我想要的内容。

提前谢谢马特。

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

定位final并在onCheckedChanged上使用

使cbfinal

cb.setOnCheckedChangeListener之前添加

cb.setTag(position);

public void onCheckedChanged中,您可以检索位置

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

最新更新