Android 从数据库中删除数据时显示即时更改

Android 从数据库中删除数据时显示即时更改,android,database,sqlite,Android,Database,Sqlite,查看数据库XML文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android

查看数据库XML文件:

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@drawable/wood_bg" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:text="Daily Fruit Log"
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textStyle="bold"/>


<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow>

    <TextView
        android:layout_width="110dp"
        android:layout_height="fill_parent"
        android:text="Name of fruit" 
        android:layout_weight="1"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="No Of Fruit" 
        android:layout_weight="1"
        android:textStyle="bold"/>

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Total Calories" 
        android:layout_weight="1"
        android:textStyle="bold"/>

    </TableRow>
    </TableLayout>

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <TableRow>

    <TextView
    android:id="@+id/view"
    android:layout_width="110dp"
    android:layout_height="wrap_content"
    android:text="food"
    android:layout_weight="1" />

    <TextView
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="1" 
        android:layout_weight="1"/>

     <TextView
         android:id="@+id/view2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="20" 
        android:layout_weight="1"/>

    </TableRow>
    </TableLayout>

<Button
    android:id="@+id/bdelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear Log"
    android:layout_gravity="center"
    android:layout_marginTop="30dp" />
    public class FruitLog extends Activity {

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

            TextView tv = (TextView) findViewById(R.id.view);
            TextView tv1 = (TextView) findViewById(R.id.view1);
            TextView tv2 = (TextView) findViewById(R.id.view2);

            FruitDB info = new FruitDB(this);
            info.open();
            String data = info.getName();
            String data1 = info.getNum();
            String data2 = info.getCal();
            info.close();

            tv.setText(data);
            tv1.setText(data1);
            tv2.setText(data2);

            Button save = (Button) findViewById(R.id.bdelete);
            save.setTextColor(Color.BLUE);
            save.setOnClickListener(new View.OnClickListener(){

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    FruitDB info = new FruitDB(FruitLog.this);
                    info.open();
                    info.deleteAll();
                    info.close();
                }

            });

}
我已经编辑了代码,现在我可以删除我页面中的所有数据,但问题是,我必须返回并进入这个水果日志页面以查看更改(所有行都已删除)。
我希望在用户单击“清除日志”按钮而不前后导航时立即看到结果。

此方法在db类中:

public boolean deleteAll() {        
    // Returns true if number of deleted rows is larger than 0;
    return mDb.delete(DATABASE_TABLE, null, 1) > 0;
}
在onClickListener中:

 public void onClick(View v) {
     FruitDB db = new FruitDB(YourClass.this);
     db.open();
         db.deleteAll();
     db.close();
 }

要删除整个表,请执行以下操作:

db.delete(DATABASE_TABLE, null, null);

要删除表中的特定记录,请执行以下操作:

db.delete(DATABASE_TABLE, whereCondition, null);
例如:
db.delete(数据库表,“KEY\u NAME='mango',null)

在DBHelper类中创建一个方法

public void delete() {
     ourDatabase.execSQL("delete from "+ DATABASE_TABLE);
     ourDatabase.execSQL("UPDATE sqlite_sequence set "+ KEY_ROWID + " =0 where name= '"+DATABASE_TABLE+"'");

}

它将删除所有记录

public void deleteAll() {
    final SQLiteDatabase db = getWritableDatabase();
    db.delete(DATABASE_TABLE, null, null);
    db.close();
}

删除后,将此代码移动到
save.setOnClickListener(新视图.OnClickListener(){..}
或者将其移动到
onResume()


因此,您需要删除数据库,如果数据库不存在,它应该创建一个数据库。您可以使用execSQL调用删除查询,对吗?您想显示或删除数据库条目吗?我想在单击“清除日志”按钮时删除所有条目。先生,现在正在工作。但一旦单击“全部删除”,它将删除所有数据,但它不会显示在屏幕上马上。我必须回去看看数据是否消失了。我能不能不来回导航,在同一屏幕上看到立即删除的结果?那么,删除后你需要更新UI?你能发布你的UI代码吗?
public void deleteAll() {
    final SQLiteDatabase db = getWritableDatabase();
    db.delete(DATABASE_TABLE, null, null);
    db.close();
}
FruitDB info = new FruitDB(this);
        info.open();
        String data = info.getName();
        String data1 = info.getNum();
        String data2 = info.getCal();
        info.close();

        tv.setText(data);
        tv1.setText(data1);
        tv2.setText(data2);