Android 调用onListItemClick时OutOfIndex崩溃
这是我的列表适配器:Android 调用onListItemClick时OutOfIndex崩溃,android,listview,android-arrayadapter,indexoutofboundsexception,listselectionlistener,Android,Listview,Android Arrayadapter,Indexoutofboundsexception,Listselectionlistener,这是我的列表适配器: 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.widge
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;
}
}
第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();
}
这里有什么问题?
ty您可以执行以下操作:
@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();
/.....
}
干脆换成
//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个字符串,您可以根据您的数据要求对其进行修改。我正在尝试从单击的项中获取值。。不将其放入文本视图文本视图包含单击项的de值添加了解释
@Override
public Object getItem(int position) {
return "123";
}