开发者

problem showing object in SimpleExpandableActivity android

Hello i have a simple expandable list activity that was working fine until i changed this: List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); for this: List<Map<String, Image>> groupData = new ArrayList<Map<String, Image>>(); i guess that when you set the TextView items in the Simple expandable list adapter's constructor the textview is expecting to get Strings instead of objects. Is there some way to work this arround ? Or do i have to get stuck with strings? Here's some code:

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

        setContentView(R.layout.expandable_list_layout);

        List<User> header = new ArrayList<User>();
        header.add(new User("John"));
        header.add(new User("Smith"));
        header.add(new User("Anderson"));
        header.add(new User("Trinity"));
        header.add(new User("Morfeo"));

        List<String> gretting = new ArrayList<String>();
        gretting.add("Hello");

        List<Map<String, User>> groupData = new ArrayList<Map<String, User>>();
        final List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

        for (User user : header) {
            Map<String, User> curGroupMap = new HashMap<String, User>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, user);

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (String word : gretting) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);

                curChildMap.put(NAME, word);
            }
            childData.add(children);
        }

        //our adapter

        mAdapter = new SimpleExpandableListAdapter(
                getApplicationContext(),
                groupData,
                R.layout.simple_expandable_list_item_1,
                new String[] { NAME },
                new int[] { R.id.header_text1 },
                childData,
                R.layout.simple_expandable_list_item_2,
                new String[] { NAME },
                new int[] { R.id.text1 }
                );

        ExpandableListView lv = 开发者_如何学Go(ExpandableListView)findViewById(R.id.list);
        lv.setAdapter(mAdapter);
    }
}

So the final question is: do i have to implement my own simple expandable list adapter to be able to set the text by myself?

EDIT: Here is the trace of the error:

02-21 08:56:54.128: ERROR/AndroidRuntime(277): java.lang.ClassCastException: se.madcoderz.categoryObject.User
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.SimpleExpandableListAdapter.bindView(SimpleExpandableListAdapter.java:249)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.SimpleExpandableListAdapter.getChildView(SimpleExpandableListAdapter.java:229)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:450)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.ListView.fillDown(ListView.java:652)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.ListView.fillSpecific(ListView.java:1284)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.ListView.layoutChildren(ListView.java:1558)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.view.View.layout(View.java:7035)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.view.View.layout(View.java:7035)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.view.View.layout(View.java:7035)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.view.View.layout(View.java:7035)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.view.View.layout(View.java:7035)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.os.Looper.loop(Looper.java:123)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at java.lang.reflect.Method.invoke(Method.java:521)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-21 08:56:54.128: ERROR/AndroidRuntime(277):     at dalvik.system.NativeStart.main(Native Method)

2nd EDIT It's almost working but the listItems are not visible even if they're there:

public class CategoriesAdapter2 extends SimpleExpandableListAdapter {

    private List<? extends List<? extends Map<String, User>>> mChildData;
    private String[] mChildFrom;
    private int[] mChildTo;

    public CategoriesAdapter2(Context context, List<? extends Map<String, ?>> groupData, int groupLayout,
                              String[] groupFrom, int[] groupTo, List<List<Map<String, User>>> childData,
                              int childLayout, String[] childFrom, int[] childTo) {
        super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);

        mChildData = childData;
        mChildFrom = childFrom;
        mChildTo = childTo;

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

        View v;
        if (convertView == null) {
            v = newChildView(isLastChild, parent);
        } else {
            v = convertView;
        }
        bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom, mChildTo, groupPosition, childPosition);
        return v;
    }

    private void bindView(View view, Map<String, User> data, String[] from, int[] to, int groupPosition, int childPosition) {
        int len = to.length - 1;

        for (int i = 0; i < len; i++) {
            TextView v = (TextView) 
            view.findViewById(to[i]);
            if (v != null) {
                v.setText(data.get(from[i]).getName());
            }
        }
    }
}


Now it's working but i don't like the solution. I made a new class and extended BaseExpandableListAdapter, the same superclass SimpleExpandableListAdapter extends. I don't like this solution, if there's a better solution please let me know.

Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