带有标题和合并适配器的可点击ListView使应用程序onListItemClick崩溃

时间:2012-06-28 11:26:05

标签: android android-listview commonsware-cwac

我在一个活动中遇到问题,该活动返回一个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)

2 个答案:

答案 0 :(得分:1)

嗯,解决方案变得与众不同,简单得多。在if语句中,我必须更改:if (position < this.firstAdapter.getCount())到以下内容: if ((position < this.firstAdapter.getCount() && position >= 0))

通过这种方式,我将确保不会出现负数,无论我有多少标头,索引总是正确的。

答案 1 :(得分:0)

我认为else中需要一些returnonListItemClick()逻辑。

假设position1firstAdapter.getCount()返回383secondAdapter.getCount()返回27。您将浏览第一个if块(383大于0),从position(现在为0)中减去一个并进行处理它。然后,您将浏览第二个块(27大于0),从position(现在为-1)中减去一个,然后您将尝试崩溃获得-1元素。由于position只标识了一行,因此它只能位于其中一个适配器中,而您现有的代码不能处理这种情况。