使用按钮删除带有 ArrayAdapter 的 Listview 中的选定项目

Using button to delete selected item in Listview w/ ArrayAdapter

我已经使用 Java 几个月了,但对 Android 还是个新手。这是一个食谱夹应用程序。

我在 ListView 上设置了一个 ArrayAdapter,我需要 select ListView 中的一个项目并使用 Button 删除它(我已经成功地设置了向 Listview 添加字符串)。我使用 ArrayList 作为用于存储成分对象的列表,而不是常规数组。我正在尝试使用 AdapterView.onSelectedItemListener 在 ListView 中识别用户 selection,然后使用按钮删除 selected 项目。对于按钮,我正在实施 Button.onClickItemListener.

为了将列表项放入 ListView,我使用了一个对话框。我正在使用一个接口将字符串输入从对话框发送到 RecipeFragmentNew 中的 ListView。我一直没有遇到从对话框中获取字符串到 RecipeFragmentNew 的问题,所以我没有包含任何代码。

问题:按钮正在删除列表项,但它删除的是列表中的第一项,而不是正在 selected 的项。

Recipe.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

    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;
    }
}

使用 mRecipe.getIngredients() 从食谱 class 中检索 arrayList。

ListView 是 mIngredientWindow。

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) {

        }
    };

简单

listViewPosition 变量总是等于 0

这就是原因,您已经从 AdapterView.OnItemSelectedListener 定义了一个对象,但您从未将其附加到列表视图

您只需添加

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.

变量 listViewPosition 始终为 0,因为未调用 setOnItemSelectedListener。请检查这个 answer 所以你可以像下面的代码一样用 setOnItemClicklistener 替换这个方法:

mIngredientWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //mIngredientWindow.setSelection(position);
            listViewPosition = position;

        }
    });