我已经使用MatrixCursor
来访问我的数据。它有4列。我有一个ListView
,其中每行有3个元素(其中一个是ImageView
。所以我实际上只需要2列)。这是我第一次使用游标,哪个适配器最适合这样做?到目前为止,这是我的工作。
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>
答案 0 :(得分:5)
使用SimpleCursorAdapter
。
您将需要一个String数组,其中包含要放入列表中的列,一个int数组,其中包含来自布局的TextView
的id,适配器将在该布局中绑定列中的数据。另外,为了将光标与ListView
一起使用,光标必须包含您添加到查询中的列_id
:
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);
修改强>
要在_id
中实现MatrixCursor
列,您可以在创建光标的类中创建一个字段:
private static int key = 0;
然后在名为MatrixCursor
的{{1}}中添加另一列。当您在BaseColumns._ID
中添加新行时,添加新的MatrixCursor
数组而不是Object
数组,如下所示:
String
然后像上面一样使用光标(参见小编辑)。
答案 1 :(得分: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);
}
}