Android Studio如何获取SQLite数据并显示到ListView上


当前第2页 返回上一页

首先将获取到的数据通过一个循环存放到map对象中

 for (int i = 0; i < list.size(); i++) {
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("image", list.get(i).bitmap);
      map.put("category", list.get(i).content);
      map.put("money", list.get(i).data);
      listitem.add(map);
    }

    SimpleAdapter adapter = new SimpleAdapter(getActivity()
        , listitem
        , R.layout.fragment_one_item
        , new String[]{"category", "money", "image"}
        , new int[]{R.id.tv_expense_category, R.id.tv_expense_money, R.id.image_expense});
   
    ListView listView = (ListView) v.findViewById(R.id.lv_expense);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//设置监听器
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);
        Toast.makeText(getActivity(), map.get("category").toString(), Toast.LENGTH_LONG).show();
      }
    });

fragment_one_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView
    android:id="@+id/image_expense"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:adjustViewBounds="true"
    android:maxWidth="72dp"
    android:maxHeight="72dp"/>
  <TextView
    android:id="@+id/tv_expense_category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:padding="10dp"/>
  <TextView
    android:id="@+id/tv_expense_money"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:text="100yuan"/>
</LinearLayout>

此时我们已经将获取到的数据和ListView进行了绑定,我们可以直接运行,发现除了小照片不能显示外其他的信息都正常显示。这是由于SimpleAdapter 适配器默认使用显示的图片资源都是程序内的本地资源就是能通过R.drawable.–得到的,如果我们想要把从数据库中获得的Bitmap类型的图片显示到ListView中就要自己实现ViewBinder()这个接口,在里面定义数据和视图的匹配关系 。

 for (int i = 0; i < list.size(); i++) {
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("image_expense", list.get(i).bitmap);
      map.put("expense_category", list.get(i).content);
      map.put("expense_money", list.get(i).data);
      listitem.add(map);
    }
     SimpleAdapter adapter = new SimpleAdapter(getActivity()
        , listitem
        , R.layout.fragment_one_item
        , new String[]{"expense_category", "expense_money", "image_expense"}
        , new int[]{R.id.tv_expense_category, R.id.tv_expense_money, R.id.image_expense});
    adapter.setViewBinder(new SimpleAdapter.ViewBinder() {

      @Override
      public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
        if ((view instanceof ImageView) & (data instanceof Bitmap)) {
          ImageView iv = (ImageView) view;
          Bitmap bm = (Bitmap) data;
          iv.setImageBitmap(bm);
          return true;
        }
        return false;
      }
    });
    ListView listView = (ListView) v.findViewById(R.id.lv_expense);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//设置监听器
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);
        Toast.makeText(getActivity(), map.get("expense_category").toString(), Toast.LENGTH_LONG).show();
      }
    });

此时照片资源也能正常显示了。

总结

到此这篇关于Android Studio如何获取SQLite数据并显示到ListView上的文章就介绍到这了,更多相关android studio SQLite数据ListView内容请搜索


标签:SQLite

返回前面的内容

相关阅读 >>

centos下更新Sqlite版本

android数据存储之Sqlite使用

django数据库(Sqlite)基本入门使用教程

android中应用多进程的整理总结

android数据库中事务操作方法之银行转账示例

关于centos 7下Sqlite3找不到的问题解决

Sqlite expert pro5.0如何安装可视化数据库管理软件激活教程

c# 启动 sql server 服务的实例

windows平台python连接Sqlite3数据库的方法分析

android 中Sqlite技术实例详解

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...