Android可扩展列表从字符串数组中放入子项

时间:2012-07-11 14:13:43

标签: android expandablelistview arrays

我正在尝试在Android中制作可扩展列表。但是我将这些值放在子列表中有一个问题。

组列表数据我手动编写,因为它们不会被更改。但子列表数据将从我的SQLite数据库生成(takeItemsFromDatabase()方法从数据库获取数据并将其放入“product”数组字符串)。

所以数组String“Product”得到了所有项目。

我的问题就在这里开始。当我试图将数组字符串“product”放入子列表时,这样:

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 个答案:

答案 0 :(得分:0)

SimpleCursorAdapter示例:

我的dbhelper为可扩展列表调用了光标:

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文件):

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 ...它为每个不同的分组提供了一条记录,然后您使用它来带孩子们。