2016-02-19 57 views
0

我在Fragment中使用RecyclerView來顯示類別列表(如圖所示)。選擇類別時,新的ActivityRecyclerView開頭。列表中的每個項目都有一個TextView和一個收藏圖標(右上角的星號)。一旦按下收藏夾圖標,TextView將被保存到另一個名爲收藏夾的Activity帶有多個適配器和SharedPreferences的RecyclerView

問題:讓我們說,我選擇A類,我按下喜歡按鈕第2項。一切看起來不錯,物品保存到收藏夾。如果我選擇B類,bamm,我發現前2項選擇...我去類別C,同樣的事情! 因此,如果我檢查收藏按鈕一次,它會檢查所有Adapters,就像他們正在溝通。這是爲什麼發生?

enter image description here

enter image description here

雖然收藏夾按鈕進行檢查,我無法從B類Ç收藏夾找到TextView活動。

當一個類別的項目選擇了開始的Activity

public class CategoriesDetailActivity extends AppCompatActivity { 

    Adapter0 ca0; 
    RecyclerView recList; 

    public CategoriesDetailActivity() { 
     // Required empty public constructor 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.categories_detail_activity); 

     Bundle bundle = this.getIntent().getExtras(); 
     // Bundle bundle = this.getArguments(); 
     bundle.getInt("id"); 
     int position = bundle.getInt("id"); 
     if (bundle.containsKey("id")) { 
      position = bundle.getInt("id"); 
     } else { 
      this.finish(); 
     } 

     recList = (RecyclerView) findViewById(R.id.cardList); 
     recList.setHasFixedSize(true); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 
     llm.setOrientation(LinearLayoutManager.VERTICAL); 
     recList.setLayoutManager(llm); 

     switch (position) { 
      case 0: 
       ca0 = new Adapter0(this, createList0(99)); 
       recList.setAdapter(ca0); 
       break; 
      case 1: 
       ca0 = new Adapter0(this, createList1(80)); 
       recList.setAdapter(ca0); 
       break; 
      ... 
      } 
    } 

    private List<BeanSampleList> createList0(int size) { 
     List<BeanSampleList> result = new ArrayList<>(); 
     for (int i = 0; i <= size; i++) { 
      BeanSampleList ci = new BeanSampleList(); 
      ci.title = DataText.Text1[i];   
      ci.id = i; 
      result.add(ci); 
     } 
     return result; 
    } 

    private List<BeanSampleList> createList1(int size) { 
     List<BeanSampleList> result = new ArrayList<>(); 
     for (int i = 0; i <= size; i++) { 
      BeanSampleList ci = new BeanSampleList(); 
      ci.title = DataText.Text2[i];   
      ci.id = i; 
      result.add(ci); 
     } 
     return result; 
    } 

@Override 
    public void onResume() { 
     super.onResume(); 
     if (recList.getAdapter() == ca0) { 
      ca0.notifyDataSetChanged(); 
     } if (recList.getAdapter() == ca1) { 
      ca1.notifyDataSetChanged(); 
     } else { 
      // nothing 
     } 
    } 
} 

Adapter類:

public class Adapter0 extends RecyclerView.Adapter<Adapter0 .ContactViewHolder> { 

    private Context context; 
    List<BeanSampleList> postBeanSampleList; 
    SharedPreference sharedPreference; 
    BeanSampleList beanSampleList; 

    public Adapter0 (Context context, List<BeanSampleList> postBeanSampleList) { 
     this.context = context; 
     this.postBeanSampleList = postBeanSampleList; 
     sharedPreference = new SharedPreference(); 
    } 

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

    public Object getItem(int position) { 
     return postBeanSampleList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public void onBindViewHolder(final ContactViewHolder holder,final int i) { 
     beanSampleList = (BeanSampleList) getItem(i); 
     holder.vName.setText(beanSampleList.getTitle()); 

     if (checkFavoriteItem(beanSampleList)) { 
      holder.btnFavourite.setLiked(true); 
      holder.btnFavourite.setTag("active"); 
     } else { 
      holder.btnFavourite.setLiked(false); 
      holder.btnFavourite.setTag("deactive"); 
     }    
    } 

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

     return new ContactViewHolder(itemView); 
    } 

    public class ContactViewHolder extends RecyclerView.ViewHolder implements OnLikeListener { 

     protected TextView vName; 
     protected LikeButton btnFavourite; 

     public ContactViewHolder(View v) { 
      super(v); 
      vName = (TextView) v.findViewById(R.id.t1); 
      btnFavourite = (LikeButton) v.findViewById(R.id.favouritesToggle); 

      btnFavourite.setOnLikeListener(this); 
     } 

