在我的应用程序中,我在一个活动中使用多个微调器,一切正常,但是当我根据当前活动中选定的微调器项目启动另一个活动时,所有微调选择项目都会更改为其“0”索引价值本身。当我从下一个活动返回到具有微调器的活动时(即当活动恢复时),其选择的值被设置为“0”索引。如何在启动另一个活动时停止改变其所选项目的微调器的这种行为。
请安卓大师尽早回复!我有点着急。
onSavedInstanceStateMethod用于保存微调器值的索引
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("InterestSpinner", interest.getSelectedItemPosition());
outState.putInt("CompanySpinner", company.getSelectedItemPosition());
outState.putInt("PolicySpinner", policy.getSelectedItemPosition());
outState.putInt("DeptSpinner", dept.getSelectedItemPosition());
}
以下是我在意图
中添加一些字符串后调用另一个活动的方法Intent intent = new Intent(PolicySelection.this, TakePicture.class);
intent.putExtra("INTERESTID", interestId);
intent.putExtra("COMPANYID", companyId);
intent.putExtra("TRANSID", transID);
intent.putExtra("UserName", username);
intent.putExtra("Password", password);
intent.putExtra("ClaimID", "");
intent.putExtra("OP", "UNDERWRITING");
startActivity(intent);
这是我的一个微调器onItemChangeListener()方法`
company.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
popDeptSpinner();
popPolicySpinner();
popInterestSpinner();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});`
这是我的onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.policy_selection);
policy = (Spinner) findViewById(R.id.policySelectionSpinner);
company = (Spinner) findViewById(R.id.companySelectSpinner);
interest = (Spinner) findViewById(R.id.interestIdSpinner);
department = (Spinner) findViewById(R.id.deptSpinner);
camera = (Button) findViewById(R.id.camera);
damages = (Button) findViewById(R.id.damages);
upload = (Button) findViewById(R.id.upload);
showDetail = (Button) findViewById(R.id.showDetail);
title = (TextView) findViewById(R.id.title);
popCompanySpinner();
popDeptSpinner();
popPolicySpinner();
popInterestSpinner();
showDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean ready = isNetworkAvailable();
if (ready) {
if (policy.getAdapter().getCount() > 0) {
deptID = department.getSelectedItem().toString();
if (deptID.equals("MOTOR") || deptID.equals("MISCELLEANEOUS")) {
interestId = interest.getSelectedItem().toString();
transID = transId_list.get(getPositions(policy.getSelectedItem().toString()));
branchId = branchId_list.get(getPositions(policy.getSelectedItem().toString()));
deptNo = deptNoList.get(getPositions(policy.getSelectedItem().toString()));
Intent intent = new Intent(PolicySelection.this, InterestDetail.class);
intent.putExtra("UserName", username);
intent.putExtra("Password", password);
intent.putExtra("InterestID", interestId);
intent.putExtra("DeptID", deptNo);
intent.putExtra("TransID", transID);
startActivity(intent);
} else {
Toast.makeText(PolicySelection.this, "No Details for Current Policy!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show();
}
}
});
damages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean ready = isNetworkAvailable();
if (ready) {
if (policy.getAdapter().getCount() > 0) {
if (department.getSelectedItem().toString().equals("MOTOR")) {
Integer pos;
pos = getPositions(policy.getSelectedItem().toString());
switch (company.getSelectedItem().toString()) {
case "UIC":
transID = transId_list.get(pos);
companyId = "UIC";
surveyTransID = UICSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
break;
case "UICTAK":
transID = transId_list.get(pos);
companyId = "UICTAK";
surveyTransID = UICTAKSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
break;
case "SPI":
transID = transId_list.get(pos);
companyId = "SPI";
surveyTransID = SPISurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
break;
case "SPITAK":
transID = transId_list.get(pos);
companyId = "SPITAK";
surveyTransID = SPITAKSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
break;
default:
break;
}
Intent intent = new Intent(PolicySelection.this, DamagesDetail.class);
if (Objects.equals(transID, "")) {
company.getSelectedItem().toString();
} else {
String options = new WebServiceCall().getPreInspectionOptions("GetPreInspectionOptions", username, password, companyId, transID);
intent.putExtra("TRANSID", transID);
intent.putExtra("COMPANYID", companyId);
intent.putExtra("UserName", username);
intent.putExtra("Password", password);
intent.putExtra("Options", options);
intent.putExtra("SurveyTransID", surveyTransID);
}
startActivity(intent);
} else {
Toast.makeText(PolicySelection.this, "No Damages Form for Current Policy!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show();
}
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean ready = isNetworkAvailable();
if (ready) {
if (policy.getAdapter().getCount() > 0) {
Integer pos;
pos = getPositions(policy.getSelectedItem().toString());
switch (company.getSelectedItem().toString()) {
case "UIC":
transID = transId_list.get(pos);
branchId = branchId_list.get(pos);
break;
case "UICTAK":
transID = transId_list.get(pos);
branchId = branchId_list.get(pos);
break;
case "SPI":
transID = transId_list.get(pos);
branchId = branchId_list.get(pos);
break;
case "SPITAK":
transID = transId_list.get(pos);
branchId = branchId_list.get(pos);
break;
default:
break;
}
Intent intent = new Intent(PolicySelection.this, SendInfoForm.class);
intent.putExtra("USERNAME", username);
intent.putExtra("PASSWORD", password);
intent.putExtra("COMPANYID", company.getSelectedItem().toString());
intent.putExtra("LAT", "1");
intent.putExtra("LONG", "1");
intent.putExtra("DEPTID", department.getSelectedItem().toString());
intent.putExtra("INTERESTID", interest.getSelectedItem().toString());
intent.putExtra("BRANCHID", branchId);
intent.putExtra("TRANSID", transID);
intent.putExtra("OP", "UNDERWRITING");
intent.putExtra("CLAIMID", "");
startActivity(intent);
} else {
Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show();
}
}
});
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (policy.getAdapter().getCount() > 0) {
Integer pos;
pos = getPositions(policy.getSelectedItem().toString());
if (policyList != null) {
if (Objects.equals(SelectedPolicy, " ")) {
SelectedPolicy = company.getSelectedItem().toString();
}
}
switch (company.getSelectedItem().toString()) {
case "UIC":
interestId = interest.getSelectedItem().toString();
companyId = "UIC";
transID = transId_list.get(pos);
break;
case "UICTAK":
interestId = interest.getSelectedItem().toString();
companyId = "UICTAK";
transID = transId_list.get(pos);
break;
case "SPI":
interestId = interest.getSelectedItem().toString();
companyId = "SPI";
transID = transId_list.get(pos);
break;
case "SPITAK":
interestId = interest.getSelectedItem().toString();
companyId = "SPITAK";
transID = transId_list.get(pos);
break;
default:
break;
}
Intent intent = new Intent(PolicySelection.this, TakePicture.class);
intent.putExtra("INTERESTID", interestId);
intent.putExtra("COMPANYID", companyId);
intent.putExtra("TRANSID", transID);
intent.putExtra("UserName", username);
intent.putExtra("Password", password);
intent.putExtra("ClaimID", "");
intent.putExtra("OP", "UNDERWRITING");
startActivity(intent);
} else {
Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
}
}
});
company.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
popDeptSpinner();
popPolicySpinner();
popInterestSpinner();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
policy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
if (policyList != null) {
SelectedPolicy = policy.getSelectedItem().toString();
policy.setSelection(position);
popInterestSpinner();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
interest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (interestList != null) {
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
department.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
popPolicySpinner();
popInterestSpinner();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
答案 0 :(得分:0)
如何使用
SaveInstanceState
@Override
public void onSaveInstanceState(Bundle outState) {//store your instance here
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {//load your instance here
int position= savedInstanceState.getInt("InterestSpinner",0);
spinner.setSelection(interestSpiner);
}
}
或
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int position= savedInstanceState.getInt("InterestSpinner",0);
spinner.setSelection(interestSpiner);
}
答案 1 :(得分:0)
这为时已晚。但这可能会帮助其他人。
活动恢复后,调用company.setOnItemSelectedListener方法并触发pop方法,
popDeptSpinner();
popPolicySpinner();
popInterestSpinner();
因此所有微调器都重置为第0个索引。因此,我将微调器选择的值保存在成员变量中,然后使用微调器的setSelection方法再次将其设置为上述弹出方法中的微调器。
if(!dept.isEmpty()){
Cursor cc = (Cursor) (department .getSelectedItem());
for (int i = 0; i < cc.getCount(); i++) {
cc.moveToPosition(i);
String temp = cc.getString((cc.getColumnIndex("_id")));
if (temp.contentEquals("selected value")) {
department .setSelection(i,false);
}
}
}