创建一个按钮以删除所有表记录

时间:2015-04-07 23:40:24

标签: java android database

我试图创建一个按钮来删除整个表记录,但是我收到了这个错误消息" java.lang.IllegalStateException:在活动类com.example.android中找不到方法deleteAll(View) .ch2.user用于视图类android.widget.Button上的onClick处理程序,其id为' b_delete'             在android.view.View $ 1.onClick(View.java:4007)             在android.view.View.performClick(View.java:4780) 任何真正有用的想法。感谢

XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.ch2.user">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="View User"
    android:id="@+id/button20"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="68dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear"
    android:id="@+id/b_delete"
    android:layout_alignTop="@+id/button20"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="deleteAll"
    android:enabled="true"
    android:clickable="true" />
    </RelativeLayout>

Java代码:

package com.example.android.ch2;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;


public class user extends ActionBarActivity {

private static final String TABLE_NAME = "reg_info";

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


    }




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_user, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void deleteAll()
{

    SQLiteDatabase db = null;
    db.execSQL("delete * from "+ TABLE_NAME);

    db.close();
}}

JavaHelper

package com.example.android.ch2;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE "+ TableData.TableInfo.TABLE_NAME+"   ("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_EMAIL+" TEXT,"+ TableData.TableInfo.USER_FOOD+" TEXT);";
public DatabaseOperations(Context context){
    super(context, TableData.TableInfo.DATABASE_NAME, null, database_version);
    Log.d ("Database operations", "Database created");
}

@Override
public void onCreate(SQLiteDatabase sdb) {
    sdb.execSQL(CREATE_QUERY);
    Log.d("Database Operations", "Table created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void putInformation (DatabaseOperations dop, String name,String email,String food){

SQLiteDatabase SQ = dop.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(TableData.TableInfo.USER_NAME, name);
    cv.put(TableData.TableInfo.USER_EMAIL, email);
    cv.put(TableData.TableInfo.USER_FOOD, food);
    long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d ("Database operations", "One raw inserted");

}

}

1 个答案:

答案 0 :(得分:0)

您的方法签名应为public void deleteAll(View view)

修改

好的,为什么我们不尝试这个:

public void deleteAll(View view) {
    DatabaseOperations helper = new DatabaseOperations(this);
    SQLiteDatabase db = helper.getWritableDatabase();
    db.execSQL("DELETE FROM " + TableData.TableInfo.TABLE_NAME);
    db.close();
}