【问题标题】:Delete selected item from listview and sqlite Android从 listview 和 sqlite Android 中删除所选项目
【发布时间】:2018-09-02 21:28:18
【问题描述】:

我的编程项目有问题。我正在创建一个购物应用程序,我只需要单击一个按钮从 listview 和 mysqlite 数据库中删除选定的数据。我还在我的 sqlite 上创建了一个查询,它将删除主键自动增量整数。

这是我的课:

public class CartPage extends AppCompatActivity {

    DatabaseHelper myDB;

    public Button button_delete;
    public Button button_home;
    public TextView textView_totalAmount;
    public TextView textView_PK;
    public ListView listView_datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart_page);

        myDB = new DatabaseHelper(this);

        listView_datas = (ListView) findViewById(R.id.listView_ShoppingData);
        button_delete = (Button) findViewById(R.id.button_deleteData);
        button_home = (Button) findViewById(R.id.button_HomePage);
        textView_totalAmount = (TextView) findViewById(R.id.textView_DisplayTotalAmount);

        int total = myDB.addPrice();
        textView_totalAmount.setText("$" + Integer.toString(total));

        final ArrayList<String> list_cart = new ArrayList<>();
        final Cursor data = myDB.getPrice_cart();

        if (data.getCount() == 0) {
            Toast.makeText(CartPage.this, "The Database is empty..", Toast.LENGTH_LONG).show();
        } else {
            while (data.moveToNext()) {
                list_cart.add(data.getString(1) + "\n" +
                        data.getString(2) + "\n");

                ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, list_cart);
                listView_datas.setAdapter(listAdapter);
            }
        }

        button_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goHome = new Intent(CartPage.this, FirstPage.class);
                startActivity(goHome);
            }
        });
    }
}

这是我的数据库助手查询,用于按主键删除数据

public int deleteSelectedItem(String number){
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(table_Cart,"number = ?" , new String[] {number} );
}

对于这种情况,我正在考虑使用,但我不知道该怎么做。

listView_datas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
});

我还看到了当您按住所选项目时它会被删除的位置。我不介意,只要它删除数据。

谢谢!

【问题讨论】:

  • 在项目中使用复选框来选择列表视图中的项目。对于所有选中的Item,调用delete方法

标签: java android sqlite listview


【解决方案1】:

我建议您在ListView 中实现一个长按监听器,然后长按列表中的项目,它将显示删除列表中相应项目的选项。这是一个可以帮助您的示例代码。

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> arg0, View view, final int position, long arg3) {
        AlertDialog.Builder alertDialog = new  AlertDialog.Builder(RecipeList.this);
        alertDialog.setTitle("Delete");
        alertDialog.setMessage(yourList.get(position));     
        alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                deleteSelectedItem(yourList.get(position))
            }
        }); 

        alertDialog.show();
        return true;
    }
});

是的,您需要在您的 deleteSelectedItem() 函数中调用 notifyDataSetChanged() 以查看列表中删除的效果。

public int deleteSelectedItem(String number){
    SQLiteDatabase db = this.getWritableDatabase();
    int result = db.delete(table_Cart,"number = ?" , new String[] {number} );
    // Update your list here
    // Remove the deleted item from the list that you have passed to the adapter. Then call notifyDataSetChanged
    updateYourListCart();
    myAdapter.notifyDataSetChanged();
}

【讨论】:

  • 你应该做deleteSelectedItem(yourList.get(position + 1))并传递position + 1,就像在SQLite数据库中一样,自动增量的主键从1开始,ListView索引从0开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-09
相关资源
最近更新 更多