简体   繁体   English

如何从活动类更新recyclerview textview

[英]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<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.在我检查品牌商品的最低价格的回收站视图中加载所有数据后,在活动中调用dismissProgressBar()。 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.我有需要设置 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));
}

Any Help?任何帮助? price_tv is not coming from database. price_tv 不是来自数据库。 Its static and i want to set its value when i get the index of lowest price item.它是静态的,我想在获得最低价格项目的索引时设置它的值。

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.假设我在 recyclerview 中,假设我想更新 recyclerview 的第三项。 All I need to do is that get items List which I'm using inside recyclerview ie myList.我需要做的就是获取我在 recyclerview 中使用的项目列表,即 myList。

Now when it is time to update the view I'll grab the myList.get(3) item and update value inside it.现在,当需要更新视图时,我将获取myList.get(3)项并更新其中的值。

In your case it is ComparisonResultRecyclerViewInfoStore so code will be:在您的情况下,它是ComparisonResultRecyclerViewInfoStore因此代码将是:

    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. //现在是时候让recyclerview知道我已经更改了第三个Item,以便Recyclerview可以更新视图。 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 ie请记住,您在 bindView 方法中使用此值,即

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.希望这会帮助你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个类更新 Activity 的 TextView - How to update a TextView of an Activity from another class android:如何从扩展RecyclerView的类中更新徽章TextView - android: How to update badge TextView from a class that extends RecyclerView 如何从不扩展活动 class 的 class 更新 TextView - How to update TextView from a class that doesn't extend Activity class 从另一个Activity的RecyclerView返回时如何更新Activity的textview? - How to update Activity's textview when returning from another Activity's RecyclerView? 当TextView不在recyclerView的行中时,如何在Adapter类中获取活动的TextView? - How to get TextView of an activity in an Adapter class while that TextView is not in row of recyclerView? 如何从 recyclerview 适配器类更改主要活动中 textview 的文本 - How change text of textview inside main activity from recyclerview adapter class 单击 RecyclerView 后在主活动中更新 TextView - Update TextView in main activity after clicking on RecyclerView 如何在另一个 Activity 中将 TextView 的值发送到 RecyclerView - How to send a value from TextView to RecyclerView in another Activity 如何将 TextView 文本从 Recyclerview 传递到 Android 中的其他活动 - How to Pass TextView text from Recyclerview to other Activity in Android 如何从活动更新导航 Header 中的 TextView? - How to update TextView in Navigation Header from Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM