我可以使用 Simpleadapter 和 hashmap 使用嵌套数组填充 listView 吗?

Can I populate a listView with a nested array using Simpleadapter and hashmap?

我正在尝试使用 HashMap 和来自具有 12 列和 12 行的嵌套数组的 SimpleAdapter 来填充 ListView

这是我的代码

try {
    String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];
    cursor.moveToFirst();
    for (int i =0; i<cursor.getCount();i++){
        for(int j = 0; j < cursor.getColumnNames().length; j++) {
            String uname = cursor.getString(j);
            array2[i][j]=uname;
        }
        cursor.moveToNext();
    }
} finally {
    cursor.close();
}

然后是带有 hashmap 的简单适配器

int[] to = new int[]{R.id.item2, R.id.item3, R.id.item4, R.id.item5, R.id.item6, R.id.item7, R.id.item8 ,R.id.item9, R.id.item10, R.id.item11, R.id.item12};
List<HashMap<String,String>> fillMaps = (List<HashMap<String, String>>) new ArrayList<HashMap<String, String>>();

for(int i = 0; i<10;i++){
    HashMap<String,ArrayList> map = new HashMap<String, ArrayList>();

}

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, array2, to);

当我尝试此操作时出现错误

SimpleAdapter(android.content.Context, java.util.List<? extends java.util.Map<java.lang.String,?>>, int, java.lang.String[], int[])' in android.widget.SimpleAdapter cannot be applied to (com.example.thevenom1215.prueba.MainActivity, java.util.List<java.util.HashMap<java.lang.String, java.lang.String>>, int, java.util.ArrayList<java.lang.String>, int[])

或者我认为我可以将嵌套数组转换为简单数组,这可能吗?如果是这样,我怎样才能做到这一点?我试过这个

String from [] = new String from[252]:
from[0]= array[0][1]+"|"+array[0][2]+"|"+array[0][3]+......+array[0][12];

但它不起作用。

array2中,即array2[12][21],每行12列,是一个人的信息,例如(name, age)

Cesar Joel Gurrola, xx

数组的第一行是:[Cesar, Joel, Gurrola, xx]

我需要在一个数组中,因为在我的代码中我需要 String by String 而不是整个字符串 "Cesar, Joel, Gurrola, xx"

Sql查询

sql="select b.CVE_CONTR, r.NO_RECIBO , a.NOM_SOLICIT ,r.NO_PARCIAL ,r.SDO_TOTAL, r.STS_PAGO ,a.AP_PATSOLICIT,a.AP_MATSOLICIT, " +
                            "f.DESCRIPCION, b.NO_PARCIALID , b.PAGO_PARCIAL, b.TOT_APAG from MFRF_M_SOLICITANTES a, MFRF_M_CONTPREV_H b, MFRF_M_CONTPREV_D_RECGEN_2 r," +
                            "C_PAQUETE f , C_PARCIALIDADES g, MFRF_C_COLONIAS c where b.CVE_CONTR = '"+etnumcontrato.getText().toString() + "' and r.STS_PAGO in ('1','10','11','12')" +
                            "and c.ID_COLONIA = a.ID_COLONIA  and f.ID_PAQUETE = b.ID_PAQUETE and g.ID_PARCIALIDAD = b.ID_PARCIAL AND a.ID_SOLICIT = b.ID_SOLICITANTE  ";

总的来说,我不推荐这个。

String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];

适配器一次显示 一个 行项目,而不是整个 table。


我建议使用 CursorAdapter,因为您确实有一个 Cursor 对象,但我将继续使用 SimpleAdapter

首先,您需要创建一个XML布局,将其命名为item_layout.xml。它包含您要将数据库中的行绑定到的所有视图。我相信这些都必须是 SimpleAdapter 的 TextView。

String[] from = new String[] {"rowid", "fname", "mname", "lname"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };

然后,遍历游标的行,并将必要的信息提取到一个HashMap 列表中。注意:这是我们每次循环游标结果时的 一行 数据。而且,如前所述,您只有 TextView,因此只能存储 String 值。

List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
final int rows = cursor.getCount();
for (int i = 0; i < rows; i++) {
    HashMap<String, String> map = new HashMap<String, String>();

    map.put("rowid", "" + i);
    map.put("fname", cursor.getString(cursor.getColumnIndexOrThrow("fname"));
    map.put("mname", cursor.getString(cursor.getColumnIndexOrThrow("mname"));
    map.put("lname", cursor.getString(cursor.getColumnIndexOrThrow("lname"));

    fillMaps.add(map);
    cursor.moveToNext();
}

现在您已经遍历了所有数据行并且对该数据感到满意,您只需设置适配器。

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, 
        R.layout.item_layout, 
        from, to);
listView.setAdapter(adapter);