get data from onclick - android

First, my problem is exactly similar to this, but I still cant solve my problem.
public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.ViewHolder> {
private static final String TAG = "PlaceAdapter";
List<PlaceSaved> items;
PlaceDatabase db;
public PlacesAdapter(List<PlaceSaved> items, PlaceDatabase db) {
this.items = items;
this.db = db;
}
#Override
public PlacesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.places_list_item,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final PlacesAdapter.ViewHolder holder, final int position) {
holder.name.setText(items.get(position).getTitle());
holder.time.setText(items.get(position).getTime());
holder.longi.setText(items.get(position).getLongi());
holder.delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
// removeItem(items.get(holder.getAdapterPosition()));
AsyncTask.execute(new Runnable() {
#Override
public void run() {
AsyncTask.execute(new Runnable() {
#Override
public void run() {
db.databaseInterface().delete(items.get(position));
Snackbar.make(view , items.get(position).getTime()+" deleted, will be updated next time!", Snackbar.LENGTH_LONG).show();
}
});
}
});
}
});
holder.cview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("CardView", "Clicked "+items.get(position).getTitle()+", "+items.get(position).getLongi());
Location Saved_Location_onCard = new Location("");
Saved_Location_onCard.setLatitude(Double.parseDouble(items.get(position).getTitle()));
Saved_Location_onCard.setLatitude(Double.parseDouble(items.get(position).getLongi()));
latlang.Lat = (Double.parseDouble(items.get(position).getTitle()));
latlang.Lang = (Double.parseDouble(items.get(position).getLongi()));
Snackbar.make(view, "The new location is "+
items.get(position).getTitle()+", "+items.get(position).getLongi(),
Snackbar.LENGTH_LONG).show();
// Intent intent = new Intent(, SunFragment.class)
SecondFragment sf = new SecondFragment();
sf.updateLocationUI();
// sf.showMap();
}
});
}
After realising that onclick is not a good idea inside onBindViewHolder, I am trying to move it to ViewHolder as:
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
public TextView name;
public TextView time;
public TextView longi;
public ImageButton delbutton;
public CardView cview;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.secondLine);
time= itemView.findViewById(R.id.firstLine);
longi = itemView.findViewById(R.id.longitude);
delbutton = itemView.findViewById(R.id.delicon);
cview = itemView.findViewById(R.id.place_view);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view){
//Snackbar.make(this,"Hi", Snackbar.LENGTH_LONG).show();
Log.e("onClick", "Hello"+getLayoutPosition());
}
}
So, I am abel to get the layout's position, but not the values stored in the card, as I was able to do when I defined it inside BindView.
How I can do that?
Also, any idea how to delete and update the delete function?

Get the position with getAdapterPosition().
#Override
public void onClick(View view){
// Clicked postion
int position =getAdapterPosition();
PlaceSaved place=items.get(position);
// Here is the data Do your stuff
}
To delete a specific item with position call.
items.remove(position);
notifyItemRemoved(position);
Read Notify methods for RecyclerView.AdapterHere.

Related

How to place onClickListeners for views inside the items of recyclerview?

