android - 在 Activity 中,更改listview可见性的按钮

  显示原文与译文双语对照的内容
0 0

我有一个 ListView的按钮,我在 ListView 适配器中声明 Button,这个 Button 是不可以见的,但我需要更改 Button 视图。 作为 ListView 适配器类中的Button 声明,我无法访问它以更改它的可见性。 我怎么能管理它?

这是我的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 中的可见性。 检查条件并更改可见性。

0 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);

这样你就可以把desicion集中在 public static boolean isButtonVisible(...) 适配器不需要知道原因,只需获取决策。

原作者:
0 0

像这样的文件:


public View getView(...) {
. . .
 holder.myBUTON.setVisibility(buttonsVisible? View.VISIBLE : View.INVISIBLE)
. . .
 } 

布尔 isVisible = MyUtil.isButtonVisible(listMapBGMessage.get(position));//传递对象或者 id int可见性= isVisible? View.VISIBLE: View.GONE;//消失或者不可见的holder.iv_bid_group_delete.setVisibility(visibility) ;

之后:

notifyDataSetChanged() ;

也可以创建用于设置可见或者不可见按钮的方法,也可以创建notifiy以查看更改。

原作者:
...