     @Override 
     public void liked(LikeButton likeButton) { 
      final int position = getAdapterPosition(); 
      if (likeButton.getId() == btnFavourite.getId()) { 
       String tag = btnFavourite.getTag().toString(); 
       if (tag.equalsIgnoreCase("deactive")) { 
        sharedPreference.addFavorite(context, postBeanSampleList.get(position)); 
        btnFavourite.setTag("active"); 
        btnFavourite.setLiked(true); 
       } 
       Snackbar snackbar = Snackbar 
         .make(likeButton, "Added to Favorites!", Snackbar.LENGTH_SHORT); 
       snackbar.show(); 
      } 
     } 

     @Override 
     public void unLiked(LikeButton likeButton) { 
      final int position = getAdapterPosition(); 
      if (likeButton.getId() == btnFavourite.getId()) { 
       sharedPreference.removeFavorite(context, postBeanSampleList.get(position)); 
       btnFavourite.setTag("deactive"); 
       btnFavourite.setLiked(false); 

       Snackbar snackbar = Snackbar 
         .make(likeButton, "Removed from Favorites!", Snackbar.LENGTH_SHORT); 
       snackbar.show(); 
      } 
     } 
    } 

    public boolean checkFavoriteItem(BeanSampleList checkProduct) { 
     boolean check = false; 
     List<BeanSampleList> favorites = sharedPreference.loadFavorites(context); 
     if (favorites != null) { 
      for (BeanSampleList product : favorites) { 
       if (product.equals(checkProduct)) { 
        check = true; 
        break; 
       } 
      } 
     } 
     return check; 
    } 
} 

對於最近兩天我一直在試圖解決這一問題,但我不能不知道是什麼原因造成的。我也嘗試爲每個位置使用不同的Adapters,但沒有成功。

public class BeanSampleList { 

    public int id; 
    public String title; 
    public String subTitle; 
    public String bottomTitle; 
    public String imageView; 

    public BeanSampleList() { 
     super(); 
    } 

    public BeanSampleList(int id, String title, String subTitle, String bottomTitle, String imageView) { 
     super(); 
     this.id = id; 
     this.title = title; 
     this.subTitle = subTitle; 
     this.bottomTitle = bottomTitle; 
     this.imageView = imageView; 
    } 

    public String getSubTitle() { 
     return subTitle; 
    } 

    public void setSubTitle(String subTitle) { 
     this.subTitle = subTitle; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getTitleBottom() { 
     return bottomTitle; 
    } 

    public void setTitleBottom(String bottomTitle) { 
     this.bottomTitle = bottomTitle; 
    } 
    public String getImageView() { 
     return imageView; 
    } 

    public void setImageView(String imageView) { 
     this.imageView = imageView; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     BeanSampleList other = (BeanSampleList) obj; 
     if (id != other.id) 
      return false; 
     return true; 
    } 

} 

非常感謝你和我的英語對不起。

+0

我認爲你在所有商品視圖中訪問和綁定同一商店最喜歡的值。 – Inducesmile

回答

0

我認爲錯誤是在你的ID生成的BeanSampleList

private List<BeanSampleList> createList0(int size) { 
     List<BeanSampleList> result = new ArrayList<>(); 
     for (int i = 0; i <= size; i++) { 
      BeanSampleList ci = new BeanSampleList(); 
      ci.title = DataText.Text1[i];   
      ci.id = i; 
      result.add(ci); 
     } 
     return result; 
    } 

    private List<BeanSampleList> createList1(int size) { 
     List<BeanSampleList> result = new ArrayList<>(); 
     for (int i = 0; i <= size; i++) { 
      BeanSampleList ci = new BeanSampleList(); 
      ci.title = DataText.Text2[i];   
      ci.id = i; 
      result.add(ci); 
     } 
     return result; 
    } 

你需要生成唯一的ID在createList *法在BeanSamleList對象。正如在你當前的代碼中,對象在equals方法方面是相同的(它們的id是從0到list_size範圍)。

或者增加一些獨特的改性劑在同等比較,像添加父列表

@Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     BeanSampleList other = (BeanSampleList) obj; 
     if (id != other.id) 
      return false; 
     if (listId != other.listId) 
      return false; 
     return true; 
    } 
+0

我已經添加了'BeanSampleList'類,所以你可以看到我正在做什麼。 – Alec

+0

好了,答案依然如此。您在createList *方法中創建BeanSampleList對象,並且不考慮您通過意圖接收到的「位置」,因爲您將爲其生成從0到大小範圍的ID。 – aelimill

+0

嗚呼!它有點有用。首先,當'int i = 0'時,每個適配器的第一項被選中(不知道爲什麼),所以我把它改成了'int i = 1'。我想明天我需要創建60個if語句......希望有另一個解決方案。無論如何,我會在明天做出改變,如果一切正常,我會接受你的答案。謝謝!! – Alec

0

的你需要找到不同的方式來唯一地標識每個BeanSampleList元素的位置。只匹配id不夠好。也許你也可以匹配標題,如果你知道他們將是唯一的。或者通過將整數域分成不同的範圍來爲這些元素中的每一個生成唯一的ID,例如1至1000000爲類別1,1000001至2000000爲類別2等等。

相關問題