0

I'm creating an Android app, the data that i'm sending through intent is being retrieved every time i click on the item.

I'm sending the retrieved data(which it's a subcollection that is being retrieved every time i click on item) through the intent,and all data receives in an arraylist, so the listener don't know if the same data existed in the arraylist,because the data are in the other activity.

when i click for the first time the data displayed normally in ItemMarkerActivity but when i go back and click again on the same item i see the data load again in the recycler view,and added to the previous same data, i'm using the technique of removing the data onStop but it didn't work perfectly,because i need to close all activities to see that the data removed, i tried to send the CollectionReference through intent but i couldn't do. so I need a way of removing the data immediately after closing the activity, and if anyone has another approach for solving this problem it would better.

Thanks in advance

adapter.setOnItemClickListener(new MarketAdapterRecyclerView.OnItemClickListener() {
    @Override
    public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
        CollectionReference path = documentSnapshot.getReference().collection("ShoppingItems");
        listener = path.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    return;
                }

                for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges()) {
                    if (dc.getType() == DocumentChange.Type.ADDED) {
                        item = dc.getDocument().toObject(Item.class);
                        itemList.add(item);
                    }
                }

                Intent intent = new Intent (shoppingActivity.this, ItemMarkerActivity.class);
                Log.v(TAG,"###################################" + itemList.toString());
                intent.putExtra("keyName", itemList);
                startActivity(intent);
            }
        });
    }
}

The Activity That Receives The data

The Manifest

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> implements Parcelable{

    public ArrayList<Item> ItemList;
    public Context mContext;
    private onMallListener mOnMallListener;





    private static final int NO_POSITION = -1;

    public ItemAdapter(ArrayList<Item> ItemList, Context mContext, onMallListener mOnMallListener) {

        this.ItemList = ItemList;
        this.mContext = mContext;
        this.mOnMallListener = mOnMallListener;



    }


    protected ItemAdapter(Parcel in) {
        ItemList = in.createTypedArrayList(Item.CREATOR);
    }

    public static final Creator<ItemAdapter> CREATOR = new Creator<ItemAdapter>() {
        @Override
        public ItemAdapter createFromParcel(Parcel in) {
            return new ItemAdapter(in);
        }

        @Override
        public ItemAdapter[] newArray(int size) {
            return new ItemAdapter[size];
        }
    };

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_card_view_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view, mOnMallListener);

        return viewHolder;

    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

        Item item = ItemList.get(i);
        viewHolder.itemType.setText(ItemList.get(i).getItemType());
        Picasso.with(mContext)
                .load(item.getImageUrl())
                .fit()
                .centerCrop().into(viewHolder.imageUrl);



    }

    @Override
    public int getItemCount() {
        return ItemList.size();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(ItemList);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

        View mView;

        public TextView price;
        public TextView description;
        public TextView itemType;
        public ImageView imageUrl;
        onMallListener onMallListener;




        public ViewHolder(@NonNull View itemView, onMallListener mOnMallListener) {
            super(itemView);
            mView = itemView;

            itemType = (TextView) mView.findViewById(R.id.card_view_image_title);
            imageUrl = (ImageView) mView.findViewById(R.id.card_view_image);
            this.onMallListener = mOnMallListener;

            mView.setOnClickListener(this);





        }

        @Override
        public void onClick(View v) {
            if(mOnMallListener != null){
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION){
                    mOnMallListener.onMallClick(position);
                }

            }
        }


    }

    public interface onMallListener{
        void onMallClick(int position);

    }

}
Michal
  • 11
  • 4
  • Please do not post images of code. Just copy the text of the code into the question so that it's easier to read and search. – Doug Stevenson Aug 07 '19 at 16:23
  • What is the problem that you are facing then? The question is not very clear. Can you please clarify? – Reaz Murshed Aug 07 '19 at 17:52
  • not able to understand your problem correctly if the activity is closed means destroyed then automatically all your data lost unless you are restoring it – Vikas Sharma Aug 07 '19 at 18:18
  • What's the issue with the shared code? – Alex Mamo Aug 08 '19 at 09:09
  • I'm sending the retrieved data(which it's a subcollection that is being retrieved every time i click on item) through the intent,and all data receives in an arraylist, so the listener don't know if the same data existed in the arraylist,because the data are in the other activity. – Michal Aug 08 '19 at 16:05
  • when i click for the first time the data displayed normally but when i go back and click on the same item i see the data loaded again in the recycler view,and added to the previous same data, i'm using the technique of removing the data onStop but it didn't work perfectly,because i need to close all activities to see that the data removed – Michal Aug 08 '19 at 16:05

1 Answers1

0

Save data using Room database in first activity and retrieve it in second. In any place of your code (and in any activity's callback) you can clean db and all lists/recyclers which listen this data.

https://developer.android.com/training/data-storage/room

Hope it'll help

Peter Staranchuk
  • 1,343
  • 3
  • 14
  • 29
  • Thank you bro, i'll try this :) – Michal Aug 07 '19 at 17:51
  • To quickly test this workaround you can use shared preferences for local db. see https://stackoverflow.com/questions/14981233/android-arraylist-of-custom-objects-save-to-sharedpreferences-serializable – Peter Staranchuk Aug 08 '19 at 09:54
  • I tried Room database, but i'm using NoSQL database, the main problem is that when i click on item for the first time the data displayed normally in ItemMarkerActivity but when i go back and click on the same item i see the data loaded again in the recycler view,and added to the previous same data, so i need a way to finish or clean all data when i close the activity – Michal Aug 09 '19 at 16:02