这是我使用eclipse构建的Android应用。 我有一个小问题是,brojac的值不会变为5,它保持初始化状态。 我不知道问题是什么,甚至不知道该尝试什么。不管怎样,谢谢!
这是sql数据库代码:
package com.peky.smartornot;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Sql {
public static final String KEY_ROWID= "_id";
public static final String KEY_JOKERS= "joker";
public static final String KEY_JOKERAMMOUNT= "ammount";
private static final String DATABASE_NAME= "SQL";
private static final String DATABASE_TABLE= "peoples_table";
private static final int DATABASE_VERSION= 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_JOKERS + " INTEGER, " +
KEY_JOKERAMMOUNT + " INTEGER);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Sql(Context c){
ourContext=c;
}
public Sql open(){
ourHelper = new DbHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long spremi(int brojjokera, int kolicina) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_JOKERS, brojjokera);
cv.put(KEY_JOKERAMMOUNT, kolicina);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public int procitaj() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_JOKERS,
KEY_JOKERAMMOUNT };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
int citac = 0;
int iJoker = c.getColumnIndex(KEY_JOKERS);
int iJokerammount = c.getColumnIndex(KEY_JOKERAMMOUNT);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
if (c.getInt(iJoker) == 1)
citac = c.getInt(iJokerammount);
}
return citac;
}
}
以下是功能:
package com.peky.smartornot;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Redeemcode extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redeemcode);
}
public void provjeri(View view){
EditText josip=(EditText)findViewById(R.id.test);
TextView proba=(TextView)findViewById(R.id.povrat);
String provjera=josip.toString();
int kolicina=5,brojac=0;
int brojjokera=1;
Sql ulaz= new Sql(this);
if (provjera=="josip"){
ulaz.open();
ulaz.spremi(brojjokera, kolicina);
brojac= ulaz.procitaj();
ulaz.close();
}
proba.setText(""+brojac);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.redeemcode, menu);
return false;
}
}
答案 0 :(得分:2)
brojac= ulaz.procitaj()
未执行,因为if
条件可能会失败,因为您正在使用==
来拼写字符串
您需要使用.equals
if (provjera.equals("josip")){
ulaz.open();
ulaz.spremi(brojjokera, kolicina);
brojac= ulaz.procitaj();
proba.setText("Updated value is "+brojac);
ulaz.close();
}else
{
proba.setText("Old value is "+brojac);
}