Passing Row Id Of A List To Another Activity
I have two activity. When the user clicks on the medicine name on the list, it will go to second activity and display the details of the medicine. For the first activity, below i
Solution 1:
In the caller activity you have: myIntent.putExtra("ID", id);
While in the opened activity instead of
i.getIntExtra("int", -1);
it should be
i.getIntExtra("ID", -1);
Notice that the name of the extra should be the same when you try to get it from the intent.
Solution 2:
in the second activity there is a problem
in the first activity
Intent myIntent = newIntent(v.getContext(), MedicineDetail.class);
myIntent.putExtra("ID", position);
startActivity(myIntent);
finalint id = i.getIntExtra("int", -1); should be
finalint id = i.getIntExtra("ID", -1);
Solution 3:
Edit : you are getting the long value but id returns int so it throws classcast exception
instead of this
idList.add(cursor.getLong(0));
use this
idList.add(cursor.getInt(0));
Your id is different from the rowid in the table.. Don't mess up with them, You are sending id of the position in your list. So what you need to do is
Create a Hashmap to map ids with data globally
Hashmap Idshash=newHashmap();
Add values to the hasmap while inserting to results list
longid=c.getLong(getIndex);
String item=c.getString(getNameIndex)+" | " + c.getString(getDosageIndex)
+" | " + c.getString(getFrequencyIndex);
idList.add(id);
Idshash.put(item,id);
result.add(item);
And in onitemclick listener do like this
@Override
publicvoidonItemClick(AdapterView<?> parent, View v, int position, long id) {
String item=((TextView)v).getText();
long selectedid=Idshash.get(item);
Intent myIntent = new Intent(v.getContext(), MedicineDetail.class);
myIntent.putExtra("ID", selectedid);
startActivity(myIntent);
}
Solution 4:
in first actvity change the id
to position
. That's all.
myIntent.putExtra("ID", position);
and in second
getIntExtra("ID", -1);
or
getExtra("ID", -1);
Post a Comment for "Passing Row Id Of A List To Another Activity"