将列表转换为从 SQLite 生成的列表视图 [英] Casting list to listview that was generated from SQLite

查看:49
本文介绍了将列表转换为从 SQLite 生成的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速信息

我正在关注 Lynda.com 上名为 App Development with Local Storage 的示例视频之一.我有练习文件,但我正在尝试使用我自己的设置.

I am following one of the example videos on Lynda.com titled App Development with Local Storage. I have the exercise files but I am attempting to follow using my own setup.

数据库设置

正在创建数据库,然后使用Cursor 对象将所有数据填充到DataItem 元素的List 接口.DataItem 是一个实现 Parcelable 的模型类,用于使用 setter 和 getter 对 SQL 数据进行建模.这是我对可能导致问题的类的有限了解.

A database is being created then all the data is being populated to the List interface of DataItem element using the Cursor Object. The DataItem is a model class that implement Parcelable and is used to model the SQL data using setters and getters. This is my limited understanding of the class which may be the cause of the problem.

当前设置

我从标准导航抽屉创建了一个新活动,我试图将数据库(已正确创建和测试)显示到位于 content_main.xml 中的 list_view.

I created a new Activity from the standard Navigation Drawer, and I am attempting to display the database (which is being created correctly, and tested) to a list_view located in content_main.xml.

问题

我的问题在于不知道在 ArrayAdapter 中到底要包含什么.

My problem lies is not knowing exactly what to include within the ArrayAdapter.

listFromDB = maSource.getAllItems();
ArrayAdapter<String> arrayAdapter;

arrayAdapter = new ArrayAdapter<String>
               (this,android.R.layout.simple_list_item_1, **?listFromDB?**); 

ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(arrayAdapter);

正如我所提到的,listFromDB 是包含数据库的列表类型.将其输出到 logcat 将产生 4 列和 2 行.

As I mentioned, listFromDB is of type list that includes the DB. Outputting it into logcat will result in 4 columns and 2 rows.

12-29 00:36:40.977 6326-6326/?D/数据库信息:[数据项{accountId='6d8bb011-bd9a-4c71-b4c7-2ddaa5fe6fce',accountName='Row 1', description='Info About Row 1', balance=1903.59},数据项{accountId='b6d08c68-d41d-4821-b901-23e7f38cebd9',accountName='Row 2', description='Info About Row 2', balance=41.28}]

12-29 00:36:40.977 6326-6326/? D/dbinfo: [DataItem{accountId='6d8bb011-bd9a-4c71-b4c7-2ddaa5fe6fce', accountName='Row 1', description='Info About Row 1', balance=1903.59}, DataItem{accountId='b6d08c68-d41d-4821-b901-23e7f38cebd9', accountName='Row 2', description='Info About Row 2', balance=41.28}]

[ 12-29 00:36:41.097 6326: 6326 D/]

[ 12-29 00:36:41.097 6326: 6326 D/ ]

HostConnection::get() 新主机连接建立 0xb7aba1b0, tid 6326

HostConnection::get() New Host Connection established 0xb7aba1b0, tid 6326

推荐答案

感谢 @Divyesh,我找到了解决问题的方法.

Thanks to @Divyesh I found a solution to my problem.

创建自定义 ArrayAdapter 就成功了:

Creating a custom ArrayAdapter did the trick:

  1. 创建额外的布局视图以包含所需的小部件.在我的案例中,在 data_view.xml
  2. 下创建了 3 个 TextViews
  3. 创建了一个列表解析器,以从在数据库中找到的每一行中提取数据并将其构造为 DataItemlistFromDB 中.
  4. 然后定义自定义适配器,它扩展 ArrayAdapter 并在 data_view.xml 膨胀后将值分配给 TextView.
  5. 最后,在 MainActivity 中使用了新的自定义适配器,并将列表中的项目分配给适当的 TextViews
  1. Create an additional layout view to include required widgets. 3 TextViews were created in my case under data_view.xml
  2. A list parser was created to extract the data from each row that is found inside the database and constructed as DataItem into listFromDB.
  3. The custom adapter was then defined which extends ArrayAdapter and assign the values to the TextViews after the data_view.xml has been inflated.
  4. Lastly, the new custom adapter was used in MainActivity and items from the list were assigned to the appropriate TextViews

以下是我稍后将重构的上述步骤的示例.

The following is a sample of the steps mentioned above to which I am going to refactor later on.

data_view.xml

<TextView
    android:id="@+id/accountName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Account Name" />
<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Description" />
<TextView
    android:id="@+id/balance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Balance" />

ListParser

public class ListParser {
    public String accountName;
    public String description;
    public String balance;

    public ListParser(String accountName, String description, String balance) {
        this.accountName = accountName;
        this.description = description;
        this.balance = balance;
    }
}

自定义适配器

public class AccountAdapter extends ArrayAdapter<ListParser> {
    public AccountAdapter(Context context, ArrayList<ListParser> users) {
        super(context, 0, users);
    }

    public static class ViewHolder {
        TextView accountName;
        TextView description;
        TextView balance;

        public ViewHolder(View convertView) {
             accountName = (TextView) convertView.findViewById(R.id.accountName);
             description = (TextView) convertView.findViewById(R.id.description);
             balance = (TextView) convertView.findViewById(R.id.balance);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        ListParser user = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.account_view, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        // Lookup view for data population
        TextView accountName = viewHolder.accountName;
        TextView description = viewHolder.description;
        TextView balance = viewHolder.balance;
        // Populate the data into the template view using the data object
        accountName.setText(user.accountName);
        description.setText(user.description);
        balance.setText(user.balance);
        // Return the completed view to render on screen
        return convertView;
    }
}

MainActivity(单行)

ArrayList<ListParser> arrayOfListParsers = new ArrayList<ListParser>();
AccountAdapter adapter = new AccountAdapter(this, arrayOfListParsers);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);

ListParser newUser = new ListParser(String.valueOf(listFromDB.get(0).getaccountName()), String.valueOf(listFromDB.get(0).getDescription()), String.valueOf(listFromDB.get(0).getBalance()));
adapter.add(newUser);

<小时>

需要对 MainActivity 类做更多的工作,但这只是一个测试运行.


More work needs to be done to the MainActivity Class, but this was just a test run.

附加信用:https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

这篇关于将列表转换为从 SQLite 生成的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