最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Studio如何獲取SQLite數(shù)據(jù)并顯示到ListView上

 更新時間:2020年03月27日 10:56:18   作者:小李也有春天  
這篇文章主要介紹了Android Studio獲取SQLite數(shù)據(jù)并顯示到ListView上,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

我們在使用ListView的時候需要和數(shù)據(jù)進行綁定,那么問題來了,如何獲取SQLite數(shù)據(jù)庫中的數(shù)據(jù)并動態(tài)的顯示到ListView當(dāng)中呢?其實過程很簡單:首先要獲取SQLite數(shù)據(jù)(當(dāng)然首先你要創(chuàng)建一個SQLite數(shù)據(jù)庫并填寫了一些數(shù)據(jù)),然后引入ListView控件,最后將數(shù)據(jù)和ListView綁定就好了。

一 獲取SQLite數(shù)據(jù)庫中的數(shù)據(jù)

SQLite是一個輕量級的數(shù)據(jù)庫,它能將數(shù)據(jù)保存到你的手機,但缺點是一旦軟件卸載所有數(shù)據(jù)將一同被銷毀。所以要根據(jù)自己的項目需要選擇性的使用。下面要演示將SQLite中的數(shù)據(jù)提取出來。

首先定義一個類用來實例化數(shù)據(jù)庫

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;
  }
}

創(chuàng)建一個List對象用來存儲數(shù)據(jù)

List<initdate> list = new ArrayList<>();

獲取SQLite中對應(yīng)表的數(shù)據(jù)

 DBOpenHelper helper = new DBOpenHelper(getActivity(), "數(shù)據(jù)庫的名稱", null, 1);//創(chuàng)建對象
    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();//關(guān)閉數(shù)據(jù)庫
    }

base64ToBitmap方法用于將String類型轉(zhuǎn)換成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"/>

三 將數(shù)據(jù)和ListView綁定

首先將獲取到的數(shù)據(jù)通過一個循環(huán)存放到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() {//設(shè)置監(jiān)聽器
      @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>

此時我們已經(jīng)將獲取到的數(shù)據(jù)和ListView進行了綁定,我們可以直接運行,發(fā)現(xiàn)除了小照片不能顯示外其他的信息都正常顯示。這是由于SimpleAdapter 適配器默認使用顯示的圖片資源都是程序內(nèi)的本地資源就是能通過R.drawable.–得到的,如果我們想要把從數(shù)據(jù)庫中獲得的Bitmap類型的圖片顯示到ListView中就要自己實現(xiàn)ViewBinder()這個接口,在里面定義數(shù)據(jù)和視圖的匹配關(guān)系 。

 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() {//設(shè)置監(jiān)聽器
      @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();
      }
    });

此時照片資源也能正常顯示了。

總結(jié)

到此這篇關(guān)于Android Studio如何獲取SQLite數(shù)據(jù)并顯示到ListView上的文章就介紹到這了,更多相關(guān)android studio SQLite數(shù)據(jù)ListView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

平江县| 苏尼特左旗| 基隆市| 五家渠市| 昌乐县| 永善县| 长丰县| 勐海县| 清河县| 神农架林区| 元朗区| 中阳县| 三河市| 宝坻区| 沈阳市| 银川市| 桓仁| 河西区| 和田县| 美姑县| 南江县| 临江市| 湄潭县| 商丘市| 房山区| 莱西市| 大埔区| 颍上县| 丰顺县| 扶余县| 襄城县| 乌苏市| 宝丰县| 拉孜县| 永新县| 综艺| 潍坊市| 玛多县| 合肥市| 云阳县| 定结县|