我在返回 ListView 或几个 ListView 的组合的活动中遇到问题,该活动与 Commonsware 的 MergeAdapter 合并。它是一种搜索活动,基于搜索查询将返回来自 1 个或多个不同类别的结果。当类别超过1时,我插入一个标题,这是另一个只有一行的ListView - 类别的名称。
一些代码:
public class FirstAdapter extends ArrayAdapter<Address> {
public FirstAdapter (Context context, int resource,
int textViewResourceId, List<Address> objects) {
super(context, resource, textViewResourceId, objects);
}
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
Address location = this.getItem(position);
TextView textView = (TextView) view.findViewById(R.id.SearchResultTextAddress);
textView.setText(location.address);
return view;
}
}
然后我有一个标头适配器:
public class HeaderAdapter extends ArrayAdapter<String> {
private String header;
public HeaderAdapter(Context context, int resource,
int textViewResourceId, String header) {
super(context, resource, textViewResourceId, new ArrayList<String>());
this.header = header;
}
public void setVisible(boolean visible) {
if (visible) show();
else hide();
}
public void show() {
clear();
add(this.header);
}
public void hide() {
clear();
}
public View getView (int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
@Override
public boolean isEnabled(int position) {
return false;
}
}
如您所见,仅在必要时才会显示。在我的活动中,我有一个 onListItemClick 被覆盖:
@Override
protected void onListItemClick(ListView listview, View view, int position, long id) {
super.onListItemClick(listview, view, position, id);
if (this.firstAdapter.getCount() > 0) {
position--;
if (position < this.firstAdapter.getCount()) onRepClick(this.firstAdapter.getItem(position));
position = position - this.firstAdapter.getCount();
}
if (this.secondAdapter.getCount() > 0) {
position--;
if (position < this.secondAdapter.getCount()) onAusschussClick(this.secondHeader.getItem(position));
position = position - this.secondAdapter.getCount();
}
因此,当我只有一个适配器中的一个类别时,一切正常。当我得到其中两个结果的那一刻,我得到了一个 ArrayIndexOutOfBoundsException。我认为我获得职位的逻辑是不正确的。任何想法将不胜感激,谢谢。
这是堆栈跟踪:
06-28 12:56:54.291: E/AndroidRuntime(3963): FATAL EXCEPTION: main
06-28 12:56:54.291: E/AndroidRuntime(3963): java.lang.ArrayIndexOutOfBoundsException
06-28 12:56:54.291: E/AndroidRuntime(3963): at java.util.ArrayList.get(ArrayList.java:313)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
06-28 12:56:54.291: E/AndroidRuntime(3963): at com.jasamer.callarep.SearchActivity.onListItemClick(SearchActivity.java:136)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.widget.ListView.performItemClick(ListView.java:3513)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.os.Handler.handleCallback(Handler.java:587)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.os.Looper.loop(Looper.java:130)
06-28 12:56:54.291: E/AndroidRuntime(3963): at android.app.ActivityThread.main(ActivityThread.java:3835)
06-28 12:56:54.291: E/AndroidRuntime(3963): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 12:56:54.291: E/AndroidRuntime(3963): at java.lang.reflect.Method.invoke(Method.java:507)
06-28 12:56:54.291: E/AndroidRuntime(3963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
06-28 12:56:54.291: E/AndroidRuntime(3963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-28 12:56:54.291: E/AndroidRuntime(3963): at dalvik.system.NativeStart.main(Native Method)