2012-07-11 29 views
0

我试图在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我发现这个代码。

感谢您的任何帮助。

+1

如果您从数据库中为孩子提供数据,那么可能组中的数据也在其中(否则,您将如何决定为每个组拉什么?)。既然如此,为什么还要用数组呢?只需扩展SimpleCursorTreeAdapter并使用数据库中的游标来驱动列表。如果你决定添加一个新组,那么这将有利于不必更改与该列表相关的一些代码。 – Barak 2012-07-11 15:03:42

+0

嗨巴拉克,我试过SimpleCursorTreeAdapter的一些例子,但没有人工作。你有没有SimpleCursorTreeAdapter的一些很好的例子? – HaOx 2012-07-12 08:30:14

回答

0

SimpleCursorAdapter例如:

public Cursor fetchGroup(String group) { 
    String query = "SELECT DISTINCT " 
      + group 
      + " AS _id FROM " 
      + LISTITEM_TABLE; 
    return mDb.rawQuery(query, null); 
} 

public Cursor fetchChildren(String column, String token) { 
    String query = "SELECT _id, name, qty, unit FROM items WHERE " 
      + column 
      + "='" 
      + token 
      + "'; 
    return mDb.rawQuery(query, null); 
} 

呼叫转接:

ExpandableListView lv; 
mGroupsCursor = mDbHelper.fetchGroup(group, 
     ListFragment_ShopList.listId); 
getActivity().startManagingCursor(mGroupsCursor); 
mGroupsCursor.moveToFirst(); 

lv = (ExpandableListView) getActivity().findViewById(android.R.id.list); 
lv.setItemsCanFocus(false); 
lv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE); 

mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(), 
     R.layout.rowlayout_expgroup, R.layout.rowlayout_itemlist_exp, 
     new String[] { "_id" }, new int[] { android.R.id.text1 }, 
     new String[] { GroceryDB.ITEM_NAME, GroceryDB.LISTITEM_QTY, 
       GroceryDB.ITEM_UNIT }, new int[] { R.id.ListItem1, 
       R.id.ListItem3, R.id.ListItem2 }); 
lv.setAdapter(mAdapter); 

适配器(在相同的.java文件呼吁

光标从我dbhelper为expandablelist调用它):

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter { 
    public MyExpandableListAdapter(Cursor cursor, Context context, 
      int groupLayout, int childLayout, String[] groupFrom, 
      int[] groupTo, String[] childrenFrom, int[] childrenTo) { 
     super(context, cursor, groupLayout, groupFrom, groupTo, 
       childLayout, childrenFrom, childrenTo); 
    } 
    @Override 
    protected Cursor getChildrenCursor(Cursor groupCursor) { 
     Cursor childCursor = mDbHelper.fetchChildren(GroceryListMain.group, 
       groupCursor.getString(groupCursor.getColumnIndex("_id")), 
       ListFragment_ShopList.listId); 
     getActivity().startManagingCursor(childCursor); 
     childCursor.moveToFirst(); 
     return childCursor; 
    } 
    protected void bindChildView(View view, Context context, Cursor cursor, 
      boolean isLastChild) { 
     TextView name = (TextView) view.findViewById(R.id.ListItem1); 
     TextView qty = (TextView) view.findViewById(R.id.ListItem3); 
     TextView unit = (TextView) view.findViewById(R.id.ListItem2); 
     name.setText(cursor.getString(1)); 
     qty.setText(cursor.getString(2)); 
     unit.setText(cursor.getString(3)); 
    } 
} 

以上所有内容中最大的问题是SELECT DISTINCT columnn as _id在群组游标的调用中......它为每个不同的分组提供了一条记录,然后您可以使用它来获取这些孩子。

相关问题