java - 如何从 Matrix 光标填充 ListView

标签 java android

我制作了一个MatrixCursor来访问我的数据。它有 4 列。我有一个 ListView,其中每行有 3 个元素(其中一个是 ImageView。所以我实际上只需要 2 列)。这是我第一次使用光标,那么哪个适配器最适合执行此操作?这是我迄今为止的工作。

矩阵光标:

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>

最佳答案

使用SimpleCursorAdapter 。 您将需要一个 String 数组,其中包含要放入列表中的列,一个 int 数组,其中包含布局中的 TextView 的 id,适配器将在该数组上绑定(bind)列中的数据。此外,为了将光标与 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);

编辑: 要在 MatrixCursor 中实现 _id 列,您可以在创建光标的类中创建一个字段:

private static int key = 0;

然后在 MatrixCursor 中添加另一列,名为 BaseColumns._ID。当您在 MatrixCursor 中添加新行时,请添加一个新的 Object 数组而不是 String 数组,如下所示:

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;

然后像上面一样使用光标(参见小编辑)。

关于java - 如何从 Matrix 光标填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9435158/

相关文章:

java - 当我按下按钮时,Java Clip不会停止并继续播放.wav文件

c# - 构造函数链接的目的?

java - 如何使用 Java Runtime.exec() 和 Windows REG 实用程序来读取/更新/删除 HKEY_LOCAL_MACHINE\...\CurrentVersion\Run 中的条目?

java - 一些网站图片没有显示在android的webview中

java - 如何在android Firestore的文档字段中保存文档ID?

java - 如何在不使用Java类的情况下实现PriorityQueue?

java -/bin 在 JDK 中丢失

java - 如何修复 Android Studio 中的 ChannelException?

java - android volley json解析器错误

Android HttpURLConnection 无法重定向到 "market://details?id=my.package.name"