gpt4 book ai didi

android - boolean 值从一开始就设置为 true

转载 作者:行者123 更新时间:2023-11-30 03:21:23 27 4
gpt4 key购买 nike

嘿,我一直在努力使这项工作正常进行,但我无法弄清楚问题出在哪里。

我希望在启动应用程序时 stopValue 为 false,然后它将由我在应用程序中的操作设置。我发现 boolean 值从一开始就是假的,因此我的开始 Intent 设置为假。

这不是我唯一的问题,当我输入开始公里数和停止公里数时,它只给我停止公里数。结果应该是开始-停止公里数。

不知道这些问题有没有联系。也许我没有正确理解 sharedpreferences 部分,或者可能是 putextra 或 getIntent 部分。

感谢您的宝贵时间。

我的主课

public class Main extends Activity{

Button bStart, bStop;
TextView tvView;
Spinner spinner1;
boolean stopValue;
int startkilometer;
String date;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

bStart = (Button) findViewById(R.id.bStart);
tvView = (TextView) findViewById(R.id.tvView);
spinner1 = (Spinner) findViewById(R.id.spinner1);

date = getIntent().getStringExtra("datoen");
startkilometer = getIntent().getIntExtra("startkm", 0);


SharedPreferences sp = getApplicationContext().getSharedPreferences("stopValue", 0);
stopValue = sp.getBoolean("stopper", stopValue);

SharedPreferences sp1 = getSharedPreferences("startkilometer", Activity.MODE_PRIVATE);
startkilometer = sp1.getInt("startkm", startkilometer);

SharedPreferences sp2 = getSharedPreferences("date", Activity.MODE_PRIVATE);
date = sp2.getString("datoen", date);



stopValue = getIntent().getBooleanExtra("stopper", stopValue);


if(stopValue){
bStart.setText("Start");
bStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent start = new Intent("com.uniqueapps.runner.START");
startActivity(start);
}
});
}
if(stopValue == false){
bStart.setText("Stop");
bStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent stop = new Intent("com.uniqueapps.runner.STOP");
stop.putExtra("startkm", startkilometer);
stop.putExtra("datoen", date);
startActivity(stop);
}
});
}

KilometerSQL info = new KilometerSQL(this);
info.open();
String data = info.getData();
info.close();
tvView.setText(data);

}




@Override
protected void onResume() {
// TODO Auto-generated method stub

super.onResume();



}

@Override
protected void onPause() {
// TODO Auto-generated method stub

SharedPreferences sp = getApplicationContext().getSharedPreferences("stopValue", 0);
Editor edit = sp.edit();
edit.putBoolean("stopper", stopValue);
edit.commit();

SharedPreferences sp1 = getApplicationContext().getSharedPreferences("startkilometer", 0);
Editor edit1 = sp1.edit();
edit1.putInt("startkm", startkilometer);
edit1.commit();

SharedPreferences sp11 = getApplicationContext().getSharedPreferences("date", 0);
Editor edit11 = sp11.edit();
edit11.putString("datoen", date);
edit11.commit();



super.onPause();
}

我的课开始

public class Start extends Main implements OnClickListener {

Button bStartTur;
EditText etDate, etKm;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
bStartTur = (Button) findViewById(R.id.bStartTur);
bStartTur.setOnClickListener(this);
etDate = (EditText) findViewById(R.id.etdate);
etKm = (EditText) findViewById(R.id.etKm);

Calendar cal = Calendar.getInstance();

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
etDate.setText(format.format(new Date()));



}



@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String date = etDate.getText().toString();
int startkilometer;
switch (v.getId()) {

case R.id.bStartTur:

startkilometer = Integer.valueOf(etKm.getText().toString());


Intent menu = new Intent("com.uniqueapps.runner.MENU");
menu.putExtra("stopper", false);
menu.putExtra("startkm", startkilometer);
menu.putExtra("datoen", date);
startActivity(menu);

break;
}

}

}

我的停课

public class Stop extends Main implements OnClickListener {

Button bStop;
EditText sqllocations, kilometer;
int startkilometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.stop);
bStop = (Button) findViewById(R.id.bstopTur);
bStop.setOnClickListener(this);
sqllocations = (EditText) findViewById(R.id.locations);
kilometer = (EditText) findViewById(R.id.kilometer);

sqllocations.setText("Unknown");


}

@Override
public void onClick(View v) {

int startkilometer;
int slutkilometer;
startkilometer = getIntent().getIntExtra("startkm", 0);
date = getIntent().getStringExtra("datoen");

// TODO Auto-generated method stub

switch(v.getId()){

case R.id.bstopTur:


slutkilometer = Integer.valueOf(kilometer.getText().toString());



int kortekm = (slutkilometer - startkilometer);

try{
String locations = sqllocations.getText().toString();

KilometerSQL entry = new KilometerSQL(this);
entry.open();
entry.createEntry(date, kortekm, locations);
entry.close();
}catch(Exception e){
e.printStackTrace();
}

Intent menu = new Intent("com.uniqueapps.runner.MENU");
menu.putExtra("stopper", true);
startActivity(menu);
break;
}
}

}

我的 SQL 类

public class KilometerSQL {

public static final String KEY_ROWID = "date";
public static final String KEY_KILOMETER = "kilometer";
public static final String KEY_LOCATIONS = "locations";

private static final String DATABASE_NAME = "Kilometerdb";
private static final String DATABASE_TABLE = "kilometertable";
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 + " TEXT NOT NULL, " +
KEY_KILOMETER + " INTEGER, " +
KEY_LOCATIONS + " TEXT NOT NULL);"


);
}

@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 KilometerSQL (Context c){
ourContext = c;
}

public KilometerSQL open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}

public void close(){
ourHelper.close();
}

public long createEntry(String date, int kortekm, String locations) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_ROWID, date);
cv.put(KEY_KILOMETER, kortekm);
cv.put(KEY_LOCATIONS, locations);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
// TODO Auto-generated method stub
String [] columns = new String []{ KEY_ROWID, KEY_KILOMETER, KEY_LOCATIONS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";

int iDate = c.getColumnIndex(KEY_ROWID);
int iKilometer = c.getColumnIndex(KEY_KILOMETER);
int iLocations = c.getColumnIndex(KEY_LOCATIONS);

for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result = result + c.getString(iDate) + " " + c.getString(iKilometer) + " " + c.getString(iLocations) + "\n";
}

return result;
}


}

最佳答案

您阅读 SharedPreferences 的方式如下。

boolean silent = perefsInstance.getBoolean("silentMode", false);//这里的第二个参数 (false) 是您可以随意设置的默认值。

阅读 SharedPreferences 以及如何为未初始化的键设置默认值 here

关于android - boolean 值从一开始就设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19079430/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com