简体   繁体   English

当TextView不在recyclerView的行中时,如何在Adapter类中获取活动的TextView?

[英]How to get TextView of an activity in an Adapter class while that TextView is not in row of recyclerView?

How to get TextView of an Activity in RecyclerViewAdapter class and TextView is not in layout of row For RecyclerView item.As in RecyclerViewAdapter we override method 如何在RecyclerViewAdapter类中获取活动的TextView,而TextView不在行的布局中对于RecyclerView项目。与RecyclerViewAdapter中一样,我们重写了方法

onBindViewHolder(ViewHolder holder, int position){
   NewsModel newsModel=values.get(position);  //to get the current object
        holder.news_title.setText(newsModel.getTitle());
        holder.news_body.setText(newsModel.getDescription());
        holder.news_published_at.setText("Date "+GiveMeDate(values.get(position).getDate()));}  

and as it get only those which are declared in ViewHolder extends RecyclerView.ViewHolder and ViewHolder will only get ui that are in View v = inflater.inflate(R.layout.news_item, parent, false); 并且,当它仅获取在ViewHolder extends RecyclerView.ViewHolder中声明的ViewHolder extends RecyclerView.ViewHolder和ViewHolder将仅获取View v = inflater.inflate(R.layout.news_item, parent, false); ui View v = inflater.inflate(R.layout.news_item, parent, false); but I want to get ui from activity's layout so that I can update TextView value in Adapter So please Help to do that work. 但我想从活动的布局中获取ui,以便可以在Adapter中更新TextView值, 因此请帮助完成该工作。 Image to reflect Layout is as follows: 反映布局的图像如下:

在此处输入图片说明

  • You should pass Textview as a parameter in adapter when you pass from activity 从活动传递时,应在适配器中将Textview作为参数传递
  • Second you should done this by using interface. 其次,您应该使用接口来完成此操作。
  • Another option is you can get textview by using Object of Activity. 另一个选择是您可以通过使用Activity对象来获取textview。

like this MyActivity.java 像这样的MyActivity.java

 MyActivity myActivity;
 public TextView tv_name;

Adapter.java Adapter.java

myActivity.tv_name.setText("InsaneCat");

Hope this help you 希望这对您有帮助

I find that the UI of Activity to be used in Adapter class. 我发现在Adapter类中使用Activity的UI。 We should make it static in Activity and then make a static method to update its value, after that whenever we want to change its value from Adapter we will call that static method with actual parameter and it will be updated .I do it as follows 我们应该在Activity中将其设为静态,然后创建一个静态方法来更新其值,此后,每当我们要从Adapter更改其值时,我们都将使用实际参数调用该静态方法并对其进行更新。

In Activity 活动中

  public static TextView counter_value;
   public static  void update_counter(String value){
        try{
            counter_value.setText(value);
        }
        catch (Exception ex){
            Log.d("Exception","Exception of type"+ex.getMessage());
        }
    }

In Adapter class just call that static method 在Adapter类中,只需调用该静态方法

News.update_counter(String.valueOf(counter));

得到它

TextView requiredTextView = (TextView) ActivityName.findViewById(R.id.requiredTextView);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM