在我的活动中,我有一个TextView和两个校准和wifi按钮。两个按钮都将进入扫描活动以获得一些结果。区别在于校准按钮会将结果保存到读数表,而wifi按钮会将结果保存到wifi表。但是两个按钮都只能将其插入到阅读表中。
这是活动课
calibrate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(db.getFriendlyWifis(building).isEmpty()) {
Context context = getApplicationContext();
CharSequence text = "Select one or more Friendly WiFi";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else {
Intent intent = new Intent(getApplicationContext(), Scan.class);
intent.putExtra("POSITION_NAME", positionName.getText().toString());
intent.putExtra("isLearning", isLearning);
intent.putExtra("NUMBER_OF_SECONDS", readingCount);
startActivityForResult(intent, SCAN_REQUEST);
}
}
});
wifi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(db.getFriendlyWifis(building).isEmpty()) {
Context context = getApplicationContext();
CharSequence text = "Select one or more Friendly WiFi";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else {
Intent intent = new Intent(getApplicationContext(), Scan.class);
intent.putExtra("POSITION_NAME", positionName.getText().toString());
intent.putExtra("isLearning", isLearning);
intent.putExtra("NUMBER_OF_SECONDS", readingCount);
startActivityForResult(intent, SCAN_REQUEST);
}
}
});
@Override
protected void onActivityResult(int requestCode, int result, Intent intent) {
if(requestCode == 0){
if(result == RESULT_OK){
positionData = (PositionData) intent
.getSerializableExtra("PositionData");
Log.v("Before db : ", positionData.toString());
db.addReadings(building, positionData);
positions = db.getPositions(building);
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, positions);
positionsList.setAdapter(arrayAdapter);
super.onActivityResult(requestCode, result, intent);
}
}
if(requestCode == 1){
if(result == RESULT_OK){
positionData = (PositionData) intent
.getSerializableExtra("PositionData");
Log.v("Before db : ", positionData.toString());
db.addWifi(building, positionData);
positiond = db.getWifi(building);
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, positiond);
positionsList.setAdapter(arrayAdapter);
super.onActivityResult(requestCode, result, intent);
}
}
}
答案 0 :(得分:2)
您在两个SCAN_REQUEST
上使用相同的请求代码startActivityForResult()
。
在calibrate.setOnClickListener()
中,像这样呼叫startActivityForResult
:startActivityForResult(intent, 0);
。
在wifi.setOnClickListener
中,像这样呼叫startActivityForResult
:startActivityForResult(intent, 1);
或者,将0和1分配给它们,而不是0和1。