【发布时间】:2016-06-15 07:02:59
【问题描述】:
我正在尝试为我的RecyclerView 获取点击项目的位置。但是,这有点奇怪,仅让我在单击时记录该位置,而不是让我创建该位置的Toast。见这里:
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>{
private List<Country> countries;
private int rowLayout;
private Context mContext;
public MainAdapter(List<Country> countries, int rowLayout, Context context) {
this.countries = countries;
this.rowLayout = rowLayout;
this.mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView countryName;
public ViewHolder(View itemView) {
super(itemView);
countryName = (TextView) itemView.findViewById(R.id.countryName);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("RecyclerView", "onClick:" + getPosition());
//Toast.makeText(v.getContext(),getPosition(), Toast.LENGTH_LONG).show();
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Country country = countries.get(position);
viewHolder.countryName.setText(country.name);
}
@Override
public int getItemCount() {
return countries == null ? 0 : countries.size();
}
}
如果我在单击项目时取消注释位置的Toast,应用程序将崩溃,但日志似乎工作正常。我怎样才能修复它,以便我可以Toast 单击项目时的位置?
【问题讨论】:
-
为了让我们能够帮助您,我们需要查看您收到的错误消息是什么。请编辑您的问题并将其添加进去。
标签: android