我有一个listview和自定义适配器的问题。我试图把一个按钮的可见性当我点击它时消失了,为列表创建一个新的元素来编辑和更改。更改自定义适配器中按钮的可见性
但它不起作用。我认为这是因为notifyOnDataChange将按钮的可见性再次显示为可见。
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;
}
应该让新元素添加到列表中,并删除按钮来添加更多的elemenets,在第一个(加号按钮)。让用户列出配料清单。
评论notifyDataSetChange()行并再次检查。 –