Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用与sqlite数据库连接的自定义基本适配器刷新Listview?_Sqlite_Listview_Custom Adapter - Fatal编程技术网

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

如何使用与sqlite数据库连接的自定义基本适配器刷新Listview?,sqlite,listview,custom-adapter,Sqlite,Listview,Custom Adapter,我已经陷入这个问题一个星期了。我有一个listview对话框片段,它使用一个自定义的基本适配器并连接到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 = "pri

我已经陷入这个问题一个星期了。我有一个listview对话框片段,它使用一个自定义的基本适配器并连接到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[] collumns = {ROWID, NAME, DESC, PRICE, DISPLAY};
    return db.query(TBNAME, collumns, null, null, null, null, null);
}
}
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();
}
}
我的Listview适配器(void刷新适配器以刷新数据集):

公共类CartAdapter扩展了BaseAdapter{
私有上下文c;
专用阵列列表显示;
私人ArrayList nama;
私人ArrayList harga;
公共CartAdapter(上下文c、ArrayList显示、ArrayList nama、ArrayList harga){
这个.c=c;
this.display=显示;
this.harga=harga;
this.nama=nama;
}
@凌驾
public int getCount(){
返回nama.size();
}
@凌驾
公共对象getItem(int位置){
返回nama.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutInflater充气器=(LayoutInflater)c.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.cart_显示,空);
}
//查看
TextView txtNama=(TextView)convertView.findViewById(R.id.txtNama);
TextView txtCharga=(TextView)convertView.findViewById(R.id.txtCharga);
ImageView imgGambar=(ImageView)convertView.findViewById(R.id.imgGambar);
//分配数据
txtNama.setText(nama.get(position));
txtHarga.setText(harga.get(position));
imgGambar.setImageResource(display.get(position));
返回视图;
}
公用适配器(ArrayList显示、ArrayList nama、ArrayList harga){
this.display.clear();
这个.harga.clear();
这个.nama.clear();
this.display=显示;
this.harga=harga;
this.nama=nama;
this.notifyDataSetChanged();
}
}
我的列表视图对话框片段:

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[] collumns = {ROWID, NAME, DESC, PRICE, DISPLAY};
    return db.query(TBNAME, collumns, null, null, null, null, null);
}
}
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();
}
}
公共类CartDialog扩展了DialogFragment{
ArrayList购物车名称;
arraylistcart_价格;
ArrayList cart_pict;
DBAdapter DBAdapter;
卡托适配器;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.fragment\u cart\u对话框,空);
ListView LV=(ListView)rootView.findViewById(R.id.listCart);
按钮btnDelete=(按钮)rootView.findviewbyd(R.id.Button);
//准备要与DB一起分配的ArrayList
cart_pict=new ArrayList();
cart_name=new ArrayList();
购物车价格=新的ArrayList();
getDialog().setTitle(“Keranjang Belanjaan”);
dbAdapter=newdbadapter(getActivity());
适配器=新的CartAdapter(getActivity(),购物车图片,购物车名称,购物车价格);
refreshDB();
低压设置适配器(适配器);
LV.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串名称=adapter.getItem(position.toString();
//删除SQLite数据库上的选定项
dbAdapter.openDB();
long result=dbAdapter.delete(名称);
dbAdapter.closeDB();
//刷新购物车
refreshDB();
刷新适配器(购物车图片、购物车名称、购物车价格);
}
});
返回rootView;
}
公共数据库(){
//刷新数据
dbAdapter.openDB();
游标c=dbAdapter.getAllValue();
while(c.moveToNext()){
String name=c.getString(1);
字符串价格=c.getString(3);
int display=c.getInt(4);
购物车名称。添加(名称);
购物车价格。添加(价格);
购物车图片添加(显示);
}
Toast.makeText(getActivity(),“Jumlah:+c.getCount(),Toast.LENGTH_SHORT).show();
dbAdapter.closeDB();
}
}
所以,每当我在listview中单击一个项目时,DBAdapter就会从SQLite数据库中删除这些项目,然后CartAdapter就会刷新listview。我一直在寻找有关此问题的引用,添加
notifyDatasetChange()
,但问题是在调用
refreshData()
方法后,listview中的数据将为空。

尝试此方法

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();
}
public void refreshDB(){
//刷新数据
ArrayList displayBaru=新的ArrayList();
ArrayList namaBaru=新的ArrayList();
ArrayList hargaBaru=新的ArrayList();
dbAdapter.openDB();
游标c=dbAdapter.getAllValue();
while(c.moveToNext()){
String name=c.getString(1);
字符串价格=c.getString(3);
int display=c.getInt(4);
namaBaru.add(姓名);
加上(价格);
displayBaru.add(显示);
}
适配器。刷新适配器(displayBaru、namaBaru、hargaBaru);
dbAdapter.closeDB();
}