Using the following code, when an item from the list is selected it updates a data table. The problem is when there are several items listed, regardless of which one is selected, it always updates the first listed item showing rather than the one selected. Thanks in advance!
Edited-Updated. Incorporated (position) and tried to simplify a bit of the code. Still will not capture the selected item, always returns the top item showing on the listview, regardless. The DB controller is working fine, all else is good except this...
setContentView(R.layout.list_messages);
ArrayList<HashMap<String, String>> phraseList = controller
.getUnreadMessage();
ListView lv = getListView();
ListAdapter adapter = new SimpleAdapter(
ReadUnReadMessages.this,
phraseList,
R.layout.view_list_messages,
new String[] { "mFromName", "mToAddress", "mBody",
"mToName", "messageTime", "mFromAddress", "mRead",
"messageId" },
new int[] { R.id.messageFrom, R.id.messageToAdd,
R.id.messageBody, R.id.messageTo, R.id.messageTime,
R.id.messageFromAdd, R.id.readCode, R.id.messageId });
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long arg) {
@SuppressWarnings("unchecked")
HashMap<String, String> queryValues = (HashMap<String, String>)parent.getItemAtPosition(position);
messageBody = (TextView) findViewById(R.id.messageBody);
readCode = (TextView) findViewById(R.id.readCode);
messageId = (TextView) findViewById(R.id.messageId);
String readCode = "1";
queryValues
.put("messageId", messageId.getText().toString());
queryValues.put("mRead", readCode);
queryValues.put("mBody", messageBody.getText().toString());
controller.markMessage(queryValues);
this.callSplash(view);
}
public void callSplash(View view) {
Intent objSplash = new Intent(getApplicationContext(),
Splash.class);
startActivity(objSplash);
}
});
It's hard to tell exactly how you were expecting it to work, but it seems like your central problem is that you are ignoring the 'position' parameter to onItemClick. This is Android's way of telling you which item was clicked. Somehow you need to pass that parameter, or else the corresponding item object that you fetch first from the adapter, on to the functions you call from onItemClick:
Otherwise those functions would have to guess which item was clicked. And apparently that is what they are doing: they are assuming the first item was clicked.