尝试将一个int变量从Activity传递给Adapter

时间:2018-04-25 23:00:49

标签: java android adapter

我试图将pub_or_priv中的Activity1传递给Myadapter

我在这里查看了帖子,我认为我已经正确地关注了它们,但它仍然无法正常工作。我的adapter中的值始终为0,而应为012,就像我{{1}中的值一样}}

这就是我所做的。

在我的Activity1我从我的服务器获取Activity1并将其转换为"publicorprivate",其中包含:

int

对于//convert public_or_private to an integer pub_or_priv = Integer.parseInt(obj.getString("publicorprivate")); 中的不同单元格,它将是recyclerView01

现在我想将此传递给我的2,所以在adapter adapter我有:

contructor

在我的public MyAdapter(List<Review> reviewUsers, Activity activity, int pub_or_priv) { this.activity = activity; the_reviews = reviewUsers; this.mPub_or_priv = pub_or_priv; }

Activity1

然后在pAdapter = new MyAdapter(reviewList, this, pub_or_priv);

MyAdapter

然而,在 @Override public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) { Review r = the_reviews.get(position); //shared_status will be Just U, Private or Public String shared_status =""; if(mPub_or_priv==0){ //change colour depending on value ((ReviewHolder) viewHolder).phone_user_name.setTextColor(Color.parseColor("#DA850B")); shared_status = "Just U"; } if(mPub_or_priv==1){ ((ReviewHolder) viewHolder).phone_user_name.setTextColor(Color.parseColor("#0A7FDA")); shared_status = "Private"; } if(mPub_or_priv==2){ ((ReviewHolder) viewHolder).phone_user_name.setTextColor(Color.parseColor("#2AB40E")); shared_status = "Public"; } ((ReviewHolder) viewHolder).phone_user_name.setText(shared_status); etc..etc.. 所有单元格中,recyclerView phone_user_name始终为textbox,为"Just U"颜色,而事实上它应该是根据{{​​1}}中的#DA850B值,某些单元格中为PrivatePublic

如何才能将pub_or_priv正确地放入我的Activity1 ?,谢谢。

1 个答案:

答案 0 :(得分:1)

您似乎在活动/适配器中使用了一个值,因此当然所有行都显示相同的值。

在适配器中,使用与当前单元格对应的Review实例中的值:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

    Review review = the_reviews.get(position);

    int pubOrPriv = review.getPublicOrPrivate();

    //shared_status will be Just U, Private or Public
    String shared_status = "";

    if (pubOrPriv == 0) {
        //change colour depending on value
        ((ReviewHolder) viewHolder).phone_user_name.setTextColor(Color.parseColor("#DA850B"));
        shared_status = "Just U";
    }

    if (pubOrPriv == 1) {
        ((ReviewHolder) viewHolder).phone_user_name.setTextColor(Color.parseColor("#0A7FDA"));
        shared_status = "Private";
    }

    if (pubOrPriv == 2) {
        ((ReviewHolder) viewHolder).phone_user_name.setTextColor(Color.parseColor("#2AB40E"));
        shared_status = "Public";

    }

    //.............
}