我制作了一个数据库应用。我有一个按钮和一个int值;我需要改变int值。
示例= sayi = 1
按钮1。 click = sayi = 2 按钮2。 click = sayi = 3
P.s =对不起,我不太懂英语。
int sayi = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
final TextView oku = (TextView)findViewById(id.oku);
final Button diger = (Button)findViewById(id.diger);
diger.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int sayi = 2;
}
});
Veritabani veritabanim;
veritabanim = new Veritabani(this);
try {
veritabanim.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
SQLiteDatabase dbm = veritabanim.getReadableDatabase();
Cursor c = dbm.rawQuery("SELECT _id,soyleyen,soz FROM sozler WHERE _id = "+sayi+" ", null);
if(c.moveToFirst()){
do{
//assing values
String column1 = c.getString(0);
String column2 = c.getString(1);
String column3 = c.getString(2);
//Do something Here with values
oku.setText(column3);
}while(c.moveToNext());
}
c.close();
dbm.close();
}
}
答案 0 :(得分:0)
这样做......
int sayi = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
Button diger = (Button)findViewById(id.diger);
diger.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sayi++;
getDBOperation();
}
});
}
private void getDBOperation()
{
TextView oku = (TextView)findViewById(id.oku);
Veritabani veritabanim;
veritabanim = new Veritabani(this);
try {
veritabanim.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
SQLiteDatabase dbm = veritabanim.getReadableDatabase();
Cursor c = dbm.rawQuery("SELECT _id,soyleyen,soz FROM sozler WHERE _id = "+sayi+" ", null);
if(c.moveToFirst()){
do{
//assing values
String column1 = c.getString(0);
String column2 = c.getString(1);
String column3 = c.getString(2);
//Do something Here with values
oku.setText(column3);
}while(c.moveToNext());
}
c.close();
dbm.close();
}