0

I have Quote List class QuoteListFragment. where I am loading data in adapter from server like this

private ArrayList<Quote> quotes;
quotes = response.body();
NewQuoteAdapter adapter = new NewQuoteAdapter(getContext(), response.body());
mQuoteList.setAdapter(adapter);

and RecyclerView Adapter called NewQuoteAdapter

I am displaying Item in details with QuoteViewFragment. I have implemented a button called "delete" in this fragment and I want to give the user a chance to delete that quote from fragment so When the user goes back to the list, it disappears from the list. I have no idea how to achieve this. Let me know if someone can give me a solution for this. Thanks

amira
  • 416
  • 1
  • 7
  • 24
Priya
  • 1,602
  • 4
  • 22
  • 37
  • Are you using any serializer for showing data ? you must share the code here so i can give a solution. do a paste bin. – Bilal Shahid Sep 29 '17 at 18:35
  • Possible duplicate of [Remove an item in RecyclerView- Android](https://stackoverflow.com/questions/36858086/remove-an-item-in-recyclerview-android) – Matt Sep 29 '17 at 18:35
  • @BilalShahid I am using serializer called Quote for it. Thanks – Priya Sep 29 '17 at 18:36
  • All the answers below will remove the item from the list. But I believe the question you are asking is: How can I keep the deleted quote from reappearing when the user restarts the Activity. Since the quotes are being loaded from the data on the server. Is that what you want? – Barns Sep 29 '17 at 19:00

3 Answers3

3

well you have a list, and adapter, and a recyclerview

ArrayList<String> myQuoteList = new ArrayList<String>();
MyCustomAdapter adapter = new MyCustomAdapter(myQuoteList);
rcvQuotes.setAdapter(adapter);

Then in your delete you can just do

private void deleteAtIndex(int index){
      myQuoteList.remove(index);
      adapter.notifyDataSetChanged();
}

//make a interface.

public interface fragmentCallback{
     boolean onQuoteDeleted(Quote deleteQuote);
}

have your activity implement the interface:

myActivity implements fragmentCallback{
     public boolean onQuoteDeleted(Quote deletedQuote){
         if(myQuotelist.contains(deleteQuote){
              myQuoteList.remove(deleteQuote);
              adapter.notifyDataSetChanged();
         }
     }
}

then in fragment simpley

myFragment.setQuote(selectedQuote);

inside of fragment just do:

       @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            mFragmentCallback = (IFragmentCallback) context;

        }catch (Exception ex){
            A35Log.e(TAG, "Parent Context does not implement fragmentCallback");

        }
    }

public void setQuote(Quote showQuote){
     mSelectedQuote = showQuote;
}

btnDelete_onClick(){
    if(mFragmentCallback != null){
         mFragmentCallback.onDeleteQuote(mSelectedQuote);
    }
}

you can handle it by selected index of the row, or by last index, or first index, or you can add a long touch listener or trash can to the row item. how you get the index is up to you.

That's it, that is all there is to it.

Sam
  • 5,342
  • 1
  • 23
  • 39
  • Hi Priya, I don't see why that changes anything. Are you asking how to retain values? You can always keep track of the selected index as I mentioned so onClick "currentSelectedItem" or "selectedIndex" then pass to fragment Quote Object for display. If they click "delete" simply callback interface of deleteQuote. I will update my answer to show how. – Sam Sep 29 '17 at 19:04
0
Arraylist.remove(index)

recycleradapter.notifydatasetchanged()
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
sdfbhg
  • 109
  • 5
0

Get the index of clicked item e.g index = 4 list of quote e.g quotesList.

quotesList.remove(index);
adapter.notifyDataSetChanged();
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
Bilal Shahid
  • 490
  • 9
  • 16