我有一個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()行並再次檢查。 –