当前位置: 代码迷 >> Android >> 如何在setOnItemClickListener上将列表视图记录从sqlite显示到第二个listview
  详细解决方案

如何在setOnItemClickListener上将列表视图记录从sqlite显示到第二个listview

热度:60   发布时间:2023-08-04 11:01:49.0

我创建了列表视图,我想要做的是,当用户点击第一个列表视图时,所选记录对于该特定字段应显示在第二个列表视图中,

在我的代码中我只是得到第一个列表视图的项目位置,我将该列表视图的选定项目传递到我的第二个活动我不知道我接下来做什么

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

这是我的代码

MainActivity :(第一个列表视图)

    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" />

第二个活动:(第二个列表视图)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" />

数据库CLass:

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;
    }

logcat的:

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中,您尝试将项目从List转换为String。 但是,方法getItemAtPosition(position)将传递给适配器的函数getItem(position) ,在您的情况下将返回一个Cursor。 您无法将Cursor转换为String。 因此,此位置的代码应如下所示:

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

     cursor=dbHelper.getsubcategory(sqLiteDatabase,sub); 

    但是我没看到你在哪里定义sub 读完代码后我最好的猜测是sub应该是第一个列表视图中的选定标题。 因此,此调用应如下所示:

     cursor=dbHelper.getsubcategory(sqLiteDatabase,selectedData); 
  相关解决方案