下面是我在数据库中插入一个整数值的代码。此代码旨在帮助用户跟踪他/她的帐户余额。在此代码中,数据库的内容在按钮单击时显示为Toast消息。我想要在调用此活动时在文本字段中显示内容。显示内容的文本字段和更新按钮属于同一活动。因此,当用户输入要从数据库中添加或减去的值时,按下更新按钮时,应使用更新的值刷新文本字段。 plz在代码中进行了足够的更改。
谢谢
主要Activity.java
package com.sqltut;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button save,load,deposit,withdraw;
EditText balance,updatevalue;
DataHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=(Button) findViewById(R.id.save);
load=(Button) findViewById(R.id.load);
deposit=(Button) findViewById(R.id.deposit);
withdraw=(Button) findViewById(R.id.withdraw);
balance=(EditText) findViewById(R.id.balance);
updatevalue=(EditText) findViewById(R.id.updatevalue);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getbalance=balance.getText().toString();
int bal=Integer.parseInt(getbalance);
handler=new DataHandler(getBaseContext());
handler.open();
long id=handler.insertData(bal);
Toast.makeText(getBaseContext(), "Data Inserted", Toast.LENGTH_LONG).show();
handler.close();
}
});
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int getbalance;
getbalance=0;
handler=new DataHandler(getBaseContext());
handler.open();
Cursor C=handler.returnData();
if(C.moveToFirst())
{
do
{
getbalance=C.getInt(0);
}while(C.moveToNext());
}
handler.close();
Toast.makeText(getBaseContext(), "Balance:"+getbalance,Toast.LENGTH_LONG).show();
}
});
withdraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String updt=updatevalue.getText().toString();
int value=Integer.parseInt(updt);
int flag=0;
handler=new DataHandler(getBaseContext());
handler.open();
handler.updateData(value,flag);
Toast.makeText(getBaseContext(), "Data Updated", Toast.LENGTH_LONG).show();
handler.close();
}
});
deposit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String updt=updatevalue.getText().toString();
int value=Integer.parseInt(updt);
int flag=1;
handler=new DataHandler(getBaseContext());
handler.open();
handler.updateData(value,flag);
Toast.makeText(getBaseContext(), "Data Updated", Toast.LENGTH_LONG).show();
handler.close();
}
});
//to call next activity
Button createAppointment = (Button)findViewById(R.id.Next);
createAppointment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, Page2.class);
MainActivity.this.startActivity(myIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
DataHandler.java
package com.sqltut;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataHandler {
public static final String BALANCE="balance";
public static final String TABLE_NAME="mytable";
public static final String DATA_BASE_NAME="mydatabase";
public static final int DATABASE_VERSION=1;
public static final String TABLE_CREATE="create table mytable(balance integer not null);";
DataBaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx)
{
this.ctx = ctx;
dbhelper=new DataBaseHelper(ctx);
}
private static class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context ctx)
{
super(ctx,DATA_BASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS mytable ");
onCreate(db);
}
}
public DataHandler open()
{
db=dbhelper.getWritableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
public long insertData(Integer balance)
{
ContentValues content=new ContentValues();
content.put(BALANCE, balance);
return db.insert(TABLE_NAME, null, content);
}
public void updateData(Integer value,Integer flag)
{
db = dbhelper.getReadableDatabase();
String selectQuery = "select balance from " + TABLE_NAME ;
Cursor cursor = db.rawQuery(selectQuery, null);
int current = 0;
if (cursor.moveToFirst()) {
current= Integer.parseInt(cursor.getString(0));
}
if(flag==0){
current = current-value;
}else {
current = current+value ;
}
cursor.close();
db.close();
try{
db = dbhelper.getWritableDatabase();
String rawQuery = "update mytable set balance="+current;
db.execSQL(rawQuery);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public Cursor returnData()
{
return db.query(TABLE_NAME, new String[] {BALANCE}, null, null, null, null, null);
}
}
答案 0 :(得分:0)
在这样的数据库中编写一个方法。
public int getValue(){
//get present value from your crud operations
//you have to relate with CRUD values and get the int value
//call the method to get you required value, ex; If delete operation is done then you have give updated value to this method or insert is done then you have to call this method and you have to send the updated value to this, similarly you have to do this for update operation also. then you will get the value into this method whether any operation has done on database.
}
step1:从你的按钮点击调用方法。 step2:在getValue()内部调用每个crud方法来获取更新的值。 试试这种方式。 在按钮的onClick中调用该方法。 希望这可以帮到你