2

Apparently having some issues with my Key-Value the way it is currently set up. The goal is to read the value of a Spinner (set using a string array) and then based on it open up a specific local html file.

public class MainActivity extends Activity {

    private Button btnGo;

    String pageChoice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Spinner page = (Spinner)findViewById(R.id.txtPage);
        final SharedPreferences sharedPref =PreferenceManager.getDefaultSharedPreferences(this);
        btnGo = (Button)findViewById(R.id.btnGo);
        btnGo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                pageChoice = page.getSelectedItem().toString();

                //Hopefully will allow for "global" url setting.
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("key1", pageChoice);
                editor.commit();
                startActivity(new Intent(MainActivity.this, Web.class));
            }
        });
    }
}

The error is located in my Web.java.

public class Web extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        String strPage = sharedPref.getString(key1, "");

        if (strPage = "By The Numbers") {
            //WebView
            WebView wv = (WebView) findViewById(R.id.webview1);
            WebSettings webSettings = wv.getSettings();
            webSettings.setBuiltInZoomControls(true);
            wv.loadUrl("file:///android_asset/2.html");
        } else if (strPage = "Photos") {
            //WebView
            WebView wv = (WebView) findViewById(R.id.webview1);
            WebSettings webSettings = wv.getSettings();
            webSettings.setBuiltInZoomControls(true);
            wv.loadUrl("file:///android_asset/3.html");
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }
}

Also, is there an issue with my strPage being a String? How would I change it over to working with an If/Else instance?

Ziem
  • 6,579
  • 8
  • 53
  • 86
Joey Sides
  • 67
  • 2
  • 8

2 Answers2

2

You have to make double quotes around key1

 String strPage = sharedPref.getString("key1", "");

because it is not a variable. It is a string.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Awesome! Thank you! That fixed the first problem. How would I go about making it so that the value output is a variable? I'm honestly just getting started with Android development and I may have bit off more than I can chew with this one. xP – Joey Sides Apr 23 '15 at 05:32
  • @JoeySides Sorry i can't get you. Would you like to define the key1 as a variable string? to get something from the `sharedPref`? – Jens Apr 23 '15 at 05:35
  • Basically, what I'm trying to do is determine which item is selected from the String Array on my MainActivity and use that with nested "if/if else" statements to change which URL is used for the WebView. I guess in other words, I'm trying to determine the position of the Spinner. – Joey Sides Apr 23 '15 at 05:38
  • @JoeySides Take a look at [this](http://stackoverflow.com/questions/3913592/start-an-activity-with-a-parameter) thread. You can put the value in the Intent ang get it in the new started activity. – Jens Apr 23 '15 at 05:42
1
 String strPage = sharedPref.getString("key1", "");
M K
  • 356
  • 3
  • 8