I am working with recyclerview and sqlite database. i want to place a button in cardview such that whenever user clicks on that button, the respective record should be deleted from the table.
This is my adapter class for recyclerview
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private Context mContext;
private Cursor mCursor;
private butttonsAdapetrListener mlistener;
private RecyclerView.ViewHolder v;
public CardAdapter(Context context, Cursor cursor ,butttonsAdapetrListener listener){
mContext=context;
mCursor=cursor;
mlistener=listener;
}
#NonNull
#Override
public CardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.mycard,parent,false);
return new CardViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CardViewHolder holder, int position) {
if(!mCursor.moveToPosition(position)){
return;
}
String name = mCursor.getString(mCursor.getColumnIndex("NAME"));
int total = mCursor.getInt(mCursor.getColumnIndex("TOTAL"));
int bunked = mCursor.getInt(mCursor.getColumnIndex("BUNK"));
int color = mCursor.getInt(mCursor.getColumnIndex("COLOR"));
long id= mCursor.getLong(mCursor.getColumnIndex("_id"));
holder.nameText.setText(name);
holder.totalText.setText(Integer.toString(total));
holder.bunkedText.setText(Integer.toString(bunked));
holder.colorText.setBackgroundColor(color);
holder.itemView.setTag(id);
v=holder;
}
#Override
public int getItemCount() {
return mCursor.getCount();
}
public class CardViewHolder extends RecyclerView.ViewHolder{
public TextView nameText;
public TextView totalText;
public TextView bunkedText;
public TextView colorText;
public Button delete;
public CardViewHolder(#NonNull View itemView) {
super(itemView);
nameText= itemView.findViewById(R.id.name);
totalText = itemView.findViewById(R.id.total_num);
bunkedText = itemView.findViewById(R.id.bunked_num);
colorText= itemView.findViewById(R.id.color);
delete= itemView.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mlistener.deleteOnClick(v,getAdapterPosition());
}
});
}
}
public interface butttonsAdapetrListener{
void deleteOnClick(RecyclerView.ViewHolder v,long position);
}
public void swapCursor(Cursor newCursor){
if(mCursor!=null){
mCursor.close();
}
mCursor=newCursor;
if(newCursor!=null){
notifyDataSetChanged();
}
}
}
This is my main activity code to delete record from database
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
private CardAdapter mAdapter;
private long id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteOpenHelper proBunkerDatabaseHelper = new ProBunkerDatabaseHelper(this);
db = proBunkerDatabaseHelper.getWritableDatabase();
RecyclerView recyclerView = findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CardAdapter(this, getCursor(), new CardAdapter.butttonsAdapetrListener() {
#Override
public void deleteOnClick(RecyclerView.ViewHolder v, long position) {
id = (long)v.itemView.getTag();
db.delete("MYTABLE","_id="+id,null);
mAdapter.swapCursor(getCursor());
}
});
recyclerView.setAdapter(mAdapter);
FloatingActionButton b = findViewById(R.id.fab);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,AddSubject.class);
startActivity(intent);
}
});
}
public Cursor getCursor(){
return db.query("MYTABLE",null,null,null,null,null,"_id ASC");
}
}
The main problem is the when I click on the delete button in any item it is deleting the item which is last in the cursor.
there are no compilation errors and runtime issues only the records are not deleting in correct order.
Try it like this:
Your interface:
public interface butttonsAdapetrListener{
void deleteOnClick(int id,long adapterPos);
}
Inside your CardViewHolder:
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int id = (int)view.getTag();
mlistener.deleteOnClick(id,getAdapterPosition());
}
});
In onBindViewHolder
holder.delete.setTag(id);
In MainActivity onCreate
mAdapter = new CardAdapter(this, getCursor(), new CardAdapter.butttonsAdapetrListener() {
#Override
public void deleteOnClick(int id, long position) {
db.delete("MYTABLE","_id="+id,null);
mAdapter.swapCursor(getCursor());
}
});
Another alternative
If you want your activity to be clean and not handle data from within.
Keep it for the adapter to handle the data being deleted and notifying any change.
Just create a listener for the button in the onbindviewholder method.

remove database item and update view on click

I am trying to remove item from database onClick of a button(defined as delbutton) here.
My Adapter looks like(I have included definitions and removed onBindViewHolder for brevity), and the onclick method is inside ViewHolder.
public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.ViewHolder> {
private static final String TAG = "PlaceAdapter";
List<PlaceSaved> items;
PlaceDatabase db;
public PlacesAdapter(List<PlaceSaved> items, PlaceDatabase db) {
this.items = items;
this.db = db;
}
#Override
public PlacesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.places_list_item, parent, false);
return new ViewHolder(view);
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
public TextView name;
public TextView time;
public TextView longi;
public ImageButton delbutton;
public CardView cview;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.secondLine);
time = itemView.findViewById(R.id.firstLine);
longi = itemView.findViewById(R.id.longitude);
delbutton = itemView.findViewById(R.id.delicon);
cview = itemView.findViewById(R.id.place_view);
itemView.setOnClickListener(this);
delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Log.e("What", "Get"+getAdapterPosition());
removeItem(getAdapterPosition());
}
});
}
#Override
public void onClick(View view) {
// Clicked postion
int position = getAdapterPosition();
PlaceSaved place = items.get(position);
// Here is the data Do your stuff
Log.e("Will WORK", place.getLongi());
}
}
private void removeItem(int position){
db.databaseInterface().delete(items.get(position));
items.remove(position);
notifyItemRemoved(position);
}
}
Now on clicking delbutton, I am getting the error:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
I know, I need to invoke runOnUIThread somehow, but inside ViewHolder, I cant define it. How can I solve this problem?
create new Thread Executors class like below
public class AppExecutors {
private final Executor mDiskIO;
private AppExecutors(Executor diskIO) {
this.mDiskIO = diskIO;
}
public AppExecutors() {
this(Executors.newSingleThreadExecutor());
}
public Executor diskIO() {
return mDiskIO;
}
}
use that class like this way..make return type of delete interface int type , to check successfully delete item.
private void removeItem(int position) {
new AppExecutors().diskIO().execute(new Runnable() {
#Override
public void run() {
if(db.databaseInterface().delete(items.get(position)) > 0 ){
runOnUiThread(new Runnable() {
#Override
public void run() {
items.remove(position);
notifyItemRemoved(position);
}
});
}
}
});
}

