我想以网格格式执行更新删除插入视图我的数据..我的CRUD操作工作正常但是一旦我在网格中查看我的数据,那么这些操作之后都没有执行,请帮助我
MainActivity.java
package com.example.sunny.sqlitedb;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
MyDBHelper dbHelper;
private EditText etID;
private EditText etFirstName;
private EditText etLastName;
private EditText etAddress;
private EditText etSalary;
private Button btnInsert;
private Button btnUpdate;
private Button btnDelete;
private Button btnLoadAll;
// private TextView tvFinalData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDBHelper(MainActivity.this);
init();
}
private void init() {
etID = (EditText) findViewById(R.id.etID);
etFirstName = (EditText) findViewById(R.id.etFirstName);
etLastName = (EditText) findViewById(R.id.etLastName);
etAddress = (EditText) findViewById(R.id.etAddress);
etSalary = (EditText) findViewById(R.id.etSalary);
btnInsert = (Button) findViewById(R.id.btnInsert);
btnUpdate = (Button) findViewById(R.id.btnUpdate);
btnDelete = (Button) findViewById(R.id.btnDelete);
btnLoadAll = (Button) findViewById(R.id.btnLoadAll);
btnInsert.setOnClickListener(dbButtonsListener);
btnUpdate.setOnClickListener(dbButtonsListener);
btnDelete.setOnClickListener(dbButtonsListener);
btnLoadAll.setOnClickListener(dbButtonsListener);
// tvFinalData = (TextView) findViewById(R.id.tvData);
}
private View.OnClickListener dbButtonsListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnInsert:
long resultInsert = dbHelper.insert(Integer.parseInt(getValue(etID)), getValue(etFirstName),
getValue(etLastName), getValue(etAddress), Double.valueOf(getValue(etSalary)));
if(resultInsert == -1){
Toast.makeText(MainActivity.this, "Some error occurred while inserting", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Data inserted successfully, ID: " + resultInsert, Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnUpdate:
long resultUpdate = dbHelper.update(Integer.parseInt(getValue(etID)), getValue(etFirstName),
getValue(etLastName), getValue(etAddress), Double.valueOf(getValue(etSalary)));
if(resultUpdate == 0){
Toast.makeText(MainActivity.this, "Some error occurred while updating", Toast.LENGTH_SHORT).show();
} else if(resultUpdate == 1) {
Toast.makeText(MainActivity.this, "Data updated successfully, ID: " + resultUpdate, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Some error occurred, multiple records updated.", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnDelete:
long resultDelete = dbHelper.delete(Integer.parseInt(getValue(etID)));
if(resultDelete == 0){
Toast.makeText(MainActivity.this, "Some error occurred while inserting", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Data deleted successfully.", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnLoadAll:
Intent i=new Intent(MainActivity.this,GridViewActivity.class);
startActivity(i);
/** StringBuffer finalData = new StringBuffer();
Cursor cursor = dbHelper.getAllRecords();
// Cursor cursor = dbHelper.getDataBasedOnQuery("SELECT * FROM " + MyDBHelper.TABLE_NAME
// + " WHERE " + MyDBHelper.ADDRESS + " = 'UK'");
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
finalData.append(cursor.getInt(cursor.getColumnIndex(MyDBHelper.ID)));
finalData.append(" - ");
finalData.append(cursor.getString(cursor.getColumnIndex(MyDBHelper.FIRST_NAME)));
finalData.append(" - ");
finalData.append(cursor.getString(cursor.getColumnIndex(MyDBHelper.LAST_NAME)));
finalData.append(" - ");
finalData.append(cursor.getString(cursor.getColumnIndex(MyDBHelper.ADDRESS)));
finalData.append(" - ");
finalData.append(cursor.getLong(cursor.getColumnIndex(MyDBHelper.SALARY)));
finalData.append("\n"); */
break;
// }
//tvFinalData.setText(finalData);
}
}
};
private String getValue(EditText editText) {
return editText.getText().toString().trim();
}
@Override
protected void onStart() {
super.onStart();
dbHelper.openDB();
}
@Override
protected void onStop() {
super.onStop();
dbHelper.closeDB();
}
}
这是我的GridViewActivity.java我在这里执行了我的网格,我必须打印sqlite数据
package com.example.sunny.sqlitedb;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by Sunny on 8/31/2016.
*/
public class GridViewActivity extends Activity {
private GridView gridView;
private ArrayList<String> list;
private ArrayAdapter<String> adapter;
MyDBHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
list = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list);
String id, firstname, lastname, address, salary;
id = "";
firstname = "";
lastname = "";
address = "";
salary = "";
helper = new MyDBHelper(getBaseContext());
helper.openDB();
Cursor c = helper.getAllRecords();
try {
c = helper.getAllRecords();
if (c.moveToFirst()) {
do {
id = c.getString(c.getColumnIndex("_id"));
firstname = c.getString(c.getColumnIndex("firstName"));
lastname = c.getString(c.getColumnIndex("lastName"));
//add in to array list
list.add(id);
list.add(firstname);
list.add(lastname);
gridView.setAdapter(adapter);
} while (c.moveToNext());//Move the cursor to the next row.
} else {
Toast.makeText(getApplicationContext(), "No data found", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "No data found" + e.getMessage(), Toast.LENGTH_LONG).show();
}
helper.close();
}
}
我的DBhelper代码在这里执行CRUD操作
package com.example.sunny.sqlitedb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Sunny on 8/23/2016.
*/
public class MyDBHelper extends SQLiteOpenHelper {
private static final String DBNAME = "mydb.db";
private static final int VERSION = 1;
public static final String TABLE_NAME = "employees";
public static final String ID = "_id";
public static final String FIRST_NAME = "firstName";
public static final String LAST_NAME = "lastName";
public static final String ADDRESS = "address";
public static final String SALARY = "salary";
private SQLiteDatabase myDB;
public MyDBHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String queryTable = "CREATE TABLE " + TABLE_NAME +
" (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FIRST_NAME + " TEXT NOT NULL, " +
LAST_NAME + " TEXT NOT NULL, " +
ADDRESS + " TEXT NOT NULL, " +
SALARY + " REAL NOT NULL " +
")";
db.execSQL(queryTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDB(){
myDB = getWritableDatabase();
}
public void closeDB(){
if(myDB != null && myDB.isOpen()) {
myDB.close();
}
}
public long insert(int id, String fName, String lName, String address, Double salary){
ContentValues values = new ContentValues();
if(id != -1)
values.put(ID, id);
values.put(FIRST_NAME, fName);
values.put(LAST_NAME, lName);
values.put(ADDRESS, address);
values.put(SALARY, salary);
return myDB.insert(TABLE_NAME, null, values);
}
public long update(int id, String fName, String lName, String address, Double salary){
ContentValues values = new ContentValues();
values.put(FIRST_NAME, fName);
values.put(LAST_NAME, lName);
values.put(ADDRESS, address);
values.put(SALARY, salary);
String where = ID + " = " + id;
return myDB.update(TABLE_NAME, values, where, null);
}
public long delete(int id){
String where = ID + " = " + id;
return myDB.delete(TABLE_NAME, where, null);
}
public Cursor getAllRecords(){
// myDB.query(TABLE_NAME, null, null, null, null, null, null);
String query = "SELECT * FROM " + TABLE_NAME;
return myDB.rawQuery(query, null);
}
public Cursor getDataBasedOnQuery(String query){
return myDB.rawQuery(query, null);
}
}
MY design Activity.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=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etID"
android:hint="Enter ID or Leave it Empty"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etFirstName"
android:hint="First Name"
android:layout_below="@+id/etID"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etLastName"
android:hint="Last Name"
android:layout_below="@+id/etFirstName"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etAddress"
android:hint="Address"
android:layout_below="@+id/etLastName"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Salary"
android:ems="10"
android:id="@+id/etSalary"
android:layout_below="@+id/etAddress"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="@+id/buttonsLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etSalary"
android:layout_centerHorizontal="true">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Insert"
android:id="@+id/btnInsert"
android:layout_below="@+id/etSalary"
android:layout_centerHorizontal="true"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Update"
android:id="@+id/btnUpdate"
android:layout_below="@+id/btnInsert"
android:layout_centerHorizontal="true"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/btnDelete"
android:layout_below="@+id/btnUpdate"
android:layout_centerHorizontal="true"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Load All"
android:id="@+id/btnLoadAll"
android:layout_below="@+id/btnDelete"
android:layout_centerHorizontal="true"
android:layout_weight="1" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/buttonsLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/idview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="ID"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/fnameview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="FirstName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lnameview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="LastName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/idview"
android:fastScrollAlwaysVisible="true"
android:fastScrollEnabled="true"
android:numColumns="3"
android:textFilterEnabled="false"
android:visibility="visible"
android:background="#d84521"
android:textColor="#fdfcfa">
</GridView>
</RelativeLayout>
</RelativeLayout>