android - 删除 ListView 和sqlite的行

标签 android android-listview android-sqlite

我的以下代码在 ListView 中显示了 sqlite 的数据。现在我想写长按删除 ListView 和 sqlite 中的行。请帮我。我该怎么做?

public class CartList extends ListActivity {
    private ArrayList<String> results = new ArrayList<String>();
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(com.example.easyshopping.R.layout.cart);
        openAndQueryDatabase();
        displayResultList();
    }
    private void displayResultList() {
        setListAdapter(new ArrayAdapter<String>(this,
                R.layout.cartformat,results));
                getListView().setTextFilterEnabled(true);   }
        private void openAndQueryDatabase() {
        try {
            SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
            Cursor c = database.rawQuery("SELECT title,qty,price FROM CART;", null);
                         if (c != null ) {
                int totalPrice=0;
                if  (c.moveToFirst()) {
                    do {
                        String title = c.getString(c.getColumnIndex("title"));
                        int qty = c.getInt(c.getColumnIndex("qty"));
                        int price = c.getInt(c.getColumnIndex("price"));
                        int pricePerTitle=price*qty;
                        results.add("Title: " + title + ",  Quantity: " + qty+",  Price: $"+pricePerTitle);
                        totalPrice=totalPrice+pricePerTitle;
                    }while (c.moveToNext());
                }
               TextView tTotalPrice=(TextView)findViewById(com.example.easyshopping.R.id.txttotalprice);
                String total= Integer.toString(totalPrice);
               tTotalPrice.setText(total);
            }
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        }}}

最佳答案

最好的方法是开发自定义适配器。但如果你不想,这应该有效:

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(com.example.easyshopping.R.layout.cart);
    openAndQueryDatabase();
    displayResultList();
    setOnLongClickDelete();
}

private void displayResultList(){
    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.cartformat,results);
    setListAdapter(listAdapter);
    listAdapter.notifyDataSetChanged();
    getListView().setTextFilterEnabled(true);
}

private void setOnLongClickDelete(){
    getListView().setOnItemLongClickListener(new OnItemLongClickListener(){
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
            String currentString = results.get(position);

            String resultRegexString = "Title\\: ([^,]+), Quantity\\: ([^,]+), Price\\: \\$([\\W\\w]+)";
            Pattern resultRegexPattern = Pattern.compile(resultRegexString);
            Matcher resultRegexMatcher = resultRegexPattern.matcher(resultRegexString);

            if(resultRegexMatcher){
                SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
                String whereClause = "title=".concat(DatabaseUtils.sqlEscapeString(resultRegexMatcher.group(1))
                                     .concat(" AND qty=").concat(resultRegexMatcher.group(2))
                                     .concat(" AND price=").concat(resultRegexMatcher.group(3));

                database.delete("CART", whereClause, null);
            }
        }
        results.remove(position);
        displayResultList();
    });
}

关于android - 删除 ListView 和sqlite的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20458033/

相关文章:

android - Cursor getInt 返回负值

安卓 : create realm table without primary key

Android ListView 添加了两次项目

android.database.sqlite.SQLiteException :

android - 添加新项目时 ListView 闪烁

android - 不适合时在 ListView 中滚动(自定义选框) TextView 文本

android - 滑动以打开选项 ListView

android - 使用 ID 删除多行?

java - 从数据库中获取不重复的数据

android - 华为设备不会导入数据库