我有一个ListView
,每行都有一个按钮,我在Button
适配器中声明了ListView
,这个Button
是不可见的,但我需要更改Button
视图在某些情况下可见。
作为Button
适配器类中的Listview
声明,我无法访问它以更改其可见性。
我怎么能管理它?
这是我的Adapter
:
public class MessageSimpleAdapter extends SimpleAdapter {
public String mId;
private static List<HashMap<String, String>> listMapBGMessage;
private static Context context;
private static int resource;
private GroupMadeByUserActivity _recAct;
protected static int[] resourceList;
protected static String[] fromList;
private static class ViewHolder {
TextView[] tv_bid_group_name;
ImageView iv_bid_group_delete;
TextView[] tv_bid_group_comment;
int position;
}
public MessageSimpleAdapter(Context context,
List<HashMap<String, String>> data, int resource, String[] from,
int[] to, GroupMadeByUserActivity recAct) {
super(context, data, resource, from, to);
// save the ArrayList and context for later usage
MessageSimpleAdapter.listMapBGMessage = data;
MessageSimpleAdapter.context = context;
MessageSimpleAdapter.resource = resource;
_recAct = recAct;
resourceList = to;
fromList = from;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// declare it final so that it could be accessed from the inner class
final ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) MessageSimpleAdapter.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(MessageSimpleAdapter.resource,
parent, false);
holder = new ViewHolder();
holder.tv_bid_group_name = new TextView[fromList.length];
holder.tv_bid_group_comment = new TextView[fromList.length];
// get the textview's from the convertView
for (int i = 0; i < fromList.length; i++) {
holder.tv_bid_group_name[i] = (TextView) convertView
.findViewById(resourceList[i]);
holder.tv_bid_group_comment[i] = (TextView) convertView
.findViewById(resourceList[i]);
}
// get the phoneIcon and emailIcon as well from convertView
holder.iv_bid_group_delete = (ImageView) convertView
.findViewById(R.id.iv_bid_group_delete);
// add a listener for phone call
holder.iv_bid_group_delete
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GroupMadeByUserActivity.messageIdStatic = MessageSimpleAdapter.listMapBGMessage
.get(holder.position).get("IDMessage");
_recAct.new AsyncDeleteMessage().execute(GroupMadeByUserActivity.messageIdStatic);
}
});
// store it in a Tag as its the first time this view is generated
convertView.setTag(holder);
} else {
/* get the View from the existing Tag */
holder = (ViewHolder) convertView.getTag();
}
/* update the textView's text for this list view item/element */
for (int i = 0; i < fromList.length; i++) {
holder.tv_bid_group_name[i].setText(listMapBGMessage.get(position)
.get(fromList[i]));
}
// store the position/index for this list view element/item
holder.position = position;
return convertView;
}
}
答案 0 :(得分:0)
您必须更改适配器getView
中的可见性。检查条件并更改可见性。
答案 1 :(得分:0)
除了在适配器中更改它之外,我没有看到其他方法。 您的适配器是否知道决定的原因?如果没有,你可以在创建时传递它们。
如果您想将决定外包,您可以在getView
中执行此操作:
...
boolean isVisible = MyUtil.isButtonVisible(listMapBGMessage.get(position)); // pass the object or an id
int visibility = isVisible ? View.VISIBLE : View.GONE; // GONE or INVISIBLE
holder.iv_bid_group_delete.setVisibility(visibility);
通过这种方式,您可以在public static boolean isButtonVisible(...)
中集中精力,并且适配器不需要知道原因,只需做出决定。
答案 2 :(得分:0)
如下文字:
public View getView(...) {
...
holder.myBUTON.setVisibility(buttonsVisible ? View.VISIBLE : View.INVISIBLE)
...
}
boolean isVisible = MyUtil.isButtonVisible(listMapBGMessage.get(位置)); //通过 object或id int visibility = isVisible? View.VISIBLE:View.GONE; // GONE或INVISIBLE holder.iv_bid_group_delete.setVisibility(可见性);
之后:
notifyDataSetChanged();
或者您可以创建一个设置Visible或Invisible按钮的方法,并在通知后查看更改。