I am working on recyclerview. Recently I added an item click listener, but when I clicked on the item the application stopped working.
Here is the adapter class:
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();
}
}
Here is the main class:
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();
}
}
And the interface
public interface Clicklistener {
void itemClicked(View view, int position);
}
You pass
int
position toToast.makeText()
and it thinks that it is a resource id value, and stops with message which looks like that: 'could not find string with id ...'Simply replace it with
String.valueOf(position)
.