欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-10-27 20:31:04
我们在使用listview的时候需要和数据进行绑定,那么问题来了,如何获取sqlite数据库中的数据并动态的显示到listview当中呢?其实过程很简单:首先要获取sqlite数据(当然首先你要创建一...

我们在使用listview的时候需要和数据进行绑定,那么问题来了,如何获取sqlite数据库中的数据并动态的显示到listview当中呢?其实过程很简单:首先要获取sqlite数据(当然首先你要创建一个sqlite数据库并填写了一些数据),然后引入listview控件,最后将数据和listview绑定就好了。

一 获取sqlite数据库中的数据

sqlite是一个轻量级的数据库,它能将数据保存到你的手机,但缺点是一旦软件卸载所有数据将一同被销毁。所以要根据自己的项目需要选择性的使用。下面要演示将sqlite中的数据提取出来。

首先定义一个类用来实例化数据库

public class initdate {
  public bitmap bitmap;
  public string content;
  public string data;
  public initdate (bitmap bitmap ,string context,string time){
    this.bitmap =bitmap;
    this.content =context;
    this.data =time;
  }
}

创建一个list对象用来存储数据

list<initdate> list = new arraylist<>();

获取sqlite中对应表的数据

 dbopenhelper helper = new dbopenhelper(getactivity(), "数据库的名称", null, 1);//创建对象
    sqlitedatabase db = helper.getwritabledatabase();
    cursor c = db.query("表名", null, null, null, null, null, null);
    if (c != null && c.getcount() >= 1) {
      while (c.movetonext()) {
        list.add(new initdate(base64tobitmap(c.getstring(c.getcolumnindex("字段名1"))), c.getstring(c.getcolumnindex("字段名2")),
            c.getstring(c.getcolumnindex("字段名3"))));
      }
      c.close();
      db.close();//关闭数据库
    }

base64tobitmap方法用于将string类型转换成bitmap

 public static bitmap base64tobitmap(string base64info) {
    byte[] bytes = base64.decode(base64info, base64.default);
    return bitmapfactory.decodebytearray(bytes, 0, bytes.length);
  }

二 引入listview控件

listview的引入是比较简单的,我们可以直接将listview控件拖拽到xml文件中即可。这里不过多介绍

 <listview
    android:id="@+id/lv_expense"
    style="@style/animation.appcompat.dropdownup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

三 将数据和listview绑定

首先将获取到的数据通过一个循环存放到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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!