4

in my app i schedule downloading task using AlarmManager when Download time arrives from Alarm Receiver Service i Download File and Delete from ArrayList of Custom Object and then save it using sharedpref and after this starting Activity if it is already running like this

if (mIsInForegroundMode == true) {
                            Intent intent1 = new Intent(this, DownloadingActivity.class);
                            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent1);

                        }

if activity running then Fragment onResume() will be called where i am getting saved list and updating

@Override
public void onResume() {
    super.onResume();
    getDestroyedData();
    adapter.notifyDataSetChanged();

}  

public void getDestroyedData() {
    String getPendingDownloadsList = preferences.getString("plist", "");

    Type type2 = new TypeToken<List<DownloadingActivity.scheduleListType>>(){}.getType();

    if (!getPendingDownloadsList.equals("")) {
        pending=gson.fromJson(getPendingDownloadsList, type2);


    }
}  

Now when onResume of fragment runs i successfully got list data but when adapter.notifyDataSetChanged(); is called data is updated on RecyclerView but it is not showing correct values but when i restart activity then RecyclerView shows correct values. i think adapter.notifyDataSetChanged(); not working correctly.

AND ALSO THIS IS MY CODE FOR SETING ADAPTER IN onCreate()

adapter = new PendingAdapter(pending, this.getActivity());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this.getActivity()));  

UPDATE
this is my Adapter

public class PendingAdapter extends RecyclerView.Adapter<PendingAdapter.PendingHolder> {

    ArrayList<DownloadingActivity.scheduleListType> pendingList;
    LayoutInflater inflater;

    PendingAdapter(ArrayList<DownloadingActivity.scheduleListType> pendingList, Context c) {

        this.pendingList = pendingList;
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public PendingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.custom_pending, parent, false);
        PendingHolder pendingHolder = new PendingHolder(v);
        return pendingHolder;
    }

    @Override
    public void onBindViewHolder(PendingHolder holder, int position) {

        holder.fileName.setText(pendingList.get(position).name);
        if (pendingList.get(position).reqCODE != 0) {
            holder.pendingType.setText("Start At " + pendingList.get(position).hour + ":" + pendingList.get(position).minute);
        }

    }

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


     //also tried this function
   /** public void swap(ArrayList<DownloadingActivity.scheduleListType> newList) {
        if (pendingList != null) {
            pendingList.clear();
            pendingList.addAll(newList);
        } else {
            pendingList = newList;
        }
        notifyDataSetChanged();
    }**/

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

        TextView fileName, pendingType;
        Button downloadByMobileBtn, removeFromPending;

        public PendingHolder(View itemView) {
            super(itemView);
            pendingType = (TextView) itemView.findViewById(R.id.pendingType);

            fileName = (TextView) itemView.findViewById(R.id.pendingFileNameTXT);
            downloadByMobileBtn = (Button) itemView.findViewById(R.id.downloadByMobileBTN);
            removeFromPending = (Button) itemView.findViewById(R.id.removeFromPending);

            downloadByMobileBtn.setOnClickListener(this);
            removeFromPending.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        ShowRemoveDialog(position);
                    }
                }
            });


        }

        @Override
        public void onClick(View v) {
            int p = getAdapterPosition();

            if(p!=RecyclerView.NO_POSITION) {
                if (v.getId() == downloadByMobileBtn.getId()) {


                    ShowDialog(p);
                }
            }
        }


    }

2 Answers2

0

Make sure the list reference you pass to the adapter gets updated. Creating a new list every time was the problem in my case. Eg.

ArrayList<String> myList = new ArrayList<String>();
MyAdapter adapter = new MyAdapter(myList);
myList = GetaNewList(); // Cause of error
adapter.notifyDataSetChanged(); 

Instead I passed a reference to my list and updated the existing list.

GetUpdatedList(myList);

Inside the function -

GetUpdatedList(ArrayList<String> myList)
{
  myList.Append("foo");
}
Duke79
  • 827
  • 1
  • 10
  • 21
0

seems pending is a list. then you should not reassign this list coz You got here new pointer and adapter will still point to old one. you should clear list and then add items to it. Something like this:

pending.clear();
pending.addAll(gson.fromJson(getPendingDownloadsList, type2));
Stepan Maksymov
  • 2,618
  • 19
  • 31