gpt4 book ai didi

android - 如何在 setOnItemClickListener 上将 ListView 记录从 sqlite 显示到第二个 ListView

转载 作者:太空狗 更新时间:2023-10-29 14:53:35 27 4
gpt4 key购买 nike

我已经创建了 ListView ,我想要做的是当用户点击第一个 ListView 时,该特定字段的选定记录应该显示在第二个 ListView 中,

在我的代码中,我只是获取第一个 ListView 的项目位置,并将该 ListView 的选定项目传递到我的第二个 Activity 我不知道接下来要做什么

所以请给我代码/想法如何做到这一点,因为我是 android 的新手..

这是我的代码

MainActivity:(第一个 ListView )

    listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}

sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.gettitles(sqLiteDatabase);

String[] from = new String[] { dbHelper.TITLE };
int[] to = new int[] {R.id.title };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.title_row,cursor,from,to);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Cursor cursor =(Cursor) (listView.getItemAtPosition(position));
String selectedItem = cursor.getString(0);
Intent intent = new Intent(MainActivity.this, SubcategoryActivity.class);
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
}
});

MainActivity Xmlcode:(FirSTListview)

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

Title_row.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/title"
android:layout_gravity="center_horizontal" />

第二个 Activity :(第二个 ListView )listView2= (ListView) findViewById(R.id.listView2);

    final String selectedData = intent.getStringExtra("selectedItem");
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}

sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase,selectedData);
String[] from = new String[] { dbHelper.SUBCATEGORY };
int[] to = new int[] {R.id.subcategory };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.subcategory_row,cursor,from,to);
adapter.notifyDataSetChanged();
listView2.setAdapter(adapter);

子类别 Xml 文件:

 <ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

subcategory_row.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/subcategory"
android:layout_gravity="center_horizontal" />

数据库类:

public Cursor gettitles(SQLiteDatabase db)
{
db = this.getReadableDatabase();
Cursor cursor;
cursor = db.query(true, "record", new String[]{TITLE,ITEMNO + " as _id"}, null, null, TITLE, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}

public Cursor getsubcategory(SQLiteDatabase db,String sub)
{
db=this.getReadableDatabase();
Cursor cursor;
cursor = db.query("record",new String[]{SUBCATEGORY},TITLE + "=" +sub,null,null,null,null);
return cursor;
}

日志:

Caused by: android.database.sqlite.SQLiteException: near "FOOD": syntax error (code 1): , while compiling: SELECT subcategory FROM record WHERE title=GENERAL FOOD TIPS
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1448)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1295)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1166)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1334)
at com.example.aeiltech.sidd.SqlLiteDbHelper.getsubcategory(SqlLiteDbHelper.java:118)
at com.example.aeiltech.sidd.SubcategoryActivity.onCreate(SubcategoryActivity.java:38)

最佳答案

我不太确定你想做什么,但我在你的代码中看到两个错误,可能有助于解决问题。如果他们不这样做,请提供更易于理解的问题描述。

  1. 在 onItemClickListener 中,您试图将项目从列表转换为字符串。但是,方法 getItemAtPosition(position) 将传递给适配器的函数 getItem(position),在您的情况下,该函数将返回一个 Cursor。您不能将 Cursor 转换为 String。因此,您在此位置的代码应如下所示:

    Cursor cursor =(Cursor) (listView.getItemAtPosition(position));
    String selectedItem = cursor.getString(0);
  2. 在您的第二个 ListView 中,您有以下函数调用:

    cursor=dbHelper.getsubcategory(sqLiteDatabase,sub);

    但是我没有看到您在哪里定义sub。阅读您的代码后,我最好的猜测是 sub 应该是第一个 ListView 中的选定标题。因此这个调用应该是这样的:

    cursor=dbHelper.getsubcategory(sqLiteDatabase,selectedData);

关于android - 如何在 setOnItemClickListener 上将 ListView 记录从 sqlite 显示到第二个 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33278556/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com