这是我的列表适配器:
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 的一项时,应用程序崩溃..
日志猫:
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();
}
这里有什么问题?泰