If you want to get a Cursor and no database returns a Cursor, you can use MatrixCursor to return a Cursor.
For more information, see the following section:
Assume that there is a database table structure as follows:
_ Id |
Name |
Price |
R. drawable.Ic_launcher |
Zhangsan |
39 |
R. drawable.Ic_launcher |
Lisi |
40 |
R. drawable.Ic_launcher |
Wangwu |
41 |
R. drawable.Ic_launcher |
Zhaoliu |
42 |
Now let's use the MatrixCursor to create a table structure like above. The MatrixCursor can be fully understood through an example below !! (Original link: http://blog.csdn.net/yelangjueqi/article/details/21232659 welcome reprint, respect for others' labor results, reprint please indicate the source !!) The following is an example:
package com.test.matrixcursor;import android.app.ListActivity;import android.database.MatrixCursor;import android.os.Bundle;import android.support.v4.widget.SimpleCursorAdapter;import android.view.View;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends ListActivity { private static final String[] COLUMN_NAME = { "_id", "name", "price" }; private MatrixCursor matrixCursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); createList(); //createList2(); } private void createList() { matrixCursor = new MatrixCursor(COLUMN_NAME, 1); startManagingCursor(matrixCursor); matrixCursor.addRow(new Object[] { R.drawable.ic_launcher, "zhangsan", 39 }); matrixCursor .addRow(new Object[] { R.drawable.ic_launcher, "lisi", 40 }); matrixCursor .addRow(new Object[] { R.drawable.ic_launcher, "wangwu", 41 }); matrixCursor.addRow(new Object[] { R.drawable.ic_launcher, "zhaoliu", 42 }); matrixCursor .addRow(new Object[] { R.drawable.ic_launcher, "sunqi", 43 }); setListAdapter(new SimpleCursorAdapter(this, R.layout.activity_main, matrixCursor, COLUMN_NAME, new int[] { R.id.icon, R.id.name, R.id.price })); } private void createList2() { matrixCursor = new MatrixCursor(COLUMN_NAME, 1); startManagingCursor(matrixCursor); MatrixCursor.RowBuilder builder1 = matrixCursor.newRow(); builder1.add(R.drawable.ic_launcher); builder1.add("zhangsan"); builder1.add(39); MatrixCursor.RowBuilder builder2 = matrixCursor.newRow(); builder2.add(R.drawable.ic_launcher); builder2.add("lisi"); builder2.add(40); MatrixCursor.RowBuilder builder3 = matrixCursor.newRow(); builder3.add(R.drawable.ic_launcher); builder3.add("wangwu"); builder3.add(41); setListAdapter(new SimpleCursorAdapter(this, R.layout.activity_main, matrixCursor, COLUMN_NAME, new int[] { R.id.icon, R.id.name, R.id.price })); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); matrixCursor.moveToPosition(position); StringBuilder builder = new StringBuilder(); builder.append("Name:") .append(matrixCursor.getString(matrixCursor .getColumnIndex("name"))).append("\n"); builder.append("Price:") .append(matrixCursor.getString(matrixCursor .getColumnIndex("price"))).append("\n"); Toast.makeText(getApplicationContext(), builder.toString(), 1000) .show(); }}
The implementation steps only take three steps. The following describes the steps through character Arrays: Step 1. create a character array, and the value of the character array corresponds to the table field, as follows: String [] COLUMN_NAME = {"_ id", "name", "price "}; step 2. use the MatrixCursor construction method to construct a MatrixCursor. The input parameter is the field array created in step 1, as shown below: MatrixCursormatrixCursor = newMatrixCursor (COLUMN_NAME); you can also specify the initial size, such: matrixCursor = newMatrixCursor (COLUMN_NAME, 10 );
Step 3. adding a row of values through the addRow method of matrixCursor is equivalent to inserting a record into the database, as shown below: matrixCursor. addRow (new Object [] {R. drawable. ic_launcher, "zhangsan", 39 });
Note: Step 3 can also be achieved by constructing a MatrixCursor. RowBuilder, which is equivalent to inserting a record into the database, as shown below: MatrixCursor. RowBuilder builder1 = matrixCursor. newRow ();
Builder1.add (R. drawable. ic_launcher );
Builder1.add ("zhangsan ");
Builder1.add (39 );
// Builder1.add (399999); The MatrixCursor construction can be completed in the preceding three steps. The process of retrieving data from MatrixCursor is the same as that of Cursor!
Note: If the number of add objects in builder1 is greater than the number of fields, that is, builder1.add (399999) is enabled. Note that the following error is reported:
02-27 19:00:44. 882: W/dalvikvm (29036): threadid = 1: thread exiting with uncaught exception (group = 0x41e689a8)
02-27 19:00:44. 882: W/dalvikvm (29036): threadid = 1: uncaught exception occurred
02-27 19:00:44. 883: W/System. err (29036): java. lang. runtimeException: Unable to start activity ComponentInfo {com. test. matrixcursor/com. test. matrixcursor. mainActivity}: android. database. cursorIndexOutOfBoundsException: No more columns left.
02-27 19:00:44. 883: W/System. err (29036): at android. app. ActivityThread. initialize mlaunchactivity (ActivityThread. java: 2356)
02-27 19:00:44. 883: W/System. err (29036): at android. app. ActivityThread. handleLaunchActivity (ActivityThread. java: 2408)
02-27 19:00:44. 883: W/System. err (29036): at android. app. ActivityThread. access $600 (ActivityThread. java: 167)
02-27 19:00:44. 883: W/System. err (29036): at android. app. ActivityThread $ H. handleMessage (ActivityThread. java: 1378)
02-27 19:00:44. 883: W/System. err (29036): at android. OS. Handler. dispatchMessage (Handler. java: 107)
02-27 19:00:44. 883: W/System. err (29036): at android. OS. low.loop (low.java: 194)
02-27 19:00:44. 884: W/System. err (29036): at android. app. ActivityThread. main (ActivityThread. java: 5405)
02-27 19:00:44. 884: W/System. err (29036): at java. lang. reflect. Method. invokeNative (Native Method)
02-27 19:00:44. 884: W/System. err (29036): at java. lang. reflect. Method. invoke (Method. java: 525)
02-27 19:00:44. 884: W/System. err (29036): at com. android. internal. OS. ZygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 838)
02-27 19:00:44. 884: W/System. err (29036): at com. android. internal. OS. ZygoteInit. main (ZygoteInit. java: 605)
02-27 19:00:44. 884: W/System. err (29036): at dalvik. system. NativeStart. main (Native Method)
02-27 19:00:44. 886: W/System. err (29036): Caused by: android. database. CursorIndexOutOfBoundsException: No more columns left.
02-27 19:00:44. 887: W/System. err (29036): at android. database. MatrixCursor $ RowBuilder. add (MatrixCursor. java: 206)
02-27 19:00:44. 887: W/System. err (29036): at com. test. matrixcursor. MainActivity. createList (MainActivity. java: 46)
02-27 19:00:44. 888: W/System. err (29036): at com. test. matrixcursor. MainActivity. onCreate (MainActivity. java: 20)
02-27 19:00:44. 889: W/System. err (29036): at android. app. Activity. Wait mcreate (Activity. java: 5127)
02-27 19:00:44. 889: W/System. err (29036): at android. app. Instrumentation. callActivityOnCreate (Instrumentation. java: 1151)
02-27 19:00:44. 890: W/System. err (29036): at android. app. ActivityThread. initialize mlaunchactivity (ActivityThread. java: 2320) running result: