Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kevin Faust
15,353 PointsHow to delete a row in RecyclerView (having 2 problems)
Hey everyone,
What I have right now is a delete method inside my Adapter class:
public void delete(int position) {
mNumbers.remove(position);
notifyItemRemoved(position);
}
The problem i'm having is where to set my onclick listener. In treehouse we generally used our onclick listener inside of our ViewHolder class. However someone on stackoverflow said never to do that and to do it in my bind method instead.
So I tried it both ways and came across a problem both ways
OnBind
I set my onclick listener inside it and then write the following:
delete(getAdapterPosition())
I'm trying to use the getAdapterPosition method to delete the current recyclerview row however android is saying this method doesn't exist
I also tried to just pass in the "position" given by the OnBind method however my app was acting all wonky and things werent getting deleted properly
In ViewHolder
I set my onclick listener here and write the same as above. At least the getAdapterPosition method is recognized by android here however now I get this error: non static method cannot be referenced from a static context
Thanks to anyone that can help,
Kevin
Seth Kroger
56,416 PointsCode? It's not entirely clear what you're trying to do from those snippets. Is there a delete button in each list item you click, or are you tapping the item to delete? Did you declare the view holder as a static class? (if it is, you can't call instance methods of the containing adapter because there's no back reference to it.)
Kevin Faust
15,353 PointsI fixed the problem by removing the static keyword:. Im wondering if this is the most efficient way to do this. Others have said this is just a quick fix and that i should use the static keyword to increase performance. Thoughts?
Below is simply just a textview, edittext, checkbox, and a delete button
public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {
private List<numbers> mNumbers = Collections.emptyList();
public myAdapter (List<numbers> numbers) {
mNumbers = numbers;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_slot, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.bindNumbers(mNumbers.get(position));
}
@Override
public int getItemCount() {
return mNumbers.size();
}
public void delete(int position) { //removes the row
mNumbers.remove(position);
notifyItemRemoved(position);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView numberText;
EditText checklistText;
Button deleteButton;
CheckBox checkDisBox;
public ViewHolder(View itemView) {
super(itemView);
numberText = (TextView) itemView.findViewById(R.id.number);
checklistText = (EditText) itemView.findViewById(R.id.editText);
deleteButton = (Button) itemView.findViewById(R.id.deleteButton);
checkDisBox = (CheckBox) itemView.findViewById(R.id.checkBox);
deleteButton.setOnClickListener(this); //button onclick listener
}
public void bindNumbers(numbers numbers) {
numberText.setText(numbers.getInt() + "");
}
@Override
public void onClick(View v) {
delete(getAdapterPosition()); //calls the method above to delete
}
}
}
Kevin Faust
15,353 PointsKevin Faust
15,353 Pointsmaybe im not even doing it right and there is a simpler way?