Android Studio如何獲取SQLite數(shù)據(jù)并顯示到ListView上
我們?cè)谑褂肔istView的時(shí)候需要和數(shù)據(jù)進(jìn)行綁定,那么問題來了,如何獲取SQLite數(shù)據(jù)庫(kù)中的數(shù)據(jù)并動(dòng)態(tài)的顯示到ListView當(dāng)中呢?其實(shí)過程很簡(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ù)提取出來。
首先定義一個(gè)類用來實(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ì)象用來存儲(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文件中即可。這里不過多介紹
<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ù)通過一個(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)聽器 @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)的本地資源就是能通過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)聽器 @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以上如何通過navicat訪問SQLite數(shù)據(jù)庫(kù)文件
- android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫(kù)的方法
- Android Studio 通過登錄功能介紹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?中?FrameLayout?布局及屬性的使用詳解
這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實(shí)現(xiàn)簡(jiǎn)單布局時(shí)非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動(dòng)按鈕等,需要的朋友可以參考下2024-03-03Android實(shí)現(xiàn)伸縮彈力分布菜單效果的示例
本文介紹下在Android中實(shí)現(xiàn)伸縮彈力分布菜單效果。這種效果比較炫酷,有需要的朋友可以參考一下。2016-10-10Android RxJava創(chuàng)建操作符Timer的方法
這篇文章主要為大家詳細(xì)介紹了Android RxJava創(chuàng)建操作符Timer的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android實(shí)現(xiàn)掃一掃識(shí)別數(shù)字功能
這篇文章主要介紹了Android實(shí)現(xiàn)掃一掃識(shí)別數(shù)字功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-09-09Android判斷11位手機(jī)號(hào)碼的方法(正則表達(dá)式)
項(xiàng)目里頭需要做一個(gè)判斷用戶輸入的號(hào)碼是否是正確的手機(jī)號(hào)碼,正確的手機(jī)號(hào)碼應(yīng)該是11位的,這里我們需要用一個(gè)正則表達(dá)式來進(jìn)行判斷,下面我把寫法分享給大家2016-12-12詳解Android?Flutter如何自定義動(dòng)畫路由
flutter中有默認(rèn)的Route組件,叫做MaterialPageRoute,但是MaterialPageRoute太普通了,如果我們想要做點(diǎn)不同的跳轉(zhuǎn)特效應(yīng)該如何處理呢?一起來看看吧2023-04-04讓Android應(yīng)用不被殺死(killer)的方法
這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實(shí)現(xiàn)方法和原理分析,需要的朋友可以參考下2015-04-04