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

时间:2012-06-05 09:27:11

标签: 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: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" />

查看数据库页面的Java类:

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

            });

}

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

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

delete整个表:

db.delete(DATABASE_TABLE, null, null);

表格中的delete个特定记录:

db.delete(DATABASE_TABLE, whereCondition, null);

例如:
db.delete(DATABASE_TABLE,“KEY_NAME ='mango'”,null);

答案 2 :(得分:0)

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

                DbHelper myDbHelper =new DbHelper(Activity.this);

            try{
                myDbHelper.open();
                myDbHelper.delete();
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                myDbHelper.close();
            }

            }

        });

在DBHelper类中创建一个方法

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

}

答案 3 :(得分:0)

它将删除所有记录

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

答案 4 :(得分:0)

删除后将此代码移至save.setOnClickListener(new View.OnClickListener(){..} 或将其移至onResume()

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