gpt4 book ai didi

java - Android ListView 适配器 - 隐藏项目

转载 作者:行者123 更新时间:2023-12-01 22:11:55 26 4
gpt4 key购买 nike

我有一个 android listView ,它将像这样填充:

final ListView userList = (ListView)root.findViewById(R.id.userList);
UserListAdapter adapter = new UserListAdapter(context, userItemList,
getActivity());
userList.setAdapter(adapter);

UserListAdapter 看起来像这样:

if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.user_item_list, null);
}
final TextView txtButtonFollow = (TextView) convertView.findViewById(R.id.button);
if(user == "showed") {
button.setVisibility(View.INVISIBLE);
}else {
button.setVisibility(View.INVISIBLE);
}
return convertView;

出于某种原因,所有用户按钮都会显示(首先),但是如果我上下滚动列表并“重新输入”用户项,按钮就会被隐藏(这就是我需要的)。

有人遇到同样的问题吗?是否无法隐藏“滚动”元素?我是否必须设置两个 user_item_list(一个有按钮,另一个没有按钮)?

编辑:这里完整的 getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
UserItem userItem = userItems.get(position);
RelativeLayout userListItemMain = (RelativeLayout)convertView.findViewById(R.id.userListItemMain);
String isContact = userItem.getIsContact();

if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}

final TextView txtButton = (TextView) convertView.findViewById(R.id.button);
if(isContact) {
txtButton .setVisibility(View.VISIBLE);
}else{
txtButton .setVisibility(View.INVISIBLE);
}
return convertView;
}

最佳答案

您可以更改此设置

 UserListAdapter adapter = new UserListAdapter(context, userItemList,getActivity());

UserListAdapter adapter = new UserListAdapter(userItemList,getActivity());

getActivity() 将为您提供上下文。

相应地更改适配器构造函数

使用ViewHolder模式

public static class ViewHolder
{
TextView txtButton ;
}

在getView中

  ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.user_item_list, null);
holder.txtButton = (TextView) convertView.findViewById(R.id.button);
convertView.setTag(holder)
} else {

holder = (ViewHolder) convertView.getTag();
}
UserItem userItem = userItems.get(position);
String isContact = userItem.getIsContact();

if(isContact.equals("showed")) {
holder.txtButton .setVisibility(View.VISIBLE);
}else{
holder.txtButton .setVisibility(View.INVISIBLE);
}

return convertView;

你的 if 语句

 if(isContact) { // makes no sense. isContact is not boolean

关于java - Android ListView 适配器 - 隐藏项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657698/

26 4 0
文章推荐: java - Spring MVC 在类 'xxxx' 中找不到属性 'xxxx' 的 setter
文章推荐: java - 在 Spring Boot Controller 中反序列化 json 时 List null