Java 我应该使用哪种适配器将数据从ArrayList传输到ListView?
我正在尝试将数据从数据库传输到ListView。以前,我使用SimpleCursorAdapter从包含所有数据的游标传输数据,但现在,我需要将五个列表中的数据传输到将游标拆分为的五个列表视图中。我认为我不能使用ArrayAdapter,因为我的数据库有多个列。我不太确定如何创建自己的适配器,尽管我读到可以使用它。我只是不知道如何从BaseAdapter创建我自己的适配器 这是我的片段布局文件 SQL来创建我的数据库Java 我应该使用哪种适配器将数据从ArrayList传输到ListView?,java,android,sqlite,listview,adapter,Java,Android,Sqlite,Listview,Adapter,我正在尝试将数据从数据库传输到ListView。以前,我使用SimpleCursorAdapter从包含所有数据的游标传输数据,但现在,我需要将五个列表中的数据传输到将游标拆分为的五个列表视图中。我认为我不能使用ArrayAdapter,因为我的数据库有多个列。我不太确定如何创建自己的适配器,尽管我读到可以使用它。我只是不知道如何从BaseAdapter创建我自己的适配器 这是我的片段布局文件 SQL来创建我的数据库 简单地说,将数据库中的结果存储到对象的arraylist中。使用阵列适配器将这
简单地说,将数据库中的结果存储到对象的arraylist中。使用阵列适配器将这些添加到列表视图中。我想不出更简单的方法了……为此,我在应用程序中使用了一个扩展了
ArrayAdapter
的CustomAdapter
以此为例。但是,如何将存储在对象中的每个不同值添加到内部列表的不同部分。因此,我的每个对象都称为条目。它们的存储方式类似于四个字符串和一个int。如何将这些值分别设置为内部listview的一个元素ArrayAdapter类可以处理任何Java对象作为输入。它将此输入的数据映射到布局中的文本视图。您可以在构造函数中定义一个,否则将使用android.R.id.text1 id。ArrayAdapter使用数据输入对象的toString()方法确定应显示的字符串。“我不想使用字符串并将所有值设置为一个文本视图,然后乘以该文本视图来创建listView。我想要一个具有多个文本视图的内部视图,并且每个值都设置为其中一个。它使列表视图更具可定制性。是的,您需要一个自定义适配器,并且为了让每个文本视图都充满不同的文本,您需要使用对象。这是我用来学习的网站。它工作得很好。谢谢,我想我会在我的应用程序中使用类似的东西。这篇文章帮助很大。我不能投票赞成你的答案,因为我还没有足够的声誉,但我会把它作为答案。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
/* ....(I skipped much of the irrelevant code here) */
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainList"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lunchList"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dinnerList"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/snackList"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/exerciseList"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"/>
</LinearLayout>
//Append Test Case to Database
db.createEntry(test);
//SimpleCursorAdapter Sequence
cursor = db.getAllEntries();
String[] from = {DESCRIPTION, CALORIES, SERVINGSIZE, DATE};
int[] to = {R.id.description, R.id.calories, R.id.servingSize, R.id.dateBox};
adapt = new SimpleCursorAdapter(getActivity(), R.layout.journal_inner_view, cursor, from, to, 1);
ListView listItem = (ListView) getActivity().findViewById(R.id.mainList);
listItem.setAdapter(adapt);
//To be implemented for delete and edit commands /*listItem.getOnItemLongClickListener();*/
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_JOURNAL + " (_id integer primary key autoincrement, " //NON-NLS-1$
+ JournalDBAdapter.MEAL + " TEXT, " //NON-NLS-1$
+ JournalDBAdapter.DESCRIPTION + " TEXT, " //NON-NLS-1$
+ JournalDBAdapter.DATE + " TEXT, " //NON-NLS-1$
+ JournalDBAdapter.CALORIES + " INTEGER, " //NON-NLS-1$
+ JournalDBAdapter.SERVINGSIZE + " INTEGER" //NON-NLS-1$
+ " );"); //NON-NLS-1$ //NON-NLS-2$);