Android Studio如何獲取SQLite數(shù)據(jù)并顯示到ListView上
我們?cè)谑褂肔istView的時(shí)候需要和數(shù)據(jù)進(jìn)行綁定,那么問(wèn)題來(lái)了,如何獲取SQLite數(shù)據(jù)庫(kù)中的數(shù)據(jù)并動(dòng)態(tài)的顯示到ListView當(dāng)中呢?其實(shí)過(guò)程很簡(jiǎn)單:首先要獲取SQLite數(shù)據(jù)(當(dāng)然首先你要?jiǎng)?chuàng)建一個(gè)SQLite數(shù)據(jù)庫(kù)并填寫了一些數(shù)據(jù)),然后引入ListView控件,最后將數(shù)據(jù)和ListView綁定就好了。
一 獲取SQLite數(shù)據(jù)庫(kù)中的數(shù)據(jù)
SQLite是一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),它能將數(shù)據(jù)保存到你的手機(jī),但缺點(diǎn)是一旦軟件卸載所有數(shù)據(jù)將一同被銷毀。所以要根據(jù)自己的項(xiàng)目需要選擇性的使用。下面要演示將SQLite中的數(shù)據(jù)提取出來(lái)。
首先定義一個(gè)類用來(lái)實(shí)例化數(shù)據(jù)庫(kù)
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)建一個(gè)List對(duì)象用來(lái)存儲(chǔ)數(shù)據(jù)
List<initdate> list = new ArrayList<>();
獲取SQLite中對(duì)應(yīng)表的數(shù)據(jù)
DBOpenHelper helper = new DBOpenHelper(getActivity(), "數(shù)據(jù)庫(kù)的名稱", null, 1);//創(chuàng)建對(duì)象
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ù)庫(kù)
}
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的引入是比較簡(jiǎn)單的,我們可以直接將ListView控件拖拽到xml文件中即可。這里不過(guò)多介紹
<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ù)通過(guò)一個(gè)循環(huán)存放到map對(duì)象中
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)聽(tīng)器
@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>
此時(shí)我們已經(jīng)將獲取到的數(shù)據(jù)和ListView進(jìn)行了綁定,我們可以直接運(yùn)行,發(fā)現(xiàn)除了小照片不能顯示外其他的信息都正常顯示。這是由于SimpleAdapter 適配器默認(rèn)使用顯示的圖片資源都是程序內(nèi)的本地資源就是能通過(guò)R.drawable.–得到的,如果我們想要把從數(shù)據(jù)庫(kù)中獲得的Bitmap類型的圖片顯示到ListView中就要自己實(shí)現(xiàn)ViewBinder()這個(gè)接口,在里面定義數(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)聽(tīng)器
@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();
}
});
此時(shí)照片資源也能正常顯示了。
總結(jié)
到此這篇關(guān)于Android Studio如何獲取SQLite數(shù)據(jù)并顯示到ListView上的文章就介紹到這了,更多相關(guān)android studio SQLite數(shù)據(jù)ListView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)
- android studio3.0以上如何通過(guò)navicat訪問(wèn)SQLite數(shù)據(jù)庫(kù)文件
- android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫(kù)的方法
- Android Studio 通過(guò)登錄功能介紹SQLite數(shù)據(jù)庫(kù)的使用流程
- SQLiteStudio優(yōu)雅調(diào)試Android手機(jī)數(shù)據(jù)庫(kù)Sqlite(推薦)
- android?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改
相關(guān)文章
android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算
這篇文章主要為大家詳細(xì)介紹了android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
解決Android 5.1限制外置SD卡寫入權(quán)限的問(wèn)題
今天小編就為大家分享一篇解決Android 5.1限制外置SD卡寫入權(quán)限的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
詳解Android的內(nèi)存優(yōu)化--LruCache
LruCache是基于Lru算法實(shí)現(xiàn)的一種緩存機(jī)制。本文對(duì)LruCache的概念和實(shí)現(xiàn)原理進(jìn)行介紹,通過(guò)實(shí)例分析和使用介紹,讓大家更好的了解LruCache,下面跟著小編一起來(lái)看下吧2016-12-12
Android WebView自定義長(zhǎng)按選擇實(shí)現(xiàn)收藏/分享選中文本功能
這篇文章主要介紹了Android WebView自定義長(zhǎng)按選擇實(shí)現(xiàn)收藏/分享選中文本功能,需要的朋友可以參考下2017-06-06
Android Studio 3.6中新的視圖綁定工具ViewBinding 用法詳解
這篇文章主要介紹了Android Studio 3.6中新的視圖綁定工具ViewBinding 用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android中WebView實(shí)現(xiàn)點(diǎn)擊超鏈接啟動(dòng)QQ的方法
這篇文章主要給大家介紹了在Android中WebView如何實(shí)現(xiàn)點(diǎn)擊超鏈接啟動(dòng)QQ的方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的學(xué)習(xí)或者工作具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04
Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解
這篇文章主要為大家介紹了Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

