If-else语句基于先前的活动选择来获取共享偏好

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了If-else语句基于先前的活动选择来获取共享偏好相关的知识,希望对你有一定的参考价值。

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

从活动A开始,我有10行用户帐户,每个帐户包含不同的数据集,可以在下一个活动B中设置。(概念就像联系人,用户联系人的布局与其他人相同,但用户电子邮件不同,电话号码等)。如果我选择第1行,它将被引导到具有相应数据的活动C.如果第2行,第2行数据将传递给活动C.

我可以使用共享偏好检索我的数据,但是一旦我根据选择进行检索,例如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();
    }
}
答案

It may help yours


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>

以上是关于If-else语句基于先前的活动选择来获取共享偏好的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio - 共享偏好无法保存在新活动中

使用共享偏好更改背景

if-else 或提前返回 [关闭]

如何使用基于先前Form的继承参数的SQL查询填充WinForm字段?

在同一选择语句中根据先前计算的行(或列)计算新行(或列)

如果未选择图像,如何将默认图像传递给先前的活动