Android: 使用列表视图中的索引从数据库中删除 - 无法访问使用视图创建但不是列表基础的 ID 数组

Android: delete from database using index from listview - Can't access array of id's that is created with the view but not the basis for the list

我有一个简单的列表视图,其中每个项目都是一个具有标题的视图,来自字符串和按钮的 ArrayList,因此 ArrayList 中的每个条目都会创建一个新的列表项目。

我还有另一个对应主键的 ArrayList,我想用它从 SQLite 数据库中删除特定项目,但在列表视图中没有使用(我不想显示 ID,但是填充列表的字符串可能不一定是唯一的,所以我不能用它们来删除)。

我在列表视图的 getView 方法中有一个 onClick 侦听器和方法,因此当有人单击删除按钮时,我知道按钮被按下的列表中的位置,所以希望我可以然后使用 id[position] 在数据库上调用删除方法,但是,我认为由于列表视图本身是在 activity 之后创建的,它无法解析 id 数组,所以我不能调用删除。

public class TodayListActivity extends AppCompatActivity {

   private  ArrayList<String> names = new ArrayList<>();

   FoodDB Db = null;
   int deleteId;


   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_todaylist);
    ListView lv = (ListView) findViewById(R.id.today_meal_list);

    Bundle a = this.getIntent().getExtras();

    String[] id = a.getStringArray("idArray"); //used to delete
    String[] mealNames = a.getStringArray("mealNamesArray"); //displayed

    Collections.addAll(names, mealNames);

    //call the list adapter to create views based off the array list 'names'
    lv.setAdapter(new MyListAdapter(this, R.layout.list_item, names));
}

  protected class MyListAdapter extends ArrayAdapter<String> {
    private int layout;

    private MyListAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        layout = resource;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder viewholder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(layout, parent, false);
            viewholder = new viewHolder();
            viewholder.title = (TextView) convertView.findViewById(R.id.report_meal_name);
            viewholder.delButton = (Button) convertView.findViewById(R.id.button_delete_meal);
            viewholder.delButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (Integer)v.getTag();
                    //int deleteId derived from id[position]
                    deleteId = Integer.parseInt(id[position]);
                    idToDelete(deleteId);
                    //update the list view to exclude the deleted item
                    names.remove(position);
                    notifyDataSetChanged();
                }
            });

            convertView.setTag(viewholder);
        } else {
            viewholder = (viewHolder) convertView.getTag();
        }
        //set string value for title
        viewholder.title.setText(getItem(position));
        viewholder.delButton.setTag(position);
        return convertView;



    }
}
public class viewHolder {
    TextView title;
    TextView delButton;
}
//delete from database
public void idToDelete(int DeleteId){
    Db.deleteFoods(deleteId);
}

}

关于如何或在何处从列表视图中获取位置索引(到 activity,id 数组所在的位置)或访问列表视图中的 id 数组的任何建议将是赞赏!

您可以将 id 数组传递给 MyListAdapter 适配器,方法是更改​​此 class' 构造函数以接受它作为参数。此外,您已经将 names 列表作为参数传递,您应该保留对它的引用,以便在按下按钮时可以访问它。

这是一个例子:

protected class MyListAdapter extends ArrayAdapter<String> {
    private int layout;
    private List<String> names;
    private String[] ids;

    private MyListAdapter(Context context, int resource, List<String> names, String[] ids) {
        super(context, resource, names);
        layout = resource;
        this.names = names;
        this.ids = ids;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ...
        viewholder.delButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int deleteId = Integer.parseInt(ids[position]);// the "position" variable needs to be set to "final" in order to access it in here.
                idToDelete(deleteId);
                names.remove(position);
                notifyDataSetChanged();
            }
        });
        ....
    }
}

下面是创建此适配器实例的方法:

lv.setAdapter(new MyListAdapter(this, R.layout.list_item, names, id));