0

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

我正在手动编写组列表数据,因为它们不会被更改。但是子列表数据将从我的 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;

}
}

在这里我找到了这段代码。

谢谢你的帮助。

4

1 回答 1

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对组游标的调用......它为每个不同的分组提供了一个记录,然后您使用它来获取孩子。

于 2012-07-12T10:52:45.323 回答