0

View Database XML file:

     <?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 class of View Database page:

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

            });

}

I have edited the code, now I can delete all the data in my page, but the issue is that, I have to navigate back and enter this FruitLog page to see the changes (all rows deleted). I want to see a immediate result when the user click 'Clear Log' Button without navigating back and front.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Riyas2329
  • 43
  • 3
  • 14

5 Answers5

1

This method in your db class:

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

In the onClickListener:

 public void onClick(View v) {
     FruitDB db = new FruitDB(YourClass.this);
     db.open();
         db.deleteAll();
     db.close();
 }
Ole
  • 7,899
  • 2
  • 29
  • 34
0

To delete entire table:

db.delete(DATABASE_TABLE, null, null);

To delete particular records in a table:

db.delete(DATABASE_TABLE, whereCondition, null);

for eg:
db.delete(DATABASE_TABLE, "KEY_NAME='mango'", null);

GAMA
  • 5,958
  • 14
  • 79
  • 126
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();
            }

            }

        });

create a method in your DBHelper class

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

}
Nishant
  • 32,082
  • 5
  • 39
  • 53
0

It will delete all records

public void deleteAll() {
    final SQLiteDatabase db = getWritableDatabase();
    db.delete(DATABASE_TABLE, null, null);
    db.close();
}
Tai Tran
  • 1,406
  • 3
  • 15
  • 27
  • its working now sir. but once i click 'delete all' it deletes all the data, but its not shown on the screen right away. i have to go back and come back to see the data gone. can i dont navigate back and front and see the immediate delete result on the same screen? – Riyas2329 Jun 05 '12 at 15:54
  • So, You need to update UI after delete? Can you post your UI code? – Tai Tran Jun 05 '12 at 16:00
0

Move this code to save.setOnClickListener(new View.OnClickListener(){..} after deleting or move it to 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);
Tai Tran
  • 1,406
  • 3
  • 15
  • 27