1

都市が配列されたスピナーがあります。各都市には独自の URL があります。スピナーから値を渡して、それぞれの URL を Web ビューで実行するにはどうすればよいですか? 以下は私のコードです:

MainActivity.java:

public class MainActivity extends Activity {

private Spinner spinner1;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addListenerOnButton();
    addListenerOnSpinnerItemSelection();

}

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

// Get selected drop down list value
public void addListenerOnButton() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);  

    final Context context = this;
    Button bcitysearch = (Button) findViewById(R.id.citysearch);
    bcitysearch.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Toast.makeText(MainActivity.this,
                    "OnClickListener : " +
                    "\nSpinner 1 :            "+String.valueOf(spinner1.getSelectedItem()),
                        Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(context, CitySearch.class);
            startActivity(intent);  
        }   
        });     
} 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

CustomOnItemSelectedListener.java:

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(parent.getContext(), 
        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
        Toast.LENGTH_SHORT).show();
  }
public void onNothingSelected(AdapterView<?> arg0) {    
}
}

CitySearch.java:

@SuppressLint("SetJavaScriptEnabled")
public class CitySearch extends Activity{

private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    Uri uri = Uri.parse("http://something.com/firstcity/index.html");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//How would I parse a different url based on the selection from the spinner?
    startActivity(intent);
    //Initialize the WebView object with data from the 'webview.xml' file
    webview = (WebView) findViewById(R.id.citywebview);
    //Enable JavaScript
    webview.getSettings().setJavaScriptEnabled(true);
    //Set the user agent  
    webview.getSettings().setUserAgentString("AndroidWebView");
    //Clear the cache
    webview.clearCache(true);


    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient(){
        public void onProgressChanged(WebView view, int progress) {
            //Activities and WebViews measure progress with different scales
            //The progress meter will automatically disappear when we reach 100%
            activity.setProgress(progress * 1000);
        }
    });

    webview.setWebViewClient(new WebViewClient(){
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
        }
    });

    webview.loadUrl("http://something.com/firstcity/index.html");
    //How do I load another city from the array that has a different URL based on spinner selection?

}
}
4

0 に答える 0