2016-06-27 94 views
2

我從ArrayList onBindViewHolder中的特定位置插入/移除。現在,我想在recyclerview上顯示這個修改後的列表。更改recyclerview適配器中的項目

適配器代碼:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAdapterViewHolder> { 
    private List<Info> dataList; 
    private Context mAct; 
    private List<Info> totalCandidatesList; 
    private String TAG = "OWD"; 

    public MyAdapter(List<Info> dataList, Context context) { 
     this.dataList = dataList; 
     this.mAct = context; 
    } 

    public void addApplications(List<Info> candidates) { 

     if(this.totalCandidatesList == null){ 
      totalCandidatesList = new ArrayList<>(); 
     } 
     this.dataList.addAll(candidates); 
     this.totalCandidatesList.addAll(candidates); 
     this.notifyItemRangeInserted(0, candidates.size() - 1); 

    } 

    public void clearApplications() { 
     int size = this.dataList.size(); 
     if (size > 0) { 
      for (int i = 0; i < size; i++) { 
       dataList.remove(0); 
       totalCandidatesList.remove(0); 
      } 
      this.notifyItemRangeRemoved(0, size); 
     } 
    } 

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


    public void onBindViewHolder(MyAdapterViewHolder mAdapterViewHolder, int i) { 

     if (i % 2 == 1) { 
      mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#ecf5fe")); 
      mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#ecf5fe")); 
     } else { 
      mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#e2f1ff")); 
      mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#e2f1ff")); 
     } 


     final WorkHolders workHolders = SingleTon.getInstance().getWorkHolders(); 
     final String customerName = SingleTon.getInstance().getCustomerName(); 
     String siteName = null; 
     if(customerName !=null) { 
      String[] sitenamearray = customerName.split("--"); 

      if (sitenamearray.length > 1) { 
       siteName = sitenamearray[1]; 
      } 
     } 
     final Info ci = dataList.get(i); 
     mAdapterViewHolder.title.setText(ci.heading1); 
     mAdapterViewHolder.jobNumber.setText(ci.heading2); 
     mAdapterViewHolder.distance.setText(ci.distance); 

     if(siteName != null && siteName.equalsIgnoreCase(ci.heading2)) { 
      mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#a7ffeb")); 
      mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#a7ffeb")); 
      if(i!=0){ 


> //Here i removed and inserted item in list . 
>      dataList.remove(i); 
>      dataList.add(0,ci); 

      } 
     } 

     final String finalSiteName = siteName; 
     final Bundle bundle = new Bundle(); 

     mAdapterViewHolder.layoutRipple.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Fragment fragment; 

       String name = ci.heading1 + "--" + ci.heading2; 

       Log.d(TAG,"new Jobname : "+ name); 
       if (finalSiteName == null || finalSiteName.equalsIgnoreCase("")) { 

        bundle.putString("name", customerName); 
        bundle.putString("oldwork", "yes"); 
        bundle.putString("running_job_selected", "yes"); 
       } else { 

        Log.d(TAG,"StartedOn Before Sending Bundle :" + workHolders.startedOn); 
        Log.d(TAG, "running Job is not selected"); 

        bundle.putString("name", name); 
        bundle.putString("oldwork", "yes"); 
        bundle.putString("running_job_selected", "no"); 
       } 

       FragmentTransaction ft = ((FragmentActivity) mAct).getSupportFragmentManager().beginTransaction(); 
       ft.setCustomAnimations(R.anim.glide_fragment_horizontal_in, R.anim.glide_fragment_horizontal_out); 

       fragment = new WorkDescriptionFragment(); 

       fragment.setArguments(bundle); 
       ft.addToBackStack("myadapter"); 
       ft.replace(R.id.content_frame, fragment).commit(); 
       SingleTon.getInstance().setWorkStatus("start"); 
      } 
     }); 

    } 

    @Override 
    public MyAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View itemView = LayoutInflater. 
       from(viewGroup.getContext()). 
       inflate(R.layout.single_item1, viewGroup, false); 

     return new MyAdapterViewHolder(itemView); 
    } 

    public static class MyAdapterViewHolder extends RecyclerView.ViewHolder { 

     protected TextView title; 
     protected TextView dateTime; 
     protected TextView distance; 
     protected TextView jobNumber; 

     protected CardView cardView; 
     protected LayoutRipple layoutRipple; 

     public MyAdapterViewHolder(View v) { 
      super(v); 
      title = (TextView) v.findViewById(R.id.title); 
      dateTime = (TextView) v.findViewById(R.id.dateTimeTextView); 
      distance = (TextView) v.findViewById(R.id.distanceTextView); 
      jobNumber = (TextView) v.findViewById(R.id.jobNumber); 
      cardView = (CardView) v.findViewById(R.id.cv); 
      layoutRipple = (LayoutRipple)v.findViewById(R.id.singleitemripple); 
     } 
    } 

} 

,你會看到在上面的代碼下面幾行我在哪裏刪除/列表中的onBindview插入項目,並想顯示recyclerview相同。

但現在我得到正常的datalist(不變)。

//Here i removed and inserted item in list . 
         dataList.remove(i); 
         dataList.add(0,ci); 

請幫我實現這一目標。

+0

請嘗試更準確地解釋您的問題。目前尚不清楚你試圖達到的目標。 –

+0

@alexeypolusov:我想更改onBindViewHolder方法中的Adapter datalist項目 –

+0

爲什麼需要在那裏更新數據?如果你想更新數據,只需更新你的適配器內的列表,然後nofifyDataChanged() –

回答

1

onBindViewHolder不是您應該更新適配器的地方。標準是更新適配器數據列表中的項目,然後notifyDataChanged()。例如,這是更新我的適配器內的信息的方法:

public void update(Track track) { 
     tracks.remove(track); 
     add(track); 
    } 

    public void add (Track track) { 
     tracks.add(track); 
     this.notifyDataSetChanged(); 
    } 

    public void addTracks(List<Track> tracks){ 
     this.tracks.addAll(tracks); 
     this.notifyDataSetChanged(); 
    } 

    public void clearAndAddTracks(List<Track> tracks) { 

     for (int i = 0; i < this.tracks.size(); i++) { 
      if (!this.tracks.get(i).isRunning()){ 

      } 
     } 
     this.tracks.clear(); 
     this.tracks.addAll(tracks); 
     this.notifyDataSetChanged(); 
    } 
+0

感謝您的回覆。根據你的建議我改變了我的代碼。謝謝 。另外,如果您提出這個問題,我將不勝感激。再次感謝 –

相關問題