【问题标题】:Listview only the last date get deletedListview 只有最后一个日期被删除
【发布时间】:2019-05-25 06:46:22
【问题描述】:

以下代码包含用于显示来自 sqlite 的数据的 listview 函数。

问题是无论我选择哪个数据,它只会删除最后一个数据,而不是正在选择的数据。我该如何解决这个问题?请帮忙。

谢谢。

Arealist.java

public class Arealist extends AppCompatActivity {

    public static ListView listView;
    public static ArrayList<Area> list;
    AreaListAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        listView = (ListView) findViewById(R.id.listView);
        list = new ArrayList<>();
        adapter = new AreaListAdapter(this, R.layout.area_items, list);
        listView.setAdapter(adapter);            

        //get all data
        Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM AREA");
        list.clear();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            double area = cursor.getDouble(1);
            String date = cursor.getString(2);

            list.add(new Area(id, area, date));
        }

        adapter.notifyDataSetChanged();
}

【问题讨论】:

    标签: android listview cardview


    【解决方案1】:

    将区域对象设为本地。

    将删除按钮中的Area Id设置为Tag并在onClick()中获取。

    添加 getView()

     holder.btnDelete.setTag(area.getId());
    

    添加 OnClick()

    final int areaId = view.getTag();
    

    在 AlertDialog 中添加:

     MainActivity.sqLiteHelper.deleteData(areaId);
    

    【讨论】:

      【解决方案2】:

      看来您的问题出在这一行 MainActivity.sqLiteHelper.deleteData(Area.getId());

      试试这个

      MainActivity.sqLiteHelper.deleteData(area.getId());
      

      【讨论】:

      • 感谢您的回复。我已经尝试过你的建议,但它给了我同样的错误。
      猜你喜欢
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多