我们在使用ListView的时候需要和数据进行绑定,那么问题来了,如何获取SQLite数据库中的数据并动态的显示到ListView当中呢?其实过程很简单:首先要获取SQLite数据(当然首先你要创建一个SQLite数据库并填写了一些数据),然后引入ListView控件,最后将数据和ListView绑定就好了。
一 获取SQLite数据库中的数据
SQLite是一个轻量级的数据库,它能将数据保存到你的手机,但缺点是一旦软件卸载所有数据将一同被销毁。所以要根据自己的项目需要选择性的使用。下面要演示将SQLite中的数据提取出来。
首先定义一个类用来实例化数据库
1
2
3
4
5
6
7
8
9
10
|
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中对应表的数据
1
2
3
4
5
6
7
8
9
10
11
|
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
1
2
3
4
|
public static Bitmap base64ToBitmap(String base64info) { byte [] bytes = Base64.decode(base64info, Base64.DEFAULT); return BitmapFactory.decodeByteArray(bytes, 0 , bytes.length); } |
二 引入ListView控件
ListView的引入是比较简单的,我们可以直接将ListView控件拖拽到xml文件中即可。这里不过多介绍
1
2
3
4
5
|
<ListView android:id= "@+id/lv_expense" style= "@style/Animation.AppCompat.DropDownUp" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> |
三 将数据和ListView绑定
首先将获取到的数据通过一个循环存放到map对象中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?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()
这个接口,在里面定义数据和视图的匹配关系 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/liyue_199609130/article/details/105100715