【发布时间】:2016-12-07 01:36:56
【问题描述】:
我正在尝试使用 HashMap 和 SimpleAdapter 从具有 12 列和 12 行的嵌套数组中填充 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);
当我尝试这个时,我得到一个错误
android.widget.SimpleAdapter中的SimpleAdapter(android.content.Context, java.util.List<? extends java.util.Map<java.lang.String,?>>, int, java.lang.String[], int[])' 不能应用于(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 ";
【问题讨论】:
-
首先,你为什么要这样做?使用带有 12 个字段的 Java 类。并将它们加载到
ArrayAdapter -
其次,您应该看到您提供的
array2参数必须是String[],而不是arraylist 或二维数组。 -
最后一个
new String from[252]:不是正确的 java 语法。new String[252]:是。 -
我这样做是因为我需要 array2 中的每一个项目,而不是整个 Arraylist 如果我使用一个类来保存数据,例如具有 12 个字段的 data(),我不会只是能够保存一个字符串?我的意思是我一次只能保存一个字符串
-
我不确定你所说的一个字符串是什么意思。您定义一个自定义 xml 布局并将该对象“绑定”到您想要的任何视图。也许你应该包括你想要的截图或绘图。
标签: android listview multidimensional-array hashmap simpleadapter