Home Android: How do you access viewHolder from outside bindView() method?
Reply: 3

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

Abu Qatada
1#
Abu Qatada Published in 2017-09-14 05:33:43Z

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.

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(); in my hideShowAll() instead of accessing ViewHolder in there. I stored changes in an array and then called 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. This is not a good idea. You will get wrong ViewHolder instead of one that belongs to a particular row.

Sagar Vasoya
2#
Sagar Vasoya Reply to 2017-09-14 05:39:35Z

Try to declare 'ViewHolderItem viewHolder' below to constructor and outside of all Override method and init into bindView method

Hassan Usman
3#
Hassan Usman Reply to 2017-09-14 05:42:45Z

First, initialize your ViewHolderItem globally then you can access your views like Button, TextView etc in another method like this.

 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.

R.R.M
4#
R.R.M Reply to 2017-09-14 05:53:22Z

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();
}
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.400028 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO