2
我的编程知识很苗条,前几天我发现了Java和Android SDK。Begginner使用ListAdapters/SQLite数据库
我有一个SQLiteDatabase,一个SQLiteOpenHelper,和光标正常工作 (指我想我明白了如何创建/使用SQLiteOpenHelper打开一个数据库,让我的数据库查询,并获得光标)
目前,我正在将i + labName + labCity包装成单个结果字符串,并显示为单个TextView R.id.label
是否可以将labName绑定到R.id.label,并将labCity绑定到另一个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类很简单,只需定义一些列如果数据库不存在,以及一些变量。
您的源代码示例很有帮助。 我使用了一个SimpleCursorAdapter,但是卡住了一些异常:java.lang.IllegalArgumentException:列'_id'不存在。 如果您的查询没有_ID列,则会出现此异常。 非常感谢我的第一个问题:)解决 – Dullahx 2010-01-16 17:10:06
是的,这是'CursorAdapter'系列的限制之一 - 你必须在名为'_ID'的结果集中有一列可以转换为'long' 。 – CommonsWare 2010-01-16 17:25:08