首页 > 解决方案 > Listview 只有最后一个日期被删除

问题描述

以下代码包含用于显示来自 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();
}

标签: androidlistviewcardview

解决方案


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

试试这个

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

推荐阅读