在活动中更改列表视图的按钮可见性
Posted
技术标签:
【中文标题】在活动中更改列表视图的按钮可见性【英文标题】:change listview's button visibility in an activity 【发布时间】:2014-08-04 02:24:48 【问题描述】:我有一个ListView
,它的每一行都有一个按钮,我在我的ListView
适配器中声明了Button
,这个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;
【问题讨论】:
您说的Button
是什么,“我无法访问”是什么意思?您是如何尝试访问它的,又是如何失败的?
您只能在适配器的 getView 方法中更改 Visibility。
【参考方案1】:
您必须更改适配器 getView
中的可见性。检查条件并更改可见性。
【讨论】:
【参考方案2】:除了在您的适配器中更改它之外,我没有看到其他方法。 您的适配器是否知道做出该决定的原因?如果没有,您可以在创建时传递它们。
如果您想将决策本身外包,您可以在 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(...)
中,而适配器不需要知道原因,而只是得到决定。
【讨论】:
【参考方案3】:一个像这样的文件:
public View getView(...)
...
holder.myBUTON.setVisibility(buttonsVisible ? View.VISIBLE : View.INVISIBLE)
...
布尔 isVisible = MyUtil.isButtonVisible(listMapBGMessage.get(position)); // 传递 对象或 id int visibility = isVisible ? View.VISIBLE : View.GONE; // 消失或不可见 holder.iv_bid_group_delete.setVisibility(visibility);
之后:
notifyDataSetChanged();
或者您可以创建一个方法来设置可见或不可见按钮并在通知后查看更改。
【讨论】:
以上是关于在活动中更改列表视图的按钮可见性的主要内容,如果未能解决你的问题,请参考以下文章