2

I have made a MatrixCursor for accessing my data. It has 4 columns. And I have a ListView in which each row have 3 elements (one of them is ImageView. So I only actually need 2 columns ). This is my first time with cursors so which adapter is best for doing this? This is my work so far.

MatrixCursor:

static MatrixCursor getnameList() { ArrayList<String> fsitem = getfsiList(); String[] columnNames = {"id", "name", "info","icon"}; MatrixCursor cursor = new MatrixCursor(columnNames); for (int i = 0; i < fsitem.size(); i++) { try { File root = new File(Environment.getExternalStorageDirectory() .getName() + "/" + fsitem.get(i)); if (root.canRead()) { File namefile = new File(root, ".name"); FileReader namereader = new FileReader(namefile); BufferedReader in = new BufferedReader(namereader); String id = in.readLine(); String name = in.readLine(); String info = in.readLine(); String[] fsii = new String[4]; fsii[0]= id; fsii[1]= name; fsii[2]= info; fsii[3]= null; cursor.addRow(fsii); } } catch (IOException e) { Log.e("NameManager.java : ", ("Error!! Not Writable!!" + Environment.getExternalStorageDirectory().getName() + "/" + fsitem.get(i))); } } Log.d("NameManager.getnameList()",cursor.toString()); return cursor; } 

row.xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="8dp" > <ImageView android:id="@+id/icon" android:layout_width="22dp" android:layout_height="22dp" android:layout_marginLeft="4dp" android:layout_marginRight="10dp" android:layout_marginTop="4dp" android:src="@drawable/list_icon" > </ImageView> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" android:layout_marginLeft="38dp" android:textStyle="bold" /> <TextView android:id="@+id/Info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/info" android:textSize="16dp" android:textStyle="bold" /> </RelativeLayout> 

2 Answers 2

5

Use the SimpleCursorAdapter. You will need a String array with the columns you want to put in the list, a int array with the ids of the TextView from the layout on which your adapter will bind the data from the columns. Also in order to use the cursor with the ListView the cursor must have a column _id that you add to the query:

String[] from = {BaseColumns._ID, "col1", "col2"}; int[] to = {R.id.Text1, R.id.text2} SimpleCursorAdapter adap = new SimpleCursorAdapter(this, R.layout.the_row_layout, curosrObject, from, to); 

Edit: To implement a _id column in your MatrixCursor you could make a field in the class where you create the cursor:

private static int key = 0; 

Then add another column in your MatrixCursor named BaseColumns._ID. When you will add a new row in the MatrixCursor add a new Object array instead of a String array, like this:

Object[] fsii = new Object[4]; fsii[0]= key; fsii[1]= name; fsii[2]= id; fsii[3]= info; cursor.addRow(fsii); key++; // the _id must have unique values so increment the key value; 

Then use the cursor like above(see small edit).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy, But as you can see my Cursor don't have a _id column. How do I fix that?
0
public class matrixCursor extends ListActivity { private static int key = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); fillData(); } private void fillData() { MatrixCursor matrixCursor = new MatrixCursor(new String[] { "_id","colName1","colName2" }); String name="sethu"; int id=1; String info="something"; for (int i = 0; i < 4; i++){ matrixCursor.addRow(new Object[] { key,name, info}); key++; } String[] columnNames = {"_id","colName1","colName2" }; int[] to = {R.id.Text1, R.id.Text2, R.id.Text3}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.the_row_layout, matrixCursor, columnNames, to); setListAdapter(adapter); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.