How to get image from recyclerview position clicked data and set it into another imageview?

I have an app which contains recyclerview with imageview and text.When I click on item based on position that is imageview i want image of that image view and set it to another Imageview.To do this I have made interface and implements its method in Activity but need to know that how do i do that
Adapter code:
public interface OnItemClicked {
void onItemClicked(int position);
}
public AvatarListAdapter(Context context, int[] arrayList) {
this.mContext = context;
this.dataModel = arrayList;
}
#Override
public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
return new AvatarListAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
holder.mAvatarImage.setImageResource(dataModel[position]);
holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemClicked(position);
}
});
}
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick;
}
#Override
public int getItemCount() {
return dataModel.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
}
}
}
Activity code:
private void initializeActions() {
int numberOfColumns = 3;
mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar);
mAvatarImages.setAdapter(mAdapter);
mAdapter.setOnClick(this);
}
#Override
public void onItemClicked(int position) {
String pos = "You clicked at position " + position;
Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
mAvatarImages.setBackgroundResource(avatar[position]);
}
You can get data by position on OnClick().As you are using local drawable for image resource, One way to do is Modify your viewHolder as follows.This is just a one way you can also use ViewTag.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Circle mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
mAvatarImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int postion =getAdapterPosition();
int image =arrayList[postion];
// Use this image to forward
}
}
Remove OnClick from onBindViewHolder.
Implement RecycleView onClick() method and get specified data while onclick position.have look
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Circle mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
mAvatarImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position=arrayList[getAdapterPosition()];
String imagepath = arraylist.get(position).getImagePath
//you can use imagepath anywhere you want either be put in bundle or intent call
}
}
Adapter code should look like this
private final OnItemClickListener listenerOnItemClick;
public interface OnItemClickListener {
void onItemClick(int position); }
public AvatarListAdapter(Context context, int[] arrayList, OnItemClickListener listenerOnItemClick) {
this.mContext = context;
this.dataModel = arrayList;
this.listenerOnItemClick=listenerOnItemClick;
}
#Override
public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
return new AvatarListAdapter.ViewHolder(view); }
#Override
public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
holder.mAvatarImage.setImageResource(dataModel[position]);
holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemClicked(position);
}
}); }
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick; }
#Override
public int getItemCount() {
return dataModel.length; }
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
} }
Activity Code
AvatarListAdapter.OnItemClickListener onItemClickListener = new AvatarListAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
//change your Image here
String pos = "You clicked at position " + position;
Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
mAvatarImages.setBackgroundResource(avatar[position]);
}
};
private void initializeActions() {
int numberOfColumns = 3;
mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar,onItemClickListener );
mAvatarImages.setAdapter(mAdapter);
mAdapter.setOnClick(this);
}

How to add click to RecyclerView whole row and a specific row item separately?

