[英]I want to put multiple values from the SQLite database in a listview row
Android初學者在這里,我無法在列表視圖中填充項目。 我總是可以獲取其中一個數據庫條目值,但是我希望每個列表行都有兩個項目。 這是用於填充列表視圖的類。 我想將KEY_TITLE和KEY_BODY的值帶入列表視圖。 理想情況下,我希望KEY_BODY成為listview的子項,但是我認為這可能需要兩個項的listview,即使閱讀了API后,我也不知道如何實現。
@SuppressWarnings("deprecation")
private void fillData() {
Cursor moviesCursor = mDbHelper.fetchAllMovies();
startManagingCursor(moviesCursor);
String[] from = new String[]{
MyMoviesDBAdapter.KEY_ROWID,
MyMoviesDBAdapter.KEY_BODY,
MyMoviesDBAdapter.KEY_TITLE
};
int[] to = new int[] {
R.id.text3,
R.id.text2,
R.id.text1
};
ListAdapter movies = new SimpleCursorAdapter(this, R.layout.listtextview,
moviesCursor, from , to);
moviesCursor.moveToNext();
setListAdapter(movies);
}
這是我的XML布局
<LinearLayout >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:textColor="#a4a4a4"
android:textSize="10sp"
/>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:textColor="#ffffff"
android:textSize="24sp"
/>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:textColor="#a4a4a4"
android:textSize="15sp"
/>
</LinearLayout>
您錯過了BaseColumns._ID
String[] from = new String[]{
MyMoviesDBAdapter.KEY_ROWID,
MyMoviesDBAdapter.KEY_BODY,
MyMoviesDBAdapter.KEY_TITLE,BaseColumns._ID
};
問題出在我的XML布局中。 我忽略了在導致運行時異常的父級“線性布局”中指定詳細信息。 希望這對某人有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.