[英]Using button to delete selected item in Listview w/ ArrayAdapter
I've been working with Java for a few months but new to Android.我已经与 Java 合作了几个月,但对 Android 来说是新手。 This is a recipe holder app.这是一个食谱持有人应用程序。
I have an ArrayAdapter set on a ListView and am needing to select an item in the ListView and delete it with a Button (I've already successfully set up adding of Strings to the Listview).我在 ListView 上设置了一个 ArrayAdapter,我需要 select ListView 中的一个项目并使用按钮将其删除(我已经成功设置了将字符串添加到 Listview)。 I'm using an ArrayList for the list used to store Ingredient objects rather than a regular array.我使用 ArrayList 作为用于存储成分对象的列表而不是常规数组。 I'm attempting to use AdapterView.onSelectedItemListener to identify a user selection in the ListView and then use the Button to delete the selected item.我正在尝试使用 AdapterView.onSelectedItemListener 来识别 ListView 中的用户选择,然后使用 Button 删除所选项目。 For the Button, I'm implementing Button.onClickItemListener.对于 Button,我正在实现 Button.onClickItemListener。
To get the list items into the ListView I'm using a dialog.要将列表项放入 ListView,我使用了一个对话框。 I'm using an interface to send the string input from the dialog to the ListView in RecipeFragmentNew.我正在使用一个接口将对话框中的字符串输入发送到RecipeFragmentNew 中的ListView。 I haven't been having issues getting the Strings from the dialog to RecipeFragmentNew, so I haven't included any of that code.我没有遇到从对话框中获取字符串到 RecipeFragmentNew 的问题,所以我没有包含任何代码。
Problem: Button is deleting list items but it is deleting the first item in the list, not the item that is being selected.问题:按钮正在删除列表项,但它正在删除列表中的第一项,而不是正在选择的项。
Recipe.java配方.java
public class Recipe {
private UUID mID;
private String mName;
private Date mDate;
private boolean mIsFavorite;
private final ArrayList<Ingredient> ingredients;
public final ArrayList<Instruction> instructions;
public Recipe() {
mID = UUID.randomUUID();
mDate = new Date();
this.ingredients = new ArrayList<>();
this.instructions = new ArrayList<>();
}
public UUID getID() {
return mID;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public Date getDate() {
return mDate;
}
public void setDate(Date date) {
mDate = date;
}
public boolean isFavorite() {
return mIsFavorite;
}
public void setFavorite(boolean favorite) {
mIsFavorite = favorite;
}
public ArrayList<Ingredient> getIngredients() {
return ingredients;
}
public ArrayList<Instruction> getInstructions() {
return instructions;
}
}
Ingredient.java成分.java
public class Ingredient {
private String name;
private String amount;
public Ingredient(String name, String amount) {
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}
public String getAmount() {
return amount;
}
@Override
public String toString() {
return this.name + " " + this.amount;
}
}
The arrayList is retreived from Recipe class using mRecipe.getIngredients().使用 mRecipe.getIngredients() 从配方 class 中检索 arrayList。
The ListView is mIngredientWindow. ListView 是 mIngredientWindow。
RecipeFragmentNew.java RecipeFragmentNew.java
public class RecipeFragmentNew extends Fragment implements IngredientListDialog.OnInputSelected {
public static final String TAG = "RecipeFragmentNew";
public static final String DIALOG_INGREDIENTS = "DialogIngredients";
private Recipe mRecipe;
private EditText mNameField;
private Button mIngredientAdd;
private Button mIngredientDelete;
private ListView mIngredientWindow;
private int listViewPosition;
private ArrayAdapter<Ingredient> listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID recipeId = (UUID) getArguments().getSerializable(ARG_RECIPE_ID);
mRecipe = RecipeQueue.get(getActivity()).getRecipe(recipeId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.lists_detail_view, container, false);
mIngredientDelete = v.findViewById(R.id.delete_ingredient_button);
mIngredientDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < mRecipe.getIngredients().size(); i++) {
if (i == listViewPosition) {
mRecipe.getIngredients().remove(i);
}
listAdapter.notifyDataSetChanged();
}
}
});
listAdapter = new ArrayAdapter<Ingredient>(
getActivity(),
android.R.layout.simple_list_item_1,
mRecipe.getIngredients()
);
mIngredientWindow = v.findViewById(R.id.ingredients_window);
mIngredientWindow.setAdapter(listAdapter);
AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
listViewPosition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
Simple简单的
the listViewPosition
variable always equals 0 listViewPosition
变量始终等于 0
Here's Why, You have defined an object from AdapterView.OnItemSelectedListener
but you never attache it to the listview这就是为什么,您已经从AdapterView.OnItemSelectedListener
但您从未将其附加到列表视图
all you need to do is add您需要做的就是添加
mIngredientWindow.setOnItemSelectedListener (itemSelectedListener );
Button is deleting list items but it is deleting the first item in the list, not the item that is being selected.按钮正在删除列表项,但它正在删除列表中的第一项,而不是正在选择的项。
The variable listViewPosition is always 0 because the setOnItemSelectedListener
is not called.变量listViewPosition始终为 0,因为未调用setOnItemSelectedListener
。 please check this answer So you can replace this method with setOnItemClicklistener
like the below code:请检查这个答案所以你可以用setOnItemClicklistener
替换这个方法,如下面的代码:
mIngredientWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//mIngredientWindow.setSelection(position);
listViewPosition = position;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.