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>
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
TextViewfrom the layout on which your adapter will bind the data from the columns. Also in order to use the cursor with theListViewthe cursor must have a column_idthat you add to the query:Edit:
To implement a
_idcolumn in yourMatrixCursoryou could make a field in the class where you create the cursor:Then add another column in your
MatrixCursornamedBaseColumns._ID. When you will add a new row in theMatrixCursoradd a newObjectarray instead of aStringarray, like this:Then use the cursor like above(see small edit).