删除listview和数据库中的行

时间:2017-11-07 08:20:50

标签: android sqlite listview delete-row

我设计了一个自定义列表视图,其中包含图像,文本和图像按钮删除。当我按下图像按钮时,我想在桌面数据库和刷新列表视图上同时删除listview上的这一行。问题是app没有正确删除id。示例:表数据库上的id为1 2 3列表视图上的4位置为0 1 2 3.删除行后:id为1 2 4,位置为0 1 2.任何人帮我正确删除listview和数据库上的行然后刷新列表视图,拜托。这是代码:

  listViewMon.setAdapter(new CartArrayAdapter(ctx, R.layout.single_list_cart, listtao));
    listViewMon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public String variable;

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {
            CartGetSetListView o = (CartGetSetListView) adapterView.getItemAtPosition(position);
            variable = o.getTenmon().toString();

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    CartActivity.this);
            // Setting Dialog Title
            alertDialog.setTitle("XÓA MÓN");
            // Setting Dialog Message
            alertDialog.setMessage("Bạn có muốn xóa " + "[" + variable + "]" + String.valueOf(position) + " khỏi giỏ hàng?");
            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.warning);
            // Setting Positive "Yes" Button
            alertDialog.setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            db.delete_order("tbl_order", String.valueOf(position + 1));
                            Toast.makeText(CartActivity.this, "Đã xóa: " + variable + String.valueOf(position + 1), Toast.LENGTH_SHORT).show();
                            load_cart();

                        }
                    });

            alertDialog.show();
        }
    });
}

删除行:

    public void delete_order(String table_name, String id) {
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("delete from " + table_name + " where ID_book ='" + id + "'");
}

将数据从表添加到数组列表

 public void load_cart() {

    ctx = this;
    db.querydata("Create table if not exists tbl_order (ID_book integer primary key, IDCH_book integer not null, ID_table integer not null, IDmon_book integer not null, TT_tt text not null, Datetime_book tex not null)");
    Cursor curs = db.getdata("select * from tbl_order, tbl_mon_app where tbl_order.IDmon_book = tbl_mon_app.IDMon");
    int cc = curs.getCount();
    if (curs.moveToFirst()) {

        while (!curs.isAfterLast()) {
            ArrImgUrl.add(curs.getString(curs.getColumnIndex("ImgUrl")));
            curs.moveToNext();
        }
    }
    if (curs.moveToFirst()) {

        while (!curs.isAfterLast()) {
            ArrTenmon.add(curs.getString(curs.getColumnIndex("TenMon")));
            curs.moveToNext();
        }
    }
    if (curs.moveToFirst()) {

        while (!curs.isAfterLast()) {         
            ArrGia.add(curs.getString(curs.getColumnIndex("Gia")));
            curs.moveToNext();
        }
    }

    for (int i = 0; i < cc; i++) {

        listtao.add(new CartGetSetListView(ArrImgUrl.get(i), ArrTenmon.get(i), ArrGia.get(i), "delete1"));

    }
    curs.close();
    db.close();
}

ArrayAdapter:

public class CartArrayAdapter extends ArrayAdapter<CartGetSetListView> {
private int resource;
private LayoutInflater inflater;
private Context context;
URL url;
Bitmap bmp = null;

public CartArrayAdapter(Context ctx, int resourceId, List<CartGetSetListView> objects) {
    super(ctx, resourceId, objects);
    resource = resourceId;
    inflater = LayoutInflater.from(ctx);
    context = ctx;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = (LinearLayout) inflater.inflate(resource, null);
    CartGetSetListView cart = getItem(position);

    ImageView imgMon = (ImageView) convertView.findViewById(R.id.imgMon);
    TextView tenMon = (TextView) convertView.findViewById(R.id.tenMon);
    tenMon.setText(cart.getTenmon());

    ImageButton imgDel = (ImageButton) convertView.findViewById(R.id.Del);
    TextView gia = (TextView) convertView.findViewById(R.id.giaMon);
    gia.setText("Giá: " + cart.getGia() + " vnđ");

    Picasso.with(getContext()).load(cart.getImgurlmon()).fit().into(imgMon);

    String uri1 = "drawable/" + cart.getImgdel();
    int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getPackageName());
    Drawable image1 = context.getResources().getDrawable(imageResource1);
    imgDel.setImageDrawable(image1);

    return convertView;
    }
}

cartgetsetListview

public class CartGetSetListView {
public CartGetSetListView(String imgurlMon, String tenmon, String gia, String imgDel) {
    super();
    this.imgurlmon = imgurlMon;
    this.tenmon = tenmon;
    this.gia = gia;
    this.imgdel = imgDel;
}

private String imgurlmon;
private String tenmon;
private String gia;
private String imgdel;

public String getImgurlmon() {
    return imgurlmon;
}
public void setImgurlmon(String imgurlmon) {
    this.imgurlmon = imgurlmon;
}

public String getTenmon() {
    return tenmon;
}

public void setTenmon(String tenmon) {
    this.tenmon = tenmon;
}

public String getGia() {
    return gia;
}

public void setGia(String gia) {
    this.gia = gia;
}

public String getImgdel() {
    return imgdel;
}

public void setImgdel(String imgdel) {
    this.imgdel = imgdel;
    }
}

2 个答案:

答案 0 :(得分:0)

adapter中,编写方法 public int getID(int position) 返回position中元素的ID。然后,当您从position获得AdapterView.OnItemClickListener时。通过致电adapter.getID(position)获取ID。我强烈建议不要使用position+1来获取从数据库中删除行的ID。删除成功后,您可以调用适配器上的notifyDataSetChanged()来更新列表视图。此外,您可能想要使用RecyclerView。它比ListView

更有效,更强大

答案 1 :(得分:0)

  1. bookid
  2. 添加更多变量CartGetSetListView
  3. 编辑:

    listViewMon.setAdapter(new CartArrayAdapter(ctx,R.layout.single_list_cart,listtao));

    CartArrayAdapter myadapter = new CartArrayAdapter(ctx,R.layout.single_list_cart,listtao)   listViewMon.setAdapter(myadapter);

  4. 修改

    db.delete_order(&#34; tbl_order&#34;,String.valueOf(position + 1));

  5.  String bookid=myadapter.getItem(position).bookid
     db.delete_order("tbl_order", bookid);