欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年03月27日 10:56:18   作者:小李也有春天  
這篇文章主要介紹了Android Studio獲取SQLite數(shù)據(jù)并顯示到ListView上,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(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 適配器默認(rèn)使用顯示的圖片資源都是程序內(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)文章

  • android中的AIDL進程間通信示例

    android中的AIDL進程間通信示例

    進程之間不能共享內(nèi)存,那么怎么在不同的應(yīng)用程序中進行通訊,這就要依賴AIDL機制,本文詳細(xì)介紹了android中的AIDL進程間通信示例,有興趣的可以了解一下。
    2016-11-11
  • Android?中?FrameLayout?布局及屬性的使用詳解

    Android?中?FrameLayout?布局及屬性的使用詳解

    這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實現(xiàn)簡單布局時非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動按鈕等,需要的朋友可以參考下
    2024-03-03
  • Android實現(xiàn)伸縮彈力分布菜單效果的示例

    Android實現(xiàn)伸縮彈力分布菜單效果的示例

    本文介紹下在Android中實現(xiàn)伸縮彈力分布菜單效果。這種效果比較炫酷,有需要的朋友可以參考一下。
    2016-10-10
  • Android 高仿微信支付數(shù)字鍵盤功能

    Android 高仿微信支付數(shù)字鍵盤功能

    現(xiàn)在很多app的支付、輸入密碼功能,都是使用自定義數(shù)字鍵盤,方便實用。下面本文給大家?guī)砹薃ndroid 高仿微信支付數(shù)字鍵盤功能,非常不錯,感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08
  • Android RxJava創(chuàng)建操作符Timer的方法

    Android RxJava創(chuàng)建操作符Timer的方法

    這篇文章主要為大家詳細(xì)介紹了Android RxJava創(chuàng)建操作符Timer的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android實現(xiàn)掃一掃識別數(shù)字功能

    Android實現(xiàn)掃一掃識別數(shù)字功能

    這篇文章主要介紹了Android實現(xiàn)掃一掃識別數(shù)字功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-09-09
  • Android判斷11位手機號碼的方法(正則表達式)

    Android判斷11位手機號碼的方法(正則表達式)

    項目里頭需要做一個判斷用戶輸入的號碼是否是正確的手機號碼,正確的手機號碼應(yīng)該是11位的,這里我們需要用一個正則表達式來進行判斷,下面我把寫法分享給大家
    2016-12-12
  • 詳解Android?Flutter如何自定義動畫路由

    詳解Android?Flutter如何自定義動畫路由

    flutter中有默認(rèn)的Route組件,叫做MaterialPageRoute,但是MaterialPageRoute太普通了,如果我們想要做點不同的跳轉(zhuǎn)特效應(yīng)該如何處理呢?一起來看看吧
    2023-04-04
  • 詳解Android Studio Git分支實踐

    詳解Android Studio Git分支實踐

    這篇文章主要介紹了Android Studio Git分支實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 讓Android應(yīng)用不被殺死(killer)的方法

    讓Android應(yīng)用不被殺死(killer)的方法

    這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實現(xiàn)方法和原理分析,需要的朋友可以參考下
    2015-04-04

最新評論