English 中文(简体)
跳板到Click Listener,打开新页
原标题:hooking a spinner to onClick Listener, to open new pages
  • 时间:2012-01-15 04:12:12
  •  标签:
  • java
  • android

Still an Android newbie , but hopefully someone can point me in the right direction on this issue. What I am trying to achieve is. I have 2 radio button, when one is clicked it changes the array in the spinner to Canada, when I click the other array appears. This works fine! But I am struggling to hook up an onClickListener (from the spinner onItemSelect) to the submit button . From which I want to issue an intent to open a new page according to the province or state chosen. As well is some could show me a switch statement for that as well it would be greatly appreciated. Thanks in advance.

String xml

        <string name="prov_picker">Select a Province</string>
    <string-array name="prov_array">
        <item>-- Select Province --</item>
        <item>British Columbia</item>
        <item>Alberta</item>
        <item>Saskatchewan</item>
        <item>Manitoba</item>
        <item>Ontario</item>
        <item>Quebec</item>
        <item>New Brunswick</item>
        <item>Nova Scotia</item>
        <item>Newfoundland</item>
    </string-array>
     <string-array name="prov1_array">
        <item>-- Select State --</item>
        <item>Washington</item>
        <item>Florida</item>
        <item>California</item>
        <item>New York</item>
        <item>Colorado</item>
    </string-array>
    <string name="radio1">Canadian</string>    
    <string name="radio2">US</string> 

xml

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.view.View;

public class CanProvselect extends Activity implements OnCheckedChangeListener {
    /** Called when the activity is first created. */
    private RadioGroup RadioProvs;
    private Button btn1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.canprovselect);

        btn1 = (Button)findViewById(R.id.button1); 
        RadioProvs = (RadioGroup) findViewById(R.id.rgProvs);
        RadioProvs.setOnCheckedChangeListener(this);
    iii
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        ArrayAdapter<CharSequence> adapter = null;
        final Spinner spinner = (Spinner) findViewById(R.id.spinner);

        switch (checkedId) {
        case R.id.radio_1 :
            adapter = ArrayAdapter.createFromResource(
                    this, R.array.prov_array, android.R.layout.simple_spinner_item);     
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new OnItemSelected(
                    ));
            break;
        case R.id.radio_2 :
            adapter = ArrayAdapter.createFromResource(
                    this, R.array.prov1_array, android.R.layout.simple_spinner_item);     
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new OnItemSelected(
                    ));
            break;

        iii
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)  {
            String selected = (String) spinner.getSelectedItem();
        Intent myIntent = new Intent (spinner.getSelectedItem(), ProvBC.class);
        startActivityForResult(myIntent, 0);    

iii

        iii);
    iii
iii



onItemSelected

    import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class OnItemSelected implements OnItemSelectedListener {
    private boolean isFirst = true;

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

        if (isFirst) { 
            isFirst = false;
            iii else {
        Toast.makeText(arg0.getContext(), "The country is"+arg0.getItemAtPosition(arg2).toString(), 
                Toast.LENGTH_SHORT).show();
            iii
    iii

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    iii

iii
最佳回答

下表

spinner.getSelectedItemId() OR spinner.getSelectedItem()
问题回答

Edit: u need to do this:

  btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)  {
                }
String selected = spinner.getSelectedItem().toString();


        });

并打算与选定项目一起开展下一个活动,作为额外的项目

感谢每个人的帮助。 工作。 这里就是任何其他人需要的例子。

btn1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Spinner sp =    (Spinner)findViewById(R.id.spinner);
            String spinnerString = null;
            spinnerString = sp.getSelectedItem().toString();

            if (spinnerString.equalsIgnoreCase("British Columbia")){
                Intent myIntent = new Intent (v.getContext(), ProvBC.class);
                startActivityForResult(myIntent, 0);
            } else 
                if (spinnerString.equalsIgnoreCase("Alberta")){
                    Intent myIntent = new Intent (v.getContext(), ProvAb.class);
                    startActivityForResult(myIntent, 0);
                } 

        }
    });




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签