I have a listview linked to a sqlite database. I have 3 items in the database, but everytime I click on the second and third value it gives me the same result as the very first value in the record set. How do I move through subsequent items in the recordset? Here is the code:
DBAdapter db = new DBAdapter(this);
db.open();
lv = (ListView)findViewById(R.id.list);
//Get cursor for list items
cursor = db.getAllChannels();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//String cText = lv.getItemAtPosition(position).toString();
//retrieve database values
Toast.makeText(getApplicationContext(), cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_STATIONURL)), Toast.LENGTH_SHORT).show();
}
});
When the adapter is already populated using a cursor, you should refrain from acquiring data using the cursor fed to the adapter. Instead use
getItem(int position)which should return a cursor that is already pointed to the correct item in your list. Your code would then look like this:You can go a step further and override your CursorAdapter’s
getItem(position)so that it returns aData* object wherein you just need togetKeyStationUrl()* to get the value that you need.