如何从活动类更新recyclerview textview

时间:2019-03-08 12:13:38

标签: android android-recyclerview textview

我在活动中使用了回收站视图。这是我的适配器类:

public class ComparisonResultRecyclerViewAdapter extends RecyclerView.Adapter<ComparisonResultRecyclerViewAdapter.MyViewHolder> {

Context ctx;
ArrayList<ComparisonResultRecyclerViewInfoStore> comparisonDataList;

public ComparisonResultRecyclerViewAdapter(Context ctx, ArrayList<ComparisonResultRecyclerViewInfoStore> comparisonDataList)
{
    this.ctx = ctx;
    this.comparisonDataList = comparisonDataList;
}

@Override
public ComparisonResultRecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ComparisonResultRecyclerViewAdapter.MyViewHolder(LayoutInflater.from(ctx).inflate(R.layout.comparison_result_recyclerview_layout, parent, false));
}

@Override
public void onBindViewHolder(ComparisonResultRecyclerViewAdapter.MyViewHolder holder, int position) {

    holder.title.setText(comparisonDataList.get(position).getTitle());
    holder.description.setText(comparisonDataList.get(position).getDescription());
    holder.rupees.setText("Pkr. " + comparisonDataList.get(position).getRupees());
    Picasso.get().load(comparisonDataList.get(position).getImage()).into(holder.image);
    holder.likes.setText(comparisonDataList.get(position).getLikes() + "k Likes");

    switch (comparisonDataList.get(position).getTitle()) {
        case "Outfitters":
            holder.title_image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.outfitters_logo));
            break;

        case "ChenOne":
            holder.title_image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.chenone_logo));
            break;

        case "Breakout":
            holder.title_image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.breakout_logo));
            break;

        case "Khaadi":
            holder.title_image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.khaadi_logo));
            break;
    }
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {

    CircleImageView title_image;
    TextView title, description, rupees, likes, likes_tv, price_tv;
    ImageView image;

    public MyViewHolder(View itemView) {
        super(itemView);

        title = (TextView)itemView.findViewById(R.id.title);
        title_image = (CircleImageView) itemView.findViewById(R.id.title_image);
        description = (TextView)itemView.findViewById(R.id.description);
        rupees = (TextView)itemView.findViewById(R.id.rupees);
        image = (ImageView)itemView.findViewById(R.id.image);
        likes = (TextView)itemView.findViewById(R.id.likes);
        likes_tv = (TextView)itemView.findViewById(R.id.likes_tv);
        price_tv = (TextView)itemView.findViewById(R.id.price_tv);  //<--- Wants to set its value from dismissProgressBar()
    }
}}

存储信息:

public class ComparisonResultRecyclerViewInfoStore {
String title, description, image, likes, rupees, type;

public ComparisonResultRecyclerViewInfoStore()
{

}

public String getTitle() {
    return title;
}

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

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getLikes() {
    return likes;
}

public void setLikes(String likes) {
    this.likes = likes;
}

public String getRupees() {
    return rupees;
}

public void setRupees(String rupees) {
    this.rupees = rupees;
}

}

回收站视图布局:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp"
    app:contentPadding="5dp"
    app:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/title_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            app:civ_border_color="@color/item_checked"
            app:civ_border_width="2dp"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="17dp"
            android:layout_toEndOf="@+id/title_image"
            android:paddingLeft="10dp"
            android:text="Outfitters"
            android:textColor="#fff"
            android:textSize="14dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/likes_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="17dp"
            android:layout_toEndOf="@+id/title"
            android:paddingLeft="25dp"
            android:text="Likes Textview"
            android:textColor="#fff"
            android:textSize="14dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sponsored_tv"
            android:layout_below="@id/title"
            android:layout_toEndOf="@+id/title_image"
            android:paddingLeft="10dp"
            android:text="Sponsored"
            android:textColor="#fff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/price_tv"
            android:layout_below="@id/likes_tv"
            android:layout_toEndOf="@+id/sponsored_tv"
            android:paddingLeft="25dp"
            android:text="Price Textview"    
            android:textColor="#fff"
            android:textSize="14dp"
            android:textStyle="bold"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/action_overflow"
            android:layout_marginTop="17dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/ic_action_overflow"
            android:layout_alignParentRight="true"
            />

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title_image"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_marginTop="10dp"
            android:text="Hello World"
            android:textColor="#fff" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/description"
            android:layout_marginTop="10dp"
            android:adjustViewBounds="true"
            android:background="#dfe1ee"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/likes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/image"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="Hello World"
            android:textColor="#fff" />

        <TextView
            android:id="@+id/rupees"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/image"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:gravity="right"
            android:text="Hello World"
            android:textColor="#fff"
            />

    </RelativeLayout>

</android.support.v7.widget.CardView>

在一个活动中,在将所有数据加载到回收器视图中后,我将检查dismissProgressBar(),在该视图中检查品牌商品的最低价格。我有需要设置其price_tv值的商品的位置,但是我不知道如何从活动中进行设置。

public void dismissProgressBar()
{
        SalesRecyclerViewAdapter.progressDialog.dismiss();

        for(int i=0; i<comparisonList.size(); i++)
        {
            priceList.add(parseDouble(comparisonList.get(i).getRupees()));

            Toast.makeText(this, comparisonList.get(i).getTitle()+"---"+comparisonList.get(i).getRupees(), Toast.LENGTH_SHORT).show();
        }

        int min_price_index = priceList.indexOf(Collections.min(priceList));
}

有帮助吗? price_tv不是来自数据库。它是静态的,我想在获得最低价格商品的索引时设置其值。

2 个答案:

答案 0 :(得分:1)

让我帮助您理解问题,然后自己解决问题。

假设 我在recyclerview中,假设我要更新recyclerview的第三项。我需要做的就是获取在recyclerview中使用的项目列表,即myList。

现在是时候更新视图了,我将抓住myList.get(3)项目并更新其中的值。

在您的情况下为ComparisonResultRecyclerViewInfoStore,因此代码为:

    ComparisonResultRecyclerViewInfoStore thirdItem = myList.get(3);

    thirdItem.setRupees(newValue);

///现在是时候让recyclerview知道我已经更改了第三项,以便Recyclerview可以更新视图。为此

yourAdapter.notifyItemChanged(3);

,我们完成了卢比值的更新。价格值和所有其他值也一样。请记住,您正在bindView方法中使用此值,即

holder.price_tv.setText(myList.get(holder.getAdapterPosition()).getPrice());

如果您只想为一项设置值,而对其他项则设置为静态,则在其上方进行检查:

if(holder.getAdapterPosition()== min_price_index){
   holder.price_tv.setText(lowest price value);
}else{
   holder.price_tv.setText("");
}

希望这会对您有所帮助。

答案 1 :(得分:0)

您必须更新适配器内的数据。例如:

您的活动中:

yourAdapter.refreshData(i, obj);

在您的适配器中

public void refreshData(int i, myObject obj) {
            this.myList.set(i, obj);
            notifyDataSetChanged();
        }