Android 从SQLiteDatabase检索数据并将其显示在新活动中
我可以在Android 从SQLiteDatabase检索数据并将其显示在新活动中,android,database,sqlite,android-listview,data-retrieval,Android,Database,Sqlite,Android Listview,Data Retrieval,我可以在sunch.java中从我的SQLiteDatabase填充我的ListView。现在我想单击一个项目(在列表视图中总共有8个项目),然后转到一个名为Display.java的新活动,并显示它的所有营养事实 编辑后 午餐。java: public void onItemClick(AdapterView<?> parent, View v, int pos, long id) { switch(pos) { case 0 : Stri
sunch.java
中从我的SQLiteDatabase
填充我的ListView
。现在我想单击一个项目(在列表视图中总共有8个项目),然后转到一个名为Display.java
的新活动,并显示它的所有营养事实
编辑后
午餐。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;
在单击的元素的id
中,您已经有了被单击元素的id
,即id
参数。使用该选项在下一个活动中标识项目:
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
String mealName = (String)parent.getItemAtPosition(pos);
Cursor cursor = dbopener.getLunchDetails(mealName);
cursor.moveToNext();
long id = cursor.getLong(cursor.getColumnIndex("the name of the id column(probably _id"));
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
上述代码适用于基于光标的适配器。但是您可能使用基于列表的适配器(因为getItemAtPosition(pos)
返回字符串而不是光标
)。在本例中,我将使用get午餐详细信息
方法返回该餐名的唯一id
,并将其传递给详细信息
活动:
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
String mealName = (String)parent.getItemAtPosition(pos);
Cursor cursor = dbopener.getLunchDetails(mealName);
cursor.moveToNext();
long id = cursor.getLong(cursor.getColumnIndex("the name of the id column(probably _id"));
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
public void-onItemClick(AdapterView父视图、视图v、int-pos、长id){
String mealName=(String)parent.getItemAtPosition(pos);
Cursor=dbopener.getSultDetails(mealName);
cursor.moveToNext();
long id=cursor.getLong(cursor.getColumnIndex(“id列的名称(可能是_id)));
Intent newActivity=newintent(“com.edu.tp.iit.mns.Display”);
newActivity.putExtra(“_键”,id);
星触觉(新活动);
}
在onicmclick
中,您已经拥有所单击元素的id
,即id
参数。使用该参数在下一个活动中标识项目:
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
String mealName = (String)parent.getItemAtPosition(pos);
Cursor cursor = dbopener.getLunchDetails(mealName);
cursor.moveToNext();
long id = cursor.getLong(cursor.getColumnIndex("the name of the id column(probably _id"));
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
上述代码适用于基于光标的适配器。但您可能使用基于列表的适配器(因为getItemAtPosition(pos)
返回的是字符串,而不是光标
)。在这种情况下,我将使用getSunchDetails
方法返回该餐名的唯一id
,并将其传递给Details
活动:
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
String mealName = (String)parent.getItemAtPosition(pos);
Cursor cursor = dbopener.getLunchDetails(mealName);
cursor.moveToNext();
long id = cursor.getLong(cursor.getColumnIndex("the name of the id column(probably _id"));
Intent newActivity = new Intent("com.edu.tp.iit.mns.Display");
newActivity.putExtra("the_key", id);
startActivity(newActivity);
}
public void-onItemClick(AdapterView父视图、视图v、int-pos、长id){
String mealName=(String)parent.getItemAtPosition(pos);
Cursor=dbopener.getSultDetails(mealName);
cursor.moveToNext();
long id=cursor.getLong(cursor.getColumnIndex(“id列的名称(可能是_id)));
Intent newActivity=newintent(“com.edu.tp.iit.mns.Display”);
newActivity.putExtra(“_键”,id);
星触觉(新活动);
}
请不要垃圾邮件发送问题。检查您的收件箱,并请删除您在此处对我的答案所做的第一条评论。请不要垃圾邮件发送问题。检查您的收件箱,并请删除您在此处对我的答案所做的第一条评论。