【问题标题】:OutOfIndex crash when onListItemClick called调用 onListItemClick 时 OutOfIndex 崩溃
【发布时间】:2013-10-20 13:58:08
【问题描述】:

这是我的列表适配器:

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MobileArrayAdapter extends ArrayAdapter<List<item>> {
    private final Context context;
    private final List<item> markers;
    private final static String[] a={"s"};

    public MobileArrayAdapter(Context context, List<item> markers) {
        super(context, R.layout.list_item);
        this.context = context;
        this.markers = markers;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView title = (TextView) rowView.findViewById(R.id.title);
        ProgressBar loader = (ProgressBar) rowView.findViewById(R.id.loader);
        ImageView image = (ImageView) rowView.findViewById(R.id.itemimgae);
        TextView views = (TextView) rowView.findViewById(R.id.views);
        TextView likes=(TextView) rowView.findViewById(R.id.likes);
        TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
        TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
        TextView desc=(TextView) rowView.findViewById(R.id.desc);
        TextView pub =(TextView) rowView.findViewById(R.id.pub);
        TextView idnum =(TextView) rowView.findViewById(R.id.idnum);

        title.setText("      "+markers.get(position).getTitle());
        views.setText(markers.get(position).getView()+"");
        likes.setText(markers.get(position).getLike()+"");
        upvote.setText(markers.get(position).getUpvote()+"");
        downvote.setText(markers.get(position).getDownvote()+"");
        desc.setText(markers.get(position).getDesc());
        pub.setText(markers.get(position).getPub()+"");
        idnum.setText(markers.get(position).getId()+"");

        return rowView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return (this.markers != null) ? this.markers.size() : 0;

    }


}

当我单击 listView 的一项时,应用程序崩溃..

LogCat:

10-12 18:20:49.791: W/dalvikvm(3450): threadid=1: thread exiting with uncaught exception (group=0x4107b930)
10-12 18:20:49.801: E/AndroidRuntime(3450): FATAL EXCEPTION: main
10-12 18:20:49.801: E/AndroidRuntime(3450): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.util.ArrayList.get(ArrayList.java:304)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at com.example.free.ListActivity.onListItemClick(ListActivity.java:94)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.os.Handler.handleCallback(Handler.java:725)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.os.Looper.loop(Looper.java:137)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.app.ActivityThread.main(ActivityThread.java:5227)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.lang.reflect.Method.invokeNative(Native Method)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.lang.reflect.Method.invoke(Method.java:511)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at dalvik.system.NativeStart.main(Native Method)

第 94 行:

String selectedValue = (String) getListAdapter().getItem(position);

整个方法:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    // get selected items
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();

}

这里有什么问题? 太棒了

【问题讨论】:

    标签: android listview android-arrayadapter indexoutofboundsexception listselectionlistener


    【解决方案1】:

    直接改成

    //String selectedValue = (String) getListAdapter().getItem(position);
    String selectedValue = markers.get(position).getTitle();
    

    或尝试覆盖 getItem() 并将项目返回那里。

    编辑

    我不确定您的数据,但您可以在 getItem() 中返回您的数据对象,如下所示

    @Override
        public Object getItem(int position) {
            return "123";
        }
    

    这只是一个示例。如果您现在调用getListAdapter().getItem(position),它将返回123字符串,您可以根据您的数据要求修改它。

    【讨论】:

    • 添加了解释
    【解决方案2】:

    你可以这样做:

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    
        TextView title = (TextView) v.findViewById(R.id.title);
        ProgressBar loader = (ProgressBar) v.findViewById(R.id.loader);
        ImageView image = (ImageView) v.findViewById(R.id.itemimgae);
        TextView views = (TextView) v.findViewById(R.id.views);
        TextView likes=(TextView) v.findViewById(R.id.likes);
        TextView upvote=(TextView) v.findViewById(R.id.upvote);
        TextView downvote=(TextView) v.findViewById(R.id.downvote);
        TextView desc=(TextView) v.findViewById(R.id.desc);
        TextView pub =(TextView) v.findViewById(R.id.pub);
        TextView idnum =(TextView) v.findViewById(R.id.idnum);
    
    //than use the values
        String stLikes = likes.getText().toString();
    /.....
    }
    

    【讨论】:

    • 我试图从单击的项目中获取值.. 不要将它们放在 textViews 中
    • TextViews 包含来自点击项目的 de 值
    猜你喜欢
    • 2019-02-22
    • 2016-02-11
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多