1
所以我一直在試圖讓ListView在SQLite數據庫中顯示數據幾天,而且我仍然找不到解決方案。帶有自定義光標適配器的ListView是空/空
ListView的XML看起來像:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/gotoEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Entry" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list" >
</ListView>
</LinearLayout>
與行項目看起來像這樣:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15dp"
android:padding="15dp"
android:id="@+id/thetext"
>
</TextView>
我CursorAdapter的樣子:
public class ItemAdapter extends CursorAdapter{
private Cursor iCursor;
private Context iContext;
private final LayoutInflater iInflater;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
iInflater=LayoutInflater.from(context);
iContext=context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView itemName = (TextView) view.findViewById(R.id.thetext);
itemName.setText(cursor.getString(cursor.getColumnIndex(db_Setup.KEY_NAME)));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) throws InflateException {
final View view = iInflater.inflate(R.layout.single_item, parent, false);
return view;
}
}
光標正在與製作此方法:
public static Cursor getName() {
Cursor q = myDatabase.query(DATABASE_TABLE, new
String[] {db_Setup.KEY_NAME}, null, null, null, null, null);
q.moveToFirst();
return q;
}
和實例在listactivity像這樣:
Cursor cursor = db_Setup.getName();
我試圖做到這一點我在ListActivity到ListView光標綁定:
ItemAdapter ia = new ItemAdapter(getBaseContext(), cursor);
ia.bindView(findViewById(R.id.thetext), getApplicationContext(), cursor);
當我運行沒有錯誤應用程序...所有這裏有一個按鈕,從這個職位的頂部的XML和空白(沒有listView)... 我已經在SQLite數據庫中輸入了一些值,如14值左右。爲什麼沒有顯示?
非常感謝!我沒想到它是那麼小的錯誤!那麼bindView方法會是什麼呢? – btru 2012-07-15 02:46:56
此方法由ListView在內部調用以綁定記錄。你不直接調用它。你實際上可以優化你的方法。 ListView回收記錄視圖。如果view參數不爲null,那麼可以重新使用它,否則將膨脹一個新的視圖。這將使滾動更順暢。 – Y2i 2012-07-15 02:50:17