gpt4 book ai didi

android - 使用 ListAdapter/SQLite 数据库的初学者

转载 作者:太空狗 更新时间:2023-10-29 15:24:57 24 4
gpt4 key购买 nike

我的编程知识很薄,几天前我发现了 Java 和 Android SDK。

我有一个 SQLiteDatabase、一个 SQLiteOpenHelper 和一个正常工作的 Cursor(意味着我认为我了解如何使用 SQLiteOpenHelper 创建/打开数据库,在我的数据库上进行查询并获取游标)

目前,我将 i + labName + labCity 包装到一个结果字符串中,并显示到一个 TextView R.id.label 中

是否可以将 labName 绑定(bind)到 R.id.label,并将 labCity 绑定(bind)到另一个 TextView R.id.label2?

这是我需要帮助的 ListActivity 的代码:

public class MainLabList extends ListActivity implements OnItemClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_lab_list);

// DB
GestionDB gdb = new GestionDB(this, GestionDB.DATABASE_NAME, null, GestionDB.DATABASE_VERSION);
SQLiteDatabase theDB = gdb.getWritableDatabase();

// Cursor
String[] columns = {GestionDB.LAB_NAME, GestionDB.LAB_ADDRESS_CITY};
Cursor c =
theDB.query(GestionDB.TABLE_NAME, columns, null, null, null, null, null);

////////
this.setTitle(" " + c.getCount() + " Labs in Local Database");
ArrayList<String> results = new ArrayList<String>();
int i = 0;
c.moveToFirst();

/* Loop through all Results */
do {
i++;
String labName = c.getString(c.getColumnIndex(GestionDB.LAB_NAME));
String labCity = c.getString(c.getColumnIndex(GestionDB.LAB_ADDRESS_CITY));

/* Add current Entry to results. */
results.add("" + i + ": " + labName
+ " (" + labCity + ")");
} while (c.moveToNext());

//need rework
ListView lvLabListing = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> labListAdapter = new ArrayAdapter<String>(this,
R.layout.listing, R.id.label, results );
lvLabListing.setAdapter(labListAdapter);
lvLabListing.setOnItemClickListener(this);
//

// don't forget to close the database
gdb.close();

还有我想修改的 listing.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/label"></TextView>
</LinearLayout>

我的 GestionDB 类很简单,如果数据库不存在,只需定义一些列,以及一些变量。

最佳答案

为此,您应该使用 CursorAdapter,也许是 SimpleCursorAdapterHere is an example .

关于android - 使用 ListAdapter/SQLite 数据库的初学者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2077900/

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