gpt4 book ai didi

android - 从 2 个表 SQLite 数据库填充 ListView

转载 作者:行者123 更新时间:2023-11-29 02:04:08 25 4
gpt4 key购买 nike

我想从 SQLite 数据库中的 2 个表填充一个 ListView 。 ListView 将包含 3 个 View :一个 ImageView 和 2 个 TextView 。

  • 第一个表:imageview 和 textview1
  • 第二张表:textview2

我可以成功地从一个表中查询数据并将其显示在 ListView 中,但不知道如何从第二个表中查询,然后将其添加到列表中。

下面的代码显示了一个仅包含来自 my_folders 的数据的 ListView 。我如何更改它,以便 textview1 显示来自表 my_folders 的数据,而 textview2 显示来自 my_expenses 的数据。任何人都可以帮忙吗?

这是我的主程序:

public class mymain extends ListActivity { ListView listContent;

SimpleCursorAdapter cursorAdapter; final DBAdapter mySQLiteAdapter = new DBAdapter(this); Cursor cursor;

/** Called when the activity is first created. */

@Override public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

// setContentView(R.layout.list_example);

  listContent = (ListView) findViewById(R.id.custom_list);
final DBAdapter mySQLiteAdapter = new DBAdapter(this);

mySQLiteAdapter.open();

cursor = mySQLiteAdapter.getAllFolders();

String[] from = new String[] { DBAdapter.KEY_NAME,DBAdapter.KEY_CURRENCY };
int[] to = new int[] { R.id.name_row, R.id.notes_row };


cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_row, cursor,
from, to);

setListAdapter(cursorAdapter);
mySQLiteAdapter.close();

} }

这是我的数据库:

import android.database.Cursor;

public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_CURRENCY = "currency";
public static final String KEY_NOTES = "notes";
public static final String KEY_EXPENSE_NAME = "expense_name";
public static final String KEY_EXPENSE_AMOUNT = "expense_amount";
public static final String KEY_EXPENSE_DATE = "expense_date";
public static final String KEY_EXPENSE_TIME = "expense_time";
public static final String KEY_EXPENSE_NOTES = "expense_notes";
public static final String KEY_EFOLDERID = "e_fid";

private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "MyDB";
private static final String DATABASE_TABLE1 = "my_folders";
private static final String DATABASE_TABLE2 = "my_expenses";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE_FOLDERS =
"create table my_folders (_id integer primary key autoincrement, "
+ "name text not null, currency text not null, notes text);";

private static final String DATABASE_CREATE_EXPENSES =
"create table my_expenses (_id integer primary key autoincrement, "
+ "expense_name text not null, expense_amount real not null, " +
"expense_date text not null, expense_time text not null," +
"expense_notes text,"
+ "e_fid integer not null," +
"FOREIGN KEY ("+ KEY_EFOLDERID+ ") REFERENCES " + DATABASE_TABLE1 +" ("+ >KEY_ROWID + ") ON DELETE CASCADE);";

// ---retrieves all the contacts---
public Cursor getAllFolders() {
return db.query(DATABASE_TABLE1, new String[] { KEY_ROWID, KEY_NAME,
KEY_CURRENCY, KEY_NOTES}, null, null, null, null, null);
}

// ---retrieves all the expenses---
public Cursor getAllExpenses() {
return db.query(DATABASE_TABLE2, new String[] { KEY_ROWID, KEY_EXPENSE_NAME,
KEY_EXPENSE_AMOUNT, KEY_EXPENSE_DATE, KEY_EXPENSE_TIME, KEY_EXPENSE_NOTES, >KEY_EFOLDERID}, null, null, null, null, null);
}

最佳答案

从 2 个表中获取数据的最佳方法是使用连接查询,例如

SELECT name, expense_name, expense_notes
FROM my_folders, my_expenses
WHERE my_folders._id = my_folders.e_fid

这将返回 1 Cursor 中的所有数据,然后您可以将其放入 View

关于android - 从 2 个表 SQLite 数据库填充 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10975434/

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