I can populate my ListView of items from my SQLiteDatabase in lunch.java. now I want to click the one item(total 8 items inside the ListView) and go to a new activity called Display.java and display all the nutrition facts of it.
AFTER EDITING
lunch.java:
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
switch(pos)
{
case 0 :
String mealName = (String) parent.getItemAtPosition(pos);
Cursor cursor = dbopener.getBreakfastDetails(mealName);
cursor.moveToNext();
id = cursor.getLong(cursor.getColumnIndex(mealName));
String message = cursor.getString(1) + "\n" + cursor.getInt(2);
Intent event1 = new Intent("com.edu.tp.iit.mns.Display");
event1.putExtra("name", id);
startActivity(event1);
break;
Display.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
TextView tv = (TextView) findViewById(R.id.tvfoodName);
Intent intent = getIntent();
long id = intent.getLongExtra("name", -1);
if(id == -1){
return;
}
tv.setText(-1);
}
In the
onItemcClickyou already have theidof the element that was clicked, theidparameter. Use that to identify the item in your next activity:Then in your
Displayactivity get that long value and get the data from the database corresponding to thatid:The above code would work for the case of a
Cursorbased adapter. But you probably use a list based adapter(because of thegetItemAtPosition(pos)returning aStringand not aCursor). In this case I would make thegetLunchDetailsmethod to return the uniqueidof that meal name and pass that to theDetailsactivity: