【问题标题】:What kind of adapter should I use to transfer data from a ArrayList to an ListView?我应该使用哪种适配器将数据从 ArrayList 传输到 ListView?
【发布时间】:2013-08-02 14:57:26
【问题描述】:

我正在尝试将数据从数据库传输到 ListView。以前,我使用 SimpleCursorAdapter 从包含所有数据的游标中传输数据,但现在,我需要将数据从五个列表传输到 5 个 ListView 中,我将游标拆分为这些列表视图。我认为我不能使用 ArrayAdapter,因为我的数据库有多个列。我不太确定如何创建自己的适配器,尽管我已经读过我可以使用它。我只是不知道如何从 BaseAdapter 创建自己的适配器。

这是我的片段布局文件

<?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>

来自 Fragment 的代码(我目前用于传输数据)

    //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();*/

SQL 创建我的数据库

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$);

【问题讨论】:

    标签: java android sqlite listview adapter


    【解决方案1】:

    为此,我在我的应用程序中使用了扩展 ArrayAdapterCustomAdapter

    查看此link 以获取示例。

    【讨论】:

    • 谢谢,我想我会在我的应用程序中使用这样的东西。这篇文章帮助很大。我无法为您的答案投票,因为我还没有足够的声誉,但我会将其标记为答案。
    【解决方案2】:

    很简单,将数据库中的结果存储到对象数组列表中。使用数组适配器将这些添加到您的列表视图中。我想不出更简单的方法...

    【讨论】:

    • 但是,如何将存储在对象中的每个不同值添加到内部列表的不同部分。所以我的每个对象都称为条目。它们存储四个字符串和一个 int。如何将每个这些值设置为内部列表视图的元素?
    • 引用我发现的一篇文章:“ArrayAdapter 类可以处理任何 Java 对象作为输入。它将此输入的数据映射到布局中的 TextView。您可以在构造函数中定义一个,否则将使用 android.R.id.text1 ID。ArrayAdapter 使用数据输入对象的 toString() 方法来确定应该显示的字符串。我不想使用字符串并将所有值设置为一个文本视图,该文本视图乘以创建列表视图。我想要一个带有多个文本视图的内部视图,并且每个值都设置为其中一个。它使列表视图更具可定制性
    • 嗯,是的,您需要一个自定义适配器,并让每个文本视图填充您需要使用对象的不同文本。这是我用来学习方法的网站。它工作得很好。 josecgomez.com/2010/05/03/…
    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 2016-03-17
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多