如何用自定义的基本适配器连接sqlite数据库来刷新Listview?

0pizxfdo  于 2022-12-13  发布在  SQLite
关注(0)|答案(1)|浏览(135)

我已经被这个问题困扰了一个星期了,我有一个列表视图对话框片段,它使用了一个自定义的基本适配器,并与sqlite数据库连接。
我的数据库适配器:

public class DBAdapter {
// Column Product
static final String ROWID = "id";
static final String NAME = "name";
static final String DESC = "desc";
static final String PRICE = "price";
static final String DISPLAY = "display";
// DB Properties
static final String DBNAME = "db_prototype";
static final String TBNAME = "tbl_product";
static final int DBVERSION = 1;

static final String CREATE_TABLE = "CREATE TABLE tbl_product(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL," +
        " desc TEXT NOT NULL, price TEXT NOT NULL, display INTEGER NOT NULL)";

final Context c;
SQLiteDatabase db;
DBHelper helper;

public DBAdapter(Context c) {
    this.c = c;
    helper = new DBHelper(c);
}

private static class DBHelper extends SQLiteOpenHelper{
    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try{
            db.execSQL(CREATE_TABLE);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(DBHelper.class.getName(), "Upgrading DB");
        db.execSQL("DROP TABLE IF EXIST tbl_product");
    }
}

// Open Database
public DBAdapter openDB(){
  try{
      db = helper.getWritableDatabase();
  }catch (SQLException e){
      e.printStackTrace();
  }
    return this;
}

public void closeDB(){
    helper.close();
}

// Insert Into Table
public long add(String name, String desc, String price, int display){
    try{
        ContentValues cv = new ContentValues();
        cv.put(NAME, name);
        cv.put(DESC, desc);
        cv.put(PRICE, price);
        cv.put(DISPLAY, display);
        return db.insert(TBNAME, ROWID, cv);
    }catch (SQLException e) {
        e.printStackTrace();
    }
    return 0;
}

// Delete Table
public long delete(String name){
    try{
        return db.delete(TBNAME, NAME + "='" + name + "'", null);
    }catch (SQLException e) {
        e.printStackTrace();
    }
    return 0;
}

// Get All Value
public Cursor getAllValue(){
    String[] columns = {ROWID, NAME, DESC, PRICE, DISPLAY};
    return db.query(TBNAME, columns, null, null, null, null, null);
}
}

我的列表视图适配器(void refreshAdapter用于刷新数据集):

public class CartAdapter extends BaseAdapter {

private Context c;
private ArrayList<Integer> display;
private ArrayList<String> nama;
private ArrayList<String> harga;

public CartAdapter(Context c, ArrayList<Integer> display, ArrayList<String> nama, ArrayList<String> harga) {
    this.c = c;
    this.display = display;
    this.harga = harga;
    this.nama = nama;
}

@Override
public int getCount() {
    return nama.size();
}

@Override
public Object getItem(int position) {
    return nama.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.cart_display, null);
    }

    // Get View
    TextView txtNama = (TextView) convertView.findViewById(R.id.txtNama);
    TextView txtHarga = (TextView) convertView.findViewById(R.id.txtHarga);
    ImageView imgGambar = (ImageView) convertView.findViewById(R.id.imgGambar);
    //Assign Data
    txtNama.setText(nama.get(position));
    txtHarga.setText(harga.get(position));
    imgGambar.setImageResource(display.get(position));
    return convertView;
}

public void refreshAdapter(ArrayList<Integer> display, ArrayList<String> nama, ArrayList<String> harga){
    this.display.clear();
    this.harga.clear();
    this.nama.clear();
    this.display = display;
    this.harga = harga;
    this.nama = nama;
    this.notifyDataSetChanged();
}
}

我的列表视图对话框片段:

public class CartDialog extends DialogFragment {

ArrayList<String> cart_name;
ArrayList<String> cart_price;
ArrayList<Integer> cart_pict;
DBAdapter dbAdapter;
CartAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_cart_dialog, null);
    ListView LV = (ListView) rootView.findViewById(R.id.listCart);
    Button btnDelete = (Button) rootView.findViewById(R.id.button);

    // Prepare ArrayList to assign with DB
    cart_pict = new ArrayList<Integer>();
    cart_name = new ArrayList<String>();
    cart_price = new ArrayList<String>();

    getDialog().setTitle("Keranjang Belanjaan");
    dbAdapter = new DBAdapter(getActivity());
    adapter = new CartAdapter(getActivity(), cart_pict, cart_name, cart_price);
    refreshDB();
    LV.setAdapter(adapter);

    LV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String name = adapter.getItem(position).toString();
            // Delete Selected Item on SQLite Database
            dbAdapter.openDB();
            long result = dbAdapter.delete(name);
            dbAdapter.closeDB();
            //Refresh Cart
            refreshDB();
            adapter.refreshAdapter(cart_pict, cart_name, cart_price);
        }
    });
    return rootView;
}

public void refreshDB(){
    // Refresh Data
    dbAdapter.openDB();
    Cursor c = dbAdapter.getAllValue();
    while(c.moveToNext()){
        String name = c.getString(1);
        String price = c.getString(3);
        int display = c.getInt(4);
        cart_name.add(name);
        cart_price.add(price);
        cart_pict.add(display);
    }
    Toast.makeText(getActivity(), "Jumlah: " + c.getCount(), Toast.LENGTH_SHORT).show();
    dbAdapter.closeDB();
}
}

因此,每当我点击列表视图中的一个项目时,DBAdapter就会从SQLite数据库中删除这些项目,然后CartAdapter会刷新列表视图。我一直在寻找这个问题的参考,添加notifyDatasetChange (),但问题是在我调用refreshData()方法后,列表视图中的数据将为空。

zlwx9yxi

zlwx9yxi1#

试试这个兄弟

public void refreshDB(){
// Refresh Data
ArrayList<Integer> displayBaru = new ArrayList<Integer>();
ArrayList<String> namaBaru = new ArrayList<String>();
ArrayList<String> hargaBaru = new ArrayList<String>();
dbAdapter.openDB();
Cursor c = dbAdapter.getAllValue();
while(c.moveToNext()){
    String name = c.getString(1);
    String price = c.getString(3);
    int display = c.getInt(4);
    namaBaru.add(name);
    hargaBaru.add(price);
    displayBaru.add(display);
}
adapter.refreshAdapter(displayBaru, namaBaru, hargaBaru);
dbAdapter.closeDB();
}

相关问题