I have a RecyclerView with two TextViews and one ImageView. I want to add separate click to whole each row and the ImageViewin each row seperately. Is it possible?
Here is my Adapter class:
public class ContactsListAdapter extends RecyclerView.Adapter<ContactsListAdapter.MyViewHolder> {
private List<ContactsDataModel> dataList;
private Typeface tfSegoeui;
Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, detail;
public ImageView catIcon, imgCall;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
detail = (TextView) view.findViewById(R.id.detail);
catIcon = (ImageView) view.findViewById(R.id.catIcon);
imgCall = (ImageView) view.findViewById(R.id.imgCall);
}
}
public ContactsListAdapter(Context context, List<ContactsDataModel> dataList) {
this.dataList = dataList;
this.context = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.contacts_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ContactsDataModel dataModel = dataList.get(position);
tfSegoeui = Typeface.createFromAsset(holder.title.getContext().getResources().getAssets(), "fonts/segoeui.ttf");
holder.title.setText(dataModel.getName());
holder.detail.setText(dataModel.getPersonalNumber());
holder.imgCall.setTag(dataModel.getPersonalNumber());
holder.catIcon.setImageResource(dataModel.getImage());
holder.title.setTypeface(tfSegoeui);
holder.detail.setTypeface(tfSegoeui);
holder.imgCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String number = holder.imgCall.getTag().toString();
Log.d("number", number);
String num = "tel:" + number;
try {
Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(num));
context.startActivity(dialIntent);
}catch(Exception e) {
Toast.makeText(context,"Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
}
});
}
#Override
public int getItemCount() {
return dataList.size();
}
private class EndCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String LOG_TAG = "Call contact ";
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
Log.i(LOG_TAG, "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i(LOG_TAG, "IDLE");
}
}
}
}
Set your single row layout file as
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, detail;
public ImageView catIcon, imgCall;
//add item_row layout e.g
public RelativeLayout rel;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
detail = (TextView) view.findViewById(R.id.detail);
catIcon = (ImageView) view.findViewById(R.id.catIcon);
imgCall = (ImageView) view.findViewById(R.id.imgCall);
rel= (RelativeLayout) view.findViewById(your_id_here);
rel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//whatever you want here
}
});
}
}
Please refer the below code for item click event.
ContactsListAdapter
private List<ContactsDataModel> dataList;
private Typeface tfSegoeui;
Context context;
public interface OnContactClick {
public void onContactClick(int position);
}
OnContactClick onContactClick;
public void setOnContactClick(OnContactClick onContactClick) {
this.onContactClick = onContactClick;
}
Add this in onBindViewHolder
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
ContactsDataModel dataModel = dataList.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(onContactClick != null) {
onContactClick.onContactClick(position);
}
}
});
Add below line in activity
ContactsListAdapter contactsListAdapter = new ContactsListAdapter(this, dataList);
contactsListAdapter.setOnContactClick(new ContactsListAdapter.OnContactClick() {
#Override
public void onContactClick(int position) {
//You will receive event when clicking item in list
}
});
you can add to click listener to any view like this inside ViewHolder class
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, detail;
public ImageView catIcon, imgCall;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
detail = (TextView) view.findViewById(R.id.detail);
catIcon = (ImageView) view.findViewById(R.id.catIcon);
imgCall = (ImageView) view.findViewById(R.id.imgCall);
imgCall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
title.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
catIcon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
detail.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
and if you want position of the item clicked.
add this line inside your onBindViewHolder
title.setTag(position);
get position using title.getTag()
save
View itemView
in the holder and do setonclick like you did with the imgCall
also you did two onclick listeners on imgCall

Removing item from RecyclerView

I am having trouble removing items from RecyclerView. When I click on delete, the item is removed from RecyclerView, but comes back when I open the app again. I'm hoping it is just a minor issue that someone here can point out or direct me to what area to troubleshoot. The removeItem(String item) in bold is what I think is the issue. You can't see it in this post, but it is "not used".
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private List<Grocery> mListData;
private SQLGroceryHelper helper;
RecyclerViewAdapter adapter;
//Adapter's Constructor//
public RecyclerViewAdapter(List<Grocery> mDataList) {
this.mListData = mDataList;
}
//Provide a reference to the views for each contact item//
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView rowItem;
ImageButton purchasedButton;
ImageButton deleteButton;
LinearLayout linearLayout;
public MyViewHolder(View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.recycler_row);
rowItem = (TextView) itemView.findViewById(R.id.item_field1);
purchasedButton = (ImageButton) itemView.findViewById(R.id.item_purchased);
deleteButton = (ImageButton) itemView.findViewById(R.id.delete_item);
}
}
//Inflate the view based on the viewtype provided//
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Create a new view by inflating the row item xml//
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row, parent, false);
//Set the view to the ViewHolder//
MyViewHolder holder = new MyViewHolder(row);
return holder;
}
//Display data at the specified position//
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.rowItem.setText(mListData.get(position).getTextItem());
holder.purchasedButton.setOnClickListener(new View.OnClickListener() {
//Ignore this click for now//
#Override
public void onClick(View v) {
removeItem(position);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeItem(position);
}
});
}
public void **removeItem**(String item) {
int position = mListData.indexOf(item);
if (position != -1) {
mListData.remove(item);
notifyItemRemoved(position);
}
}
public void removeItem(int position) {
mListData.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
if (mListData == null) {
return 0;
}
return mListData.size();
}
}
You are removing the data from local object, mListData I guess the original data object remains intact. Remove the data item from the original data object as well
Declare a interface
public interface AdapterCommunication{
void removeStringItem(int position);
}
then in your adapter
private AdapterCommunication mListener;
public void setOnClickListener(AdapterCommunication listener){
mListener = listener;
}
Then from your activity where you initialize the adapter
RecyclerViewAdapter adapter = new RecyclerViewAdapter(list);
adapter.setOnClickListener(new AdapterCommunication{
public void removeStringItem(int position){
list.remove(position);
adapter.notifyDataSetChanged();
}
});
In your adaper,
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.remove(position);
}
});

Categories

Resources