Home > Mobile >  Inquired database insert. SetOnClickListener (this); Why run error?
Inquired database insert. SetOnClickListener (this); Why run error?

Time:09-27

This is Main3Activity, namely MainActivity:



Package com. Example. Xiangmu;

The import android. Content. ContentValues;
The import android. Database. Cursor;
The import android. Database. Sqlite. SQLiteDatabase;
The import android. OS. Bundle;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. The EditText;
The import android. Widget. TextView;

The import androidx. Appcompat. App. AppCompatActivity;

Public class Main3Activity extends AppCompatActivity implements the View. An OnClickListener {
Public void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);

initView();
}

Public void initView () {
The Button insert=(Button) the findViewById (R.i d.i nsert_button);
The Button insert_cleardata=https://bbs.csdn.net/topics/(Button) the findViewById (R.i, dc lear_insert_button);

The Button update=(Button) the findViewById (R.i d.u pdate_button);
The Button update_cleardata=https://bbs.csdn.net/topics/(Button) the findViewById (R.i, dc lear_update_button);

The Button delete=(Button) the findViewById (R.i d.d elete_button);
The Button delete_cleardata=https://bbs.csdn.net/topics/(Button) the findViewById (R.i, dc lear_delete_button);

The Button query=(Button) the findViewById (R.i d.q uery);
The Button clearquery=(Button) the findViewById (R.i, dc lear_query);

//button object set for all listeners
Insert. SetOnClickListener (this);
Insert_cleardata. SetOnClickListener (this);

Update. SetOnClickListener (this);
Update_cleardata. SetOnClickListener (this);

Delete. SetOnClickListener (this);
Delete_cleardata. SetOnClickListener (this);

Query. SetOnClickListener (this);
Clearquery. SetOnClickListener (this);
}

Public void onClick (View View) {
The EditText insert_text=(EditText) the findViewById (R.i d.i nsert_text);
String insert_data=(https://bbs.csdn.net/topics/insert_text.getText). The toString ();

The EditText delete_text=(EditText) the findViewById (R.i d.d elete_text);
String delete_data=(https://bbs.csdn.net/topics/delete_text.getText). The toString ();

The EditText update_before_text=(EditText) the findViewById (R.i d.u pdate_before_text);
String update_before_data=(https://bbs.csdn.net/topics/update_before_text.getText). The toString ();
The EditText update_after_text=(EditText) the findViewById (R.i d.u pdate_after_text);
String update_after_data=(https://bbs.csdn.net/topics/update_after_text.getText). The toString ();

TextView TextView=(TextView) the findViewById (R.i d.q uery_text);

//has built a test_db database
DatabaseHelper DatabaseHelper=new DatabaseHelper (this, "test_db", null, 1);
SQLiteDatabase db=databaseHelper. GetWritableDatabase ();

The switch (the getId ()) {
Case R.i d.i nsert_button:
ContentValues values=new ContentValues ();
Values. The put (" name ", insert_data);
The insert (" user ", null values);
break;
Case R.i, dc lear_insert_button:
Insert_text. SetText (" ");
break;
Case R.i d.d elete_button:
The delete (" user ", "name=?" , new String [] {delete_data});
break;
Case R.i, dc lear_delete_button:
Delete_text. SetText (" ");
break;
//update the data button
Case R.i d.u pdate_button:
ContentValues values2=new ContentValues ();
Values2. Put (" name ", update_after_data);
The update (" user ", values2, "name=?" , new String [] {update_before_data});
break;
//update the data button at the back of the clear button
Case R.i, dc lear_update_button:
Update_before_text. SetText (" ");
Update_after_text. SetText (" ");
break;

//query all button
Case R.i d.q uery:
//create a cursor object
Cursor Cursor=db. Query (" user ", new String [] {} "name", null, null, null, null, null);
//use cursor traverses all data objects
//in order to show all, put all objects connected, in the TextView
String textview_datahttps://bbs.csdn.net/topics/="";
While (cursor. MoveToNext ()) {
String name=cursor. Get String (cursor. GetColumnIndex (" name "));
Textview_data=https://bbs.csdn.net/topics/textview_data + "\ n" + name;
}
TextView. SetText (textview_data);
break;
//query button below to remove all the query button
Case R.i, dc lear_query:
TextView. SetText (" ");
TextView. SetHint (" query content is empty ");
break;

Default:
break;
}
}
}



This is the DatabaseHelper:


The import androidx. The annotation. Nullable;
The import androidx. Appcompat. App. AppCompatActivity;

The import android. The content. The Context;
The import android. Database. Sqlite. SQLiteDatabase;
The import android. Database. Sqlite. SQLiteOpenHelper;
The import android. OS. Bundle;

Import the Java. SQL. SQLException;

Public class DatabaseHelper extends SQLiteOpenHelper {

Public DatabaseHelper (@ Nullable Context Context, @ Nullable String name, @ Nullable SQLiteDatabase. CursorFactory factory, int version) {
Super (context, name, factory, version);
}

@ Override
Public void onCreate (SQLiteDatabase db) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related