【问题标题】:delete row of listview and sqlite删除 listview 和 sqlite 行
【发布时间】:2013-12-08 19:37:52
【问题描述】:

我的以下代码在列表视图中显示了 sqlite 的数据。现在我想写长按删除列表视图和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");
        }}}

【问题讨论】:

    标签: android android-listview android-sqlite


    【解决方案1】:

    最好的方法是开发一个自定义适配器。但是,如果您不想要,这应该可以:

    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();
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2013-10-06
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多