你好我有一个简单的可扩展列表活动,直到我改变了这个:List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
为此:List<Map<String, Image>> groupData = new ArrayList<Map<String, Image>>();
我想当你在Simple可扩展列表适配器的构造函数中设置TextView项时textview期望获得字符串而不是对象。有没有办法解决这个问题?或者我必须陷入困境?这是一些代码:
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 = (ExpandableListView)findViewById(R.id.list);
lv.setAdapter(mAdapter);
}
}
所以最后一个问题是:我是否必须实现自己的简单可扩展列表适配器才能自己设置文本?
编辑:以下是错误的跟踪:
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)
第二次编辑它几乎正常工作但listItems即使在那里也不可见:
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());
}
}
}
}
答案 0 :(得分:0)
现在它正在运作,但我不喜欢这个解决方案。我创建了一个新类并扩展了BaseExpandableListAdapter,同一个超类SimpleExpandableListAdapter扩展。我不喜欢这个解决方案,如果有更好的解决方案,请告诉我。
感谢。