[英]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
Button
, TextView
的视图元素。
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
. 另外,我有得到它为我工作,因为这里的一些意见提到的申报问题
ViewHolder
外newView
刚过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
访问诸如Button
, TextView
等的视图。
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.