How to update recyclerview textview from activity class

问题: I am using recycler view in my activity. This is my adapter class: public class ComparisonResultRecyclerViewAdapter extends RecyclerView.Adapter<ComparisonResultRecycl...

问题:

I am using recycler view in my activity. This is my adapter class:

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()
    }
}}

Storing information:

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;
}

}

Recycler view layout:

    <?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>

In an activity dismissProgressBar() is called after all the data is loaded in recycler view where I check for the lowest price of the brand item. I have the position of the item whose price_tv value I need to set but I don't know how to set it from the activity.

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));
}

Any Help? price_tv is not coming from database. Its static and i want to set its value when i get the index of lowest price item.


回答1:

Let me help you to understand the problem and then you resolve your issue by yourself.

Suppose I'm in recyclerview and let's say I want to update the third item of recyclerview. All I need to do is that get items List which I'm using inside recyclerview i.e myList.

Now when it is time to update the view I'll grab the myList.get(3) item and update value inside it.

In your case it is ComparisonResultRecyclerViewInfoStore so code will be:

    ComparisonResultRecyclerViewInfoStore thirdItem = myList.get(3);

    thirdItem.setRupees(newValue);

//now it's time to let the recyclerview know that I have changed the third Item so that Recyclerview can update the view. To do this

yourAdapter.notifyItemChanged(3);

and we are done with the update of Rupees value. Same goes for price value and all other values. Keep in mind that you are using this value inside bindView method i.e

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

If you want to set value for only one item and for others it is static then put a check above it:

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

Hope this will help you.


回答2:

You have to update the data inside your adapter. For example:

in your activity:

yourAdapter.refreshData(i, obj);

in your adapter:

public void refreshData(int i, myObject obj) {
            this.myList.set(i, obj);
            notifyDataSetChanged();
        }
  • 发表于 2019-03-10 23:59
  • 阅读 ( 64 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除