Reputation: 1074
I have a android listView wich will filled like this:
final ListView userList = (ListView)root.findViewById(R.id.userList);
UserListAdapter adapter = new UserListAdapter(context, userItemList,
getActivity());
userList.setAdapter(adapter);
And the UserListAdapter looks like this:
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;
For some reason all user buttons will be showed (first), but if i scroll the list up and down and "reenter" the user-item, the buttons are hided (this is what i need).
Does anyone had the same problem? Is it not possible to hide elements "on-scroll"? Do i have to setup two user_item_list (one with the button and the other one without the button)?
Edit: Here full 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;
}
Upvotes: 0
Views: 865
Reputation: 133560
You can change this
UserListAdapter adapter = new UserListAdapter(context, userItemList,getActivity());
to
UserListAdapter adapter = new UserListAdapter(userItemList,getActivity());
getActivity()
will give you context.
Change the adapter constructor accordingly
Use a ViewHolder
Pattern
public static class ViewHolder
{
TextView txtButton ;
}
In 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;
Your if statement
if(isContact) { // makes no sense. isContact is not boolean
Upvotes: 3