0

我有一个连接到一个sqlite数据库的列表视图。起初它显示我的sqlite领域。但我的问题是,我不能通过setOnLongClickDelete()删除行。使用列表视图删除sqlite行

错误:

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.makeText(getApplicationContext(), "Row Delete", Toast.LENGTH_SHORT).show(); 
+0

你没有在'onItemLongClick'方法中调用'listAdapter.notifyDataSetChanged();'' –

回答

0

如果你能够从SQLite数据库中成功删除的数据,那么你只需做您的listAdapter全球,然后添加代码

listAdapter.notifyDataSetChanged(); 

results.remove(position); 

onItemLongClick后。在此之后,您可以删除setOnLongClickDelete()中的呼叫displayResultList();