Android 如何在ArrayAdapter的getView方法中更新视图?
我有以下问题。我有一个Android 如何在ArrayAdapter的getView方法中更新视图?,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,我有以下问题。我有一个ListView,我正在使用一个自定义适配器扩展ArrayAdapter类。每一行都有一个follow按钮,当我点击它时,我需要改变它的样式 到目前为止,我已经: public View getView(final int position, View convertView, ViewGroup parent) { View vi = convertView; if (convertView == null) {
ListView
,我正在使用一个自定义适配器扩展ArrayAdapter
类。每一行都有一个follow按钮,当我点击它时,我需要改变它的样式
到目前为止,我已经:
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.people_item, null);
mViewHolder = new ViewHolder();
mViewHolder.follow = (Button) vi.findViewById(R.id.people_item_btn_follow);
mViewHolder.name = (TextView)....
vi.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.follow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Changing the style of the button
if(mData[position].getFollow().equals("0")) {
mViewHolder.follow.setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border));
mViewHolder.follow.setText(mCtx.getString(R.string.unfollow));
} else {
mViewHolder.follow.setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.follow_button_border));
mViewHolder.follow.setText(mCtx.getString(R.string.follow));
}
mSharedAsyncTasks.getFollowerTask().execute(mData[position].getId());
}
});
if(mData[position] != null) {
// Setting data
}
return vi;
}
private static class ViewHolder {
TextView fullName;
Button follow;
}
问题是,当单击任意行的按钮时,新样式将应用于另一行的按钮(尽管以下效果将应用于右行)
我知道这与该行被回收/再利用的事实有关
但如何真正解决这个问题呢
谢谢 不确定是否要执行以下操作
public void onClick(View v) {
mViewHolder.follow.setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border));
...
}
具体来说,我认为问题在于你的参考mViewHolder,它看起来像是挂在周围的,在你滚动时可以指向任何按钮。这是一个范围问题,您应该能够通过以下方法解决。在onClick(视图v)中,我相信是您单击的按钮
相反,您应该能够执行以下操作
public void onClick(View v) {
((Button)v).setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border));
...
}
不确定是否要执行以下操作
public void onClick(View v) {
mViewHolder.follow.setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border));
...
}
具体来说,我认为问题在于你的参考mViewHolder,它看起来像是挂在周围的,在你滚动时可以指向任何按钮。这是一个范围问题,您应该能够通过以下方法解决。在onClick(视图v)中,我相信是您单击的按钮
相反,您应该能够执行以下操作
public void onClick(View v) {
((Button)v).setBackgroundDrawable(mCtx.getResources().getDrawable(R.drawable.unfollow_button_border));
...
}