使用listview删除sqlite行

时间:2013-12-09 13:32:14

标签: android sqlite android-layout android-listview android-sqlite

我有一个连接到sqlite数据库的listview。首先它显示了我的sqlite字段。但我的问题是,我无法通过setOnLongClickDelete()删除行。

错误:

The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
        at android.widget.ListView.layoutChildren(ListView.java:1510)

代码:

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();
        setOnLongClickDelete();
    }
    private void displayResultList() {
       // setListAdapter(new ArrayAdapter<String>(this,R.layout.cartformat,results));
        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.cartformat,results);
        setListAdapter(listAdapter);
        listAdapter.notifyDataSetChanged();
        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");
        }

    }

    private void setOnLongClickDelete(){
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
                try{ String currentString = results.get(position);
                    String resultRegexString = "Title\\:([^,]+),Quantity\\: ([^,]+), Price\\: \\$([\\W\\w]+)";
                    Pattern resultRegexPattern = Pattern.compile(resultRegexString);
                    Matcher resultRegexMatcher = resultRegexPattern.matcher(currentString);

                    if(resultRegexMatcher.find()){
                        String whereClause = "title=".concat(DatabaseUtils.sqlEscapeString(resultRegexMatcher.group(1))
                                .concat(" AND qty=").concat(resultRegexMatcher.group(2))
                                .concat(" AND price=").concat(resultRegexMatcher.group(3)));

                       SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
                        database.delete("CART", whereClause, null);
                        database.close();
                        Toast.makeText(getApplicationContext(), "Row Delete", Toast.LENGTH_SHORT).show();
                        results.remove(position);
                    }
                    return true;
                }  catch (Exception ex){
                    Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
                    return false;
                }
            }

        } );
        displayResultList();
    }
}

跟随Toast工作:

Toast.makeText(getApplicationContext(), "Row Delete", Toast.LENGTH_SHORT).show();

1 个答案:

答案 0 :(得分:0)

如果您能够成功删除SQLite数据库中的数据,那么您只需将listAdapter全局设为全局,然后添加代码

listAdapter.notifyDataSetChanged();

results.remove(position);

onItemLongClick中。在此之后,您可以移除displayResultList();中的setOnLongClickDelete()来电。