1

很多小时以来,我都在尝试将值分配给 Expandablelistview 的子项,但结果不理想。

我正在从 SQLite 数据库中获取值。然后我将它们转换为数组字符串。

然后我将这些值放入可扩展列表视图的子项中。这样:

respuestas = new String[charSequenceItems.length][1];
    for (int i = 0; i < charSequenceItems.length; i++) {
                    //The group is 0 and childs are many as charSequenceItems length
        respuestas[0][i] = charSequenceItems[i];
    }

我得到这个错误:

java.lang.ArrayIndexOutOfBoundsException

但是当我以这种方式这样做时(我将 charSequenceItems 的值放入可扩展列表组而不是孩子):

respuestas = new String[charSequenceItems.length][1];
    for (int i = 0; i < charSequenceItems.length; i++) {
                    //The values of charSequenceItems are assigned to groups and not childs.
        respuestas[i][0] = charSequenceItems[i];
    }

它可以正常工作。

我已经尝试了很多将价值观赋予孩子的组合,但没有一个奏效。

我究竟做错了什么?

提前致谢。

这是我的全部代码:

public class Resumen extends ExpandableListActivity {
String[] charSequenceItems;
private MyExpandableListAdapter mAdapter;
private String[] preguntas = { "Preguntas contrato", "Especies detectadas",
        "Medidas estructurales", "Medidas higiénico sanitarias",
        "Medidas sobre hábitos", "Medidas de control directo", "Productos" };
private String productos[];
private String[][] respuestas;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.resumenlista);

    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);

        charSequenceItems = uGraduateNamesList
                .toArray(new String[uGraduateNamesList.size()]);

    }

    cursor.close();
    sqliteDatabase.close();

    respuestas = new String[charSequenceItems.length][1];
    for (int i = 0; i < charSequenceItems.length; i++) {
        respuestas[3][0] = charSequenceItems[1];
    }



    // Set up our adapter
    mAdapter = new MyExpandableListAdapter();
    setListAdapter(mAdapter);
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    public Object getChild(int groupPosition, int childPosition) {
        return respuestas[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return respuestas[groupPosition].length;
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        TextView textView = new TextView(Resumen.this);

        textView.setLayoutParams(lp);

        textView.setPadding(60, 5, 0, 5);
        textView.setTextAppearance(getBaseContext(), R.style.TitleText);
        return textView;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        // Including a custom view for the child image and text
        View inflatedView = View.inflate(getApplicationContext(),
                R.layout.nombrefilaresumen, null);
        inflatedView.setPadding(50, 0, 0, 0);
        TextView textView = (TextView) inflatedView
                .findViewById(R.id.textView1);

        // lets set up a custom font

        textView.setTextSize(17);
        textView.setText(getChild(groupPosition, childPosition).toString());
        return inflatedView;
    }

    public Object getGroup(int groupPosition) {
        return preguntas[groupPosition];
    }

    public int getGroupCount() {
        return preguntas.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

}
}
4

0 回答 0