2016-02-19 39 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等等。

相关问题