我在这里遇到了一个问题,我尝试了很多在线解决方案,但没有找到任何改进。
从活动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();
}
}
答案 0 :(得分:2)
public class MainActivity extends Activity {
Button bt;
EditText tt;
String ss;
public static SharedPreferences pref;
static Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button1ddddddd);
tt = (EditText) findViewById(R.id.editText1ddddddddd);
pref = getSharedPreferences("share", 0);
editor = pref.edit();
if (pref.getString("name", null) != null) {
// SharedPreferences pref = getSharedPreferences("share", 0);
String aa = pref.getString("name", null);
tt.setText(aa);
// Intent i = new Intent(MainActivity.this, seco.class);
// startActivity(i);
}
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ss = tt.getText().toString();
// Storing email in pref
editor.putString("name", ss);
// commit changes
editor.commit();
Intent i = new Intent(MainActivity.this, seco.class);
startActivity(i);
}
});
}
}
public class seco extends Activity {
Button bttt, btt44, back;
TextView tqq;
String aa;
// Editor editor = MainActivity.editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ss);
SharedPreferences pref = getSharedPreferences("share", 0);
back =(Button)findViewById(R.id.button2back) ;
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(seco.this, MainActivity.class);
startActivity(i);
}
});
tqq = (TextView) findViewById(R.id.textView1);
bttt = (Button) findViewById(R.id.button1);
btt44 = (Button) findViewById(R.id.button2hhhhhhh);
aa = pref.getString("name", null);
bttt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tqq.setText(aa);
}
});
btt44.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences pref = getSharedPreferences("share", 0);
Editor editor = pref.edit();
editor.remove("name");
// editor.clear();
editor.commit();
Intent i = new Intent(seco.this, MainActivity.class);
startActivity(i);
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1ddddddd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="176dp"
android:text="clickTo" />
<EditText
android:id="@+id/editText1ddddddddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="38dp"
android:layout_marginTop="67dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="68dp"
android:layout_marginTop="110dp"
android:text="show" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2hhhhhhh"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2hhhhhhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button1"
android:layout_marginRight="56dp"
android:text="clearShare" />
<Button
android:id="@+id/button2back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="back" />
</RelativeLayout>