我正在处理recyclerview。最近我添加了一个项目点击监听器,但当我点击该项目时,应用程序停止工作。
这是适配器类:
public class RVAdapter extends
RecyclerView.Adapter<RVAdapter.ItemViewHolder> {
ArrayList<String>itemlist;
Clicklistener clicklistener=null;
public void setClicklistener(Clicklistener clicklistener) {
this.clicklistener = clicklistener;
}
public RVAdapter(ArrayList<String> itemlist) {
this.itemlist = itemlist;
}
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView text;
public ItemViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
text= (TextView) itemView.findViewById(R.id.itemtextView);
}
@Override
public void onClick(View v) {
if(clicklistener!=null)
clicklistener.itemClicked(v,getAdapterPosition());
}
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
ItemViewHolder iv=new ItemViewHolder(v);
return iv;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.text.setText(itemlist.get(position));
}
@Override
public int getItemCount() {
return itemlist.size();
}
}
这是主要课程:
public class MainActivity extends AppCompatActivity implements Clicklistener {
ArrayList<String> list;
RecyclerView recyclerView;
RVAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
list=new ArrayList<>();
list.add("ABC");
list.add("DEF");
list.add("GHI");
list.add("JKL");
recyclerView= (RecyclerView) findViewById(R.id.RV);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter=new RVAdapter(list);
adapter.setClicklistener(this);
recyclerView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void itemClicked(View view, int position) {
Toast.makeText(getBaseContext(),position,Toast.LENGTH_SHORT).show();
}
}
界面
public interface Clicklistener {
void itemClicked(View view, int position);
}
答案 0 :(得分:3)
您将int
位置传递给Toast.makeText()
并认为它是资源ID值,并停止显示如下消息:'找不到带ID的字符串...'
只需将其替换为String.valueOf(position)
。