在android中使用arraylist的值存储到expandablelistview的字符串数组中时出错

时间:2014-01-02 05:54:23

标签: android arraylist expandablelistview

我正在尝试expandablelistview,如api demo可扩展列表视图示例1中所示,使用字符串数组,这里我面临将数组列表转换为字符串数组的问题,因为我的所有值都保存在arraylist中,值被添加到asynctask的数组列表中, 并且数组列表的值在我的适配器类中检索,该类扩展了BaseExpandableListAdapter。

这是我的代码

1)数组列表解除,

public final ArrayList<ListItemReminderSummary> listItemList = new ArrayList<ListItemReminderSummary>();
    public final ArrayList<ListItemReminderSummaryList> listItemListSummary = new ArrayList<ListItemReminderSummaryList>();

2)在doinbackground()

中向数组列表中添加值
ListItemReminderSummary listItem = new ListItemReminderSummary();
            listItem.Car_Id = Vehicle_Id;
            listItem.Car_Type = Car_Type;
            listItemList.add(listItem);


            ListItemReminderSummaryList SummaryList = new ListItemReminderSummaryList();
            SummaryList.SummaryReminder_Id = Reminder_Id;
            SummaryList.SummaryReminder_Type = Reminder_Type;

            listItemListSummary.add(SummaryList);

 @Override
    protected void onPostExecute(String result)
    {

         mAdapter = new SlowAdapter(this);
         ExpandableListView.setAdapter(mAdapter);

        dialog.dismiss();
    }

这是我的适配器类

public class SlowAdapter extends BaseExpandableListAdapter {
        private LayoutInflater mInflater;
        private LoadData mContext;


         private String[] groups=(String[]) listItemList.toArray(new String[listItemList.size()]);
         private String[][] children = (String[][]) listItemListSummary.toArray(new String[listItemListSummary.size()][]);


        public SlowAdapter(LoadData loadData) {
            mContext = loadData;
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
             return children[groupPosition][childPosition];
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
             return childPosition;
        }

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

            TextView textView = new TextView(ReminderSummary.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }
        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
              TextView textView = getGenericView();
                textView.setText(getChild(groupPosition, childPosition).toString());
                return textView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
             return children[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
             return groups[groupPosition];
        }

        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
             return groups.length;
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
             return groupPosition;
        }

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

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }

    }

logcat

java.lang.ArrayStoreException: source[0] of type com.example.expandablelist.ListItemReminderSummary cannot be stored in destination array of type java.lang.String[]

所以如何将mt arraylist转换为字符串数组,或者我必须更改我的代码以使用可扩展的listview,

这个arraylist工作让我在列表视图中显示值我想让这个arraylist工作也可以扩展listview,

所以请帮助我,完成这件事 谢谢......

2 个答案:

答案 0 :(得分:1)

而不是管理Array,如何管理ArrayList本身!

尝试:

private List<String> listHeader; // header titles
private HashMap<String, List<String>> listData;

答案 1 :(得分:1)

从ArrayList转换为字符串数组.toArray(方法就足够了。但请注意,在您的代码中,您的ArrayList中包含ListItemReminderSummaryListListItemReminderSummary的对象。所以当然它会导致异常。

并且通过您的代码我可以得出结论,没有必要将ArrayList转换为字符串。只需直接使用它们就像

一样
listItemList.get(position).SummaryReminder_Id

希望这有帮助!

删除课程并直接将传入的数据存储为以下格式:

        ArrayList<String> parent=new ArrayList<String>();
    ArrayList<String[][]> child=new ArrayList<String[][]>();

或类似此格式的内容。