I have a problem with listview and custom adapters. Im trying to put the visibility of a button as gone when i click on it, an create a new element for the list to edit and change.
But it doesn't work. I think it is because the notifyOnDataChange puts the buttons visibility as visible again.
public class CustomAdapterIngredientsUser extends ArrayAdapter<Recipe.Ingredients.Ingredient>
List<Recipe.Ingredients.Ingredient> ingredientList;
Context context;
EditText textQuantity;
EditText textName;
Button xButton;
Button plusButton;
public CustomAdapterIngredientsUser(Context context, List<Recipe.Ingredients.Ingredient> resource) {
super(context,R.layout.ingredient_new_recipe,resource);
this.context = context;
this.ingredientList = resource;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
final View customview = layoutInflater.inflate(R.layout.ingredient_new_recipe,parent,false);
textQuantity = (EditText) customview.findViewById(R.id.quantityText);
textName = (EditText) customview.findViewById(R.id.ingredientName);
plusButton= (Button) customview.findViewById(R.id.newIngredientButton);
xButton = (Button) customview.findViewById(R.id.Xbutton);
xButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ingredientList.remove(position);
notifyDataSetChanged();
}
});
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
plusButton.setVisibility(customview.GONE);
String name = textName.getText().toString();
String qnt = textQuantity.getText().toString();
Recipe.Ingredients.Ingredient ing2 = new Recipe.Ingredients.Ingredient("Quantity","Name","Photo");
ingredientList.add(ing2);
notifyDataSetChanged();
}
});
return customview;
}
It should let add new elements to the list, and remove the button to add more elemenets, in the first one (The plus button). To let the user make a list of ingredients.
You are essentially correct; calling notifyDataSetChanged()
is going to make your button re-appear. But why?
When you call notifyDataSetChanged()
, your ListView will completely redraw itself, going back to your adapter for the information it needs. This involves calling getView()
for all currently-visible items in your list.
Your implementation of getView()
always inflates and returns customview
. Since you're always returning a newly-inflated view, all attributes of views that you do not manually set after inflation will be set to the values in your layout xml (or to defaults, if they are not set here).
The default value for visibility is VISIBLE
, so when you inflate customview
, your plusButton
will always be visible unless you manually change it to GONE
.
You will have to store some sort of indication about whether the button at a given position
should be visible or gone, and then apply this inside getView()
after you inflate customview
.
PS: In general, it is a bad idea to inflate a new view every time getView()
is called. You should leverage the convertView
argument and the "view holder" pattern. This will improve the performance of your app. Searching Google and this site should give you some ideas about this.