2016-10-15 42 views
1

在我的應用我計劃使用AlarmManager時,下載時間從報警接收器服務到達我下載的文件和刪除ArrayList的自定義對象,然後使用sharedpref保存它,這個出發活動後,如果它已經在運行這樣爲什麼片段onResume()沒有正確地更新RecyclerView?

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

         } 

如果活動,則Fragment運行的onResume()將被調用,其中我得到保存的列表,並更新

@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); 


    } 
} 

現在,當片段運行的onResume我順利地拿到了清單數據,但是當adapter.notifyDataSetChanged();被稱爲數據在RecyclerView上更新,但它不顯示正確的值,但是當我重新啓動活動時,則RecyclerView顯示正確的值。我認爲adapter.notifyDataSetChanged();無法正常工作。

和還這是我在的onCreate()

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

UPDATE塞汀適配器代碼
這是我的適配器

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); 
       } 
      } 
     } 


    } 

回答

0

確保你傳遞給適配器列表參考得到更新。 每次創建一個新列表都是我的問題。例如,

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

而是我傳遞了對我的列表的引用並更新了現有的列表。

GetUpdatedList(myList); 

在函數內部 -

GetUpdatedList(ArrayList<String> myList) 
{ 
    myList.Append("foo"); 
} 
+0

我已經嘗試通過'pending.clear()'然後'pending.addAll(tempPending)'但沒有工作我可以做什麼在這裏請幫助 –

0

似乎pending是一個列表。那麼你不應該重新分配這個列表因爲你在這裏得到新的指針和適配器仍然會指向老的。你應該清除列表然後添加項目到它。 事情是這樣的:

pending.clear(); 
pending.addAll(gson.fromJson(getPendingDownloadsList, type2)); 
+0

我已經嘗試過這一點,但結果是一樣的。這不起作用 –

+0

顯示您的適配器的代碼 –

+0

請檢查我已更新的適配器代碼 –