Android(安卓)MatrixCursor的详解
16lz
2021-01-26
ContentProvider对外共享数据的时候的query()方法是需要一个cursor的,
但是如果没有数据库,而项目又需要从ContentProvider读取数据的时候怎么办?
更糟糕的是其他方法操作也都是需要cursor的。
此时就需要MatrixCursor了,它相当于为你模拟了一个表。
废话不多说直接贴代码:
public class List1 extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); String[] menuCols = new String[] {"_id", "item", "price" }; int[] to = new int[] { R.id.icon, R.id.item, R.id.price }; MatrixCursor menuCursor = new MatrixCursor(menuCols); startManagingCursor(menuCursor); menuCursor.addRow(new Object[] { R.drawable.icon, "Chicken Sandwich", "$3.99" }); ListAdapter menuItems = new SimpleCursorAdapter( this, R.layout.menu_row, menuCursor, menuCols, to); setListAdapter(menuItems); }}<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <TextView android:id="@+id/item" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView></LinearLayout>实现的效果 图片 Chicken Sandwich $3.99
更多相关文章
- SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
- 一句话锁定MySQL数据占用元凶
- Android使用getIdentifier()方法根据资源名来获取资源id
- android 利用java反射调用系统类的隐藏方法
- Android入门之四大组件
- Android(安卓)Studio 新建项目的R文件丢失的解决方法
- Android(安卓)View (4) View的绘制过程
- 二、Android(安卓)NDK编程预备之Java jni入门Hello World
- Android获取app应用程序大小的方法