简体   繁体   English

Android:如何从外部bindView()方法访问viewHolder?

[英]Android: How do you access viewHolder from outside bindView() method?

Below is a sample code of what I want to achieve. 下面是我要实现的示例代码。

    public class ToDoCursorAdapter extends CursorAdapter {
    ......

    public ToDoCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        ViewHolderItem viewHolder = new ViewHolderItem();
        View rowView = LayoutInflater.from(context).inflate(R.layout.listview, parent, false);

        viewHolder.refTextV = (TextView) rowView.findViewById(R.id.refText);
        viewHolder.buttonIRV = (Button) rowView.findViewById(R.id.buttonIR); //For Ref Text
        .....

        rowView.setTag(viewHolder);
        return rowView;
    }

    @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        final ViewHolderItem viewHolder = (ViewHolderItem) view.getTag();

        ......

        viewHolder.refTextV.setText(ref);

        ......
    }

    public void hideShowAll(Button but) {

       //I want  to be able to acess BUTTONS/TEXTVIWEs from this function.
       // Like viewHolder.refTextV.setText(ref) in binView

        Toast.makeText(mContext, "Hello" , Toast.LENGTH_SHORT).show();
    }
 }

I'm trying to access elements of view like Button , TextView in bindView from another function within the adapter. 我正在尝试从适配器内的另一个函数访问bindView ButtonTextView的视图元素。

Also, please add a little bit of explanation, if you can, to your answers so that I can understand how it works and may learn something for future as well. 另外,如果可以的话,请在回答中添加一些解释,以便我理解它的工作原理,并可能为将来学习。

Thank you 谢谢

UPDATE 更新

I finally used notifyDataSetChanged(); 我终于使用notifyDataSetChanged(); in my hideShowAll() instead of accessing ViewHolder in there. 在我的hideShowAll()而不是在那里访问ViewHolder I stored changes in an array and then called notifyDataSetChanged() . 我将更改存储在数组中,然后称为notifyDataSetChanged() It refreshed my views and reflected the changes I wanted to make based on array. 它刷新了我的观点,并反映了我想基于数组进行的更改。

Also, I had issue with get it work for me because some comments here mentioned to declare ViewHolder outside newView just after Constructor . 另外,我有得到它为我工作,因为这里的一些意见提到的申报问题ViewHoldernewView刚过Constructor This is not a good idea. 这不是一个好主意。 You will get wrong ViewHolder instead of one that belongs to a particular row. 您将得到错误的ViewHolder而不是属于特定行的ViewHolder

尝试向构造函数和所有Override方法外部声明“ ViewHolderItem viewHolder ”,并初始化为bindView方法

First, initialize your ViewHolderItem globally then you can access your views like Button , TextView etc in another method like this. 首先,在全局范围内初始化ViewHolderItem ,然后您可以通过另一种method访问诸如ButtonTextView等的视图。

 public class ToDoCursorAdapter extends CursorAdapter {
    ......
// initialize ViewHolderItem here
ViewHolderItem viewHolder = new ViewHolderItem();
 @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        //remove this line here
       // ViewHolderItem viewHolder = new ViewHolderItem();
        View rowView = LayoutInflater.from(context).inflate(R.layout.listview, parent, false);

        viewHolder.refTextV = (TextView) rowView.findViewById(R.id.refText);
        viewHolder.buttonIRV = (Button) rowView.findViewById(R.id.buttonIR); //For Ref Text
        .....

        rowView.setTag(viewHolder);
        return rowView;
    }

public void hideShowAll(Button but) {

       //call it like this and do what you want
        viewHolder.buttonIRV.setEnabled(false);
        Toast.makeText(mContext, "Hello" , Toast.LENGTH_SHORT).show();
    }

Hope it'll help you. 希望对您有帮助。

Try this : 尝试这个 :

  public class ToDoCursorAdapter extends CursorAdapter {
......

public ToDoCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    ViewHolderItem viewHolder = new ViewHolderItem();
    View rowView = LayoutInflater.from(context).inflate(R.layout.listview, parent, false);

    viewHolder.refTextV = (TextView) rowView.findViewById(R.id.refText);
    viewHolder.buttonIRV = (Button) rowView.findViewById(R.id.buttonIR); //For Ref Text
    .....

    rowView.setTag(viewHolder);
    return rowView;
}

@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
    final ViewHolderItem viewHolder = (ViewHolderItem) view.getTag();

    ......

    viewHolder.refTextV.setText(ref);

    ......
}

public void hideShowAll(Button but, ViewHolderItem viewHolder) {

   //I want  to be able to acess BUTTONS/TEXTVIWEs from this function.
    viewHolder.refTextV.setText("hello");

    Toast.makeText(mContext, "Hello" , Toast.LENGTH_SHORT).show();
}
}

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

相关问题 Android:如何访问位置的视图(从适配器外部) - Android: How to access viewholder at position (from outside the adapter) android使用viewholder out bindview() - android using viewholder out of bindview() 如何从适配器外部访问Recyclerview.ViewHolder参数? - How to access Recyclerview.ViewHolder params from outside of adapter? 从ViewHolder或ViewHolder的ViewModel访问RecyclerView.Adapter的方法 - access RecyclerView.Adapter's method from ViewHolder or ViewModel of ViewHolder 如何在Android中的游标适配器的bindview中进行日期解析和格式化 - How to do Date parsing and formating inside a bindview of cursor adapter in android 如何将数据从 BindView 方法中的适配器发送到活动中? - How to send data from adapter in method BindView into activity? 如何从Activity / Fragment调用ViewHolder方法 - How to call ViewHolder method from Activity/Fragment 如何在Android中从ViewHolder更改布局的颜色? - How to change the color of layout from ViewHolder in Android? 从片段访问内部类方法(来自适配器的Viewholder) - Access Inner Class Method (from Viewholder in adapter) from a Fragment Android - 如何在getView中获取bindView中的位置? - Android - How to get position in bindView like in getView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM