我这里有一个问题,我尝试了很多在线解决方案并没有发现任何改进。
在活动 A 中,我有 10 行用户帐户,每行包含不同的数据集,可以在下一个活动 B 中设置。(这个概念就像联系人一样,用户联系人的布局与其他用户相同,但用户电子邮件不同,电话号码等)。如果我选择第 1 行,它将被定向到带有相应数据的活动 C。如果是第 2 行,第 2 行的数据将被传递给活动 C。
我可以使用 sharedpreferences 检索我的数据,但是一旦我根据选择进行检索,例如 if-else 选择行,那么活动 C 中将不会显示任何内容。我的代码有什么问题?这里的专家能指出我的问题吗?
A中的代码段:
btnDone = (Button)findViewById(R.id.btnDone);
btnDone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent Intent = new Intent(ActivityA.this, ActivityC.class);
if (tableRow1.isFocused()){
Intent.putExtra("activity", "activity1");
startActivity(Intent);
finish();}
else if (tableRow2.isFocused()){
Intent.putExtra("activity", "activity2");
startActivity(Intent);
finish();}
else if (tableRow3.isFocused()){
Intent.putExtra("activity", "activity3");
startActivity(Intent);
finish();}
else if (tableRow4.isFocused()){
Intent.putExtra("activity", "activity4");
startActivity(Intent);
finish();}
else if (tableRow5.isFocused()){
Intent.putExtra("activity", "activity5");
startActivity(Intent);
finish();}
else if (tableRow6.isFocused()){
Intent.putExtra("activity", "activity6");
startActivity(Intent);
finish();}
else if (tableRow7.isFocused()){
Intent.putExtra("activity", "activity7");
startActivity(Intent);
finish();}
else if (tableRow8.isFocused()){
Intent.putExtra("activity", "activity8");
startActivity(Intent);
finish();}
else if (tableRow9.isFocused()){
Intent.putExtra("activity", "activity9");
startActivity(Intent);
finish();}
else if (tableRow10.isFocused()){
Intent.putExtra("activity", "activity10");
startActivity(Intent);
finish();}
C中的代码段:
// Assigns value
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
//String passedVar = getIntent().getStringExtra("activity");
etAccountName.setText(sp.getString("accountName2", ""));
etWanIp.setText(sp.getString("wanIp2", ""));
etLocalIp.setText(sp.getString("localIp2", ""));
etPort.setText(sp.getString("port2", ""));
etPassword.setText(sp.getString("password2", ""));
在 C 中的代码中,如果我放置“2”,那么无论我点击哪一行,都会选择 row2 数据。所以为了克服这个问题,我使用了下面的代码,但是没有任何东西被传递给 C:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String passedVar = getIntent().getStringExtra("activity");
//I tried before passedVar.equals("activity1")
if("activity1".equals(passedVar)){
etAccountName.setText(sp.getString("accountName1", ""));
etWanIp.setText(sp.getString("wanIp1", ""));
etLocalIp.setText(sp.getString("localIp1", ""));
etPort.setText(sp.getString("port1", ""));
etPassword.setText(sp.getString("password1", ""));}
else if("activity2".equals(passedVar)){
etAccountName.setText(sp.getString("accountName2", ""));
etWanIp.setText(sp.getString("wanIp2", ""));
etLocalIp.setText(sp.getString("localIp2", ""));
etPort.setText(sp.getString("port2", ""));
etPassword.setText(sp.getString("password2", ""));}
else if("activity3".equals(passedVar)){
etAccountName.setText(sp.getString("accountName3", ""));
etWanIp.setText(sp.getString("wanIp3", ""));
etLocalIp.setText(sp.getString("localIp3", ""));
etPort.setText(sp.getString("port3", ""));
etPassword.setText(sp.getString("password3", ""));}
关于为什么使用 isFocused() 的部分代码(因为这个应用程序的所有者请求它):
tableRow1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tableRow1.setFocusableInTouchMode(true);
tableRow1.requestFocus();}});
B中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_tab_content_setting);
this.initViews();
}
private void initViews(){
etAccountName = (EditText)this.findViewById(R.id.etAccountName);
etWanIp = (EditText)this.findViewById(R.id.etWanIp);
etLocalIp = (EditText)this.findViewById(R.id.etLocalIp);
etPort = (EditText)this.findViewById(R.id.etPort);
etPassword = (EditText)this.findViewById(R.id.etPassword);
// Assigns value
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
etAccountName.setText(sp.getString("accountName1", ""));
etWanIp.setText(sp.getString("wanIp1", ""));
etLocalIp.setText(sp.getString("localIp1", ""));
etPort.setText(sp.getString("port1", ""));
etPassword.setText(sp.getString("password1", ""));
etWanIp.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View arg0, boolean hasFocus) {
if(!hasFocus){
System.out.println("lost focus");
AccountSettingActivity1.this.saveSettings();}}});
}
private void saveSettings(){
String accountName1 = etAccountName.getText().toString();
String wanIp1 = etWanIp.getText().toString();
String localIp1 = etLocalIp.getText().toString();
String port1 = etPort.getText().toString();
String password1 = etPassword.getText().toString();
accountName1 = (accountName1.trim().length() == 0)? "User": accountName1;
wanIp1 = (wanIp1.trim().length() == 0)? "0.0.0.0": wanIp1;
localIp1 = (localIp1.trim().length() == 0)? "0.0.0.0": localIp1;
port1 = (port1.trim().length() == 0)? "8000": port1;
password1 = (password1.trim().length() == 0)? "xxxx": password1;
etAccountName.setText(accountName1);
etWanIp.setText(wanIp1);
etLocalIp.setText(localIp1);
etPort.setText(port1);
etPassword.setText(password1);
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
editor.putString("accountName1", etAccountName.getText().toString());
editor.putString("wanIp1", etWanIp.getText().toString());
editor.putString("localIp1", etLocalIp.getText().toString());
editor.putString("port1", etPort.getText().toString());
editor.putString("password1", etPassword.getText().toString());
editor.commit();
}
public void onBackPressed() {
saveSettings();
super.onBackPressed();
etAccountName = (EditText)findViewById(R.id.etAccountName);
etWanIp = (EditText)findViewById(R.id.etWanIp);
etLocalIp = (EditText)findViewById(R.id.etLocalIp);
etPort = (EditText)findViewById(R.id.etPort);
etPassword = (EditText)findViewById(R.id.etPassword);
String accountName1 = etAccountName.getText().toString();
String wanIp1 = etWanIp.getText().toString();
String localIp1 = etLocalIp.getText().toString();
String port1 = etPort.getText().toString();
String password1 = etPassword.getText().toString();
Intent i = new Intent(B.this, A.class);
i.putExtra("accountName1" ,accountName1);
i.putExtra("wanIp1" ,wanIp1);
i.putExtra("localIp1" ,localIp1);
i.putExtra("port1" ,port1);
i.putExtra("password1" ,password1);
setResult(RESULT_OK, i);
startActivity(i);
finish();
}
@Override
protected void onPause() {
// When user leaves this tab, saves the values
this.saveSettings();
super.onPause();
}
}