将数据从对话框插入SQLi后,不会刷新自定义列表视图

问题描述 投票:0回答:2

我从像这样的对话框插入数据到SQLi

 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ContentValues cv = new ContentValues();
                        cv.put(DBHelper.COL_VEHICLE_TYPE, 1);
                        cv.put(DBHelper.COL_OPTION_NAME, "driver");
                        cv.put(DBHelper.COL_DATE, mDisplayDate.getText().toString());

                        db.insert(DBHelper.TABLE_NAME, null, cv);
                        cursor.requery();
                        adapter.notifyDataSetChanged();

                    }
                });

我在函数onResume()中查询数据,以便像这样在列表视图中显示。

public void onResume() {

    super.onResume();

    lst_driver.clear();

    db = dbHelper.getWritableDatabase();
    String[] queryColumns = new String[]{"_id", DBHelper.COL_VEHICLE_TYPE, DBHelper.COL_OPTION_NAME,DBHelper.COL_DATE };
    cursor = db.query(DBHelper.TABLE_NAME, queryColumns, null,null,
           null,null,null);

    HashMap<String,String> map ;
    String vh_name;

    while(cursor.moveToNext())
    {
        map = new HashMap<String,String>();

        vh_name= dbHelper.VehicleType(cursor.getString(1));
        map.put("vehicle_type", vh_name);
        map.put("date", cursor.getString(3));
        lst_driver.add(map);


    }

    String[] showColumns = new String[]{"vehicle_type", "date"};
    int[] views = new int[] {R.id.ColType, R.id.ColDate};

    adapter = new SimpleAdapter(DriverLicenseActivity.this,lst_driver, R.layout.activity_list_layout, showColumns, views);
    lv_driver.setAdapter(adapter);

    adapter.notifyDataSetChanged();

}

插入数据后,它不会显示在列表视图中。我必须再次打开此活动,然后它将出现在列表视图中。插入到SQLi后如何在列表视图中显示数据?

android listview android-adapter
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.