android - If-else 语句根据先前的 Activity 选择获取共享首选项

标签 android android-intent sharedpreferences

我有一个问题,我尝试了很多在线解决方案,但没有发现任何改进。

在 Activity A 中,我有 10 行用户帐户,每行包含不同的数据集,可以在下一个 Activity B 中设置。(这个概念就像联系人一样,其中用户联系人与其他联系人具有相同的布局,但不同用户电子邮件、电话号码等)。如果我选择第 1 行,它将被定向到具有相应数据的 Activity C。如果是第2行,第2行的数据会传给activity C。

我可以使用 sharedpreferences 检索我的数据,但是一旦我根据选择进行检索,例如if-else 选择行,那么 Activity C 中不会显示任何内容。我的代码有什么问题? 各位高手能指出我的问题吗?

A 中的 fragment 代码:

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 中的 fragment 代码:

// 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”,那么无论我单击哪一行,都会选择第 2 行数据。所以为了克服这个问题,我使用了下面的代码,但是没有任何东西被传递给 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();
    }
}

最佳答案

可能对你有帮助


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>

关于android - If-else 语句根据先前的 Activity 选择获取共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16767779/

相关文章:

java - 将图像 url 存储在共享首选项中并在 Recyclerview 中显示它们

android - android(xml)中的普通搜索栏和离散搜索栏有什么区别

android - 如何在 Android Cordova v4.0.2 中使用 window.location = "http://{remote_url}"?

java.lang.NullPointerException : error - app is not opening 异常

android - 如何让我的 Activity 的单个实例与后台堆栈一起工作?

android - SharedPreference 或 SQLite 数据库

java - 为共享首选项自动生成 getter setter

android - ProgressDialog.show() 不显示进度对话框

java - 从 JNI 调用 Java 对象中的方法返回错误

android - 服务未收到 Intent/额外信息