Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从SQLiteDatabase检索数据并将其显示在新活动中_Android_Database_Sqlite_Android Listview_Data Retrieval - Fatal编程技术网

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);
星触觉(新活动);
}

请不要垃圾邮件发送问题。检查您的收件箱,并请删除您在此处对我的答案所做的第一条评论。请不要垃圾邮件发送问题。检查您的收件箱,并请删除您在此处对我的答案所做的第一条评论。