我试图在Android中创建一个Expandable列表。但是我有一个问题把值放在子列表中。Android Expandable List从字符串数组中放置子项目
我手动编写的组列表数据,因为它们不会被更改。但子列表数据将从我的SQLite数据库中生成(takeItemsFromDatabase()方法正在从数据库获取数据,并将其放到“产品”数组字符串中)。
所以数组字符串“产品”得到所有项目。
我的问题是从这里开始。当我试图把阵列字符串“产品”到孩子列表中,以这样的方式
static final String hijosGrupos[][] = {
// Shades of grey
{product},
// Shades of blue
{ "example 1", "example 2", "etc" }, {}, {}, {}, {}};
它给我这个错误:
"Type mismatch: cannot convert from String[] to String"
我知道为什么它显示我的错误,但我不知道如何解决它。
所以基本上我想把数据从我的数据库的一些列的子列表。
任何人有一些想法?
这里是我的代码:
public class Resumen extends ExpandableListActivity {
String[] preguntasContrato;
String[] especiesDetectadas;
String[] medidasEstructurales;
String[] medidasSobreHabitos;
String[] medidasControlDirecto;
static String[] product;
String Grupos[] = { "Product", "Preguntas contrato", "Others",
"Medidas 1", "Medidas 2",
"Medidas 3", "Medidas 4" };
//Here are values of child lists
static final String hijosGrupos[][] = {
// Shades of grey
{},
// Shades of blue
{ "example 1", "example 2", "etc" }, {}, {}, {}, {}};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.resumenlista);
Log.i("Prueba", "Prrr");
cogerProductosSeleccionados();
@SuppressWarnings("unchecked")
SimpleExpandableListAdapterWithEmptyGroups expListAdapter = new SimpleExpandableListAdapterWithEmptyGroups(
this, createGroupList(), R.layout.nombrefilaresumen,
new String[] { "colorName" }, new int[] { R.id.groupname },
createChildList(), R.layout.filahijoresumen, new String[] {
"shadeName", "rgb" }, new int[] { R.id.childname,
R.id.rgb });
setListAdapter(expListAdapter);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private List createGroupList() {
ArrayList result = new ArrayList();
for (int i = 0; i < Grupos.length; ++i) {
HashMap m = new HashMap();
m.put("colorName", Grupos[i]);
result.add(m);
}
return (List) result;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private List createChildList() {
ArrayList result = new ArrayList();
for (int i = 0; i < hijosGrupos.length; ++i) {
ArrayList secList = new ArrayList();
for (int n = 0; n < hijosGrupos[i].length; n += 2) {
HashMap child = new HashMap();
child.put("shadeName", hijosGrupos[i][n]);
child.put("rgb", hijosGrupos[i][n + 1]);
secList.add(child);
}
result.add(secList);
}
return result;
}
public void takeItemsFromDatabase() {
List<String> uGraduateNamesList = new ArrayList<String>();
AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query("ProductosTemporal", null, null,
null, null, null, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
String descripcionproducto = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.descripcionproducto));
uGraduateNamesList.add(descripcionproducto);
final String[] itemsInArrayString = uGraduateNamesList
.toArray(new String[uGraduateNamesList.size()]);
Items(itemsInArrayString);
}
cursor.close();
sqliteDatabase.close();
}
public void Items(String[] itemDesc) {
product = itemDesc;
}
}
Here我发现这个代码。
感谢您的任何帮助。
如果您从数据库中为孩子提供数据,那么可能组中的数据也在其中(否则,您将如何决定为每个组拉什么?)。既然如此,为什么还要用数组呢?只需扩展SimpleCursorTreeAdapter并使用数据库中的游标来驱动列表。如果你决定添加一个新组,那么这将有利于不必更改与该列表相关的一些代码。 – Barak 2012-07-11 15:03:42
嗨巴拉克,我试过SimpleCursorTreeAdapter的一些例子,但没有人工作。你有没有SimpleCursorTreeAdapter的一些很好的例子? – HaOx 2012-07-12 08:30:14