0

I have created to arrays.One is for all the states(Full Form) in USA and other is the all short form of the states in USA.I have dispayed the states(Full form) in a spinner.Now what i have to do is display the state in short form according to which the user selects in the spinner.How can i achieve it.

5
  • 1
    Post your code what you have tried. Commented May 14, 2014 at 6:39
  • Get index of selected item of spinner and use this index to get value from other array... Commented May 14, 2014 at 6:42
  • how to do it @Haresh pls can u explain it?? Commented May 14, 2014 at 6:43
  • have you done any code for this then plz post ? Commented May 14, 2014 at 6:46
  • Load the list(whatever you need short or full) in the adapter depend upon your situation from where you need. Commented May 14, 2014 at 6:46

2 Answers 2

1

You should use mapping i.e in both the arrays the elements should be mapped. Let me give you an example.

    String[] fullForm = { Alabama,  California,  Florida,   Illinois};
    String[] shortForm = {AL, CA, FL, IL};

    ArrayAdapter<String> ad = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,fullForm );

spinner.setAdapter(ad);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub


                Toast.makeText(getApplicationContext(), " You selected " + fullForm[position] + "\n Short form is : " + shortForm[position], Toast.LENGTH_SHORT).show();
            }

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

            }
        });
Sign up to request clarification or add additional context in comments.

Comments

1
// try this way,hope this will help you...

**activity_main.xml**

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

     <Spinner
         android:id="@+id/spnState"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
</LinearLayout>

**MainActivity.java**

public class MainActivity extends Activity {

    Spinner spnState;
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_main);
        spnState = (Spinner) findViewById(R.id.spnState);
        String[] stateFullNameArray = new String[]{"statefullname1","statefullname2","statefullname3","statefullname4","statefullname5"};
        final String[] stateShortNameArray = new String[]{"stateshortname1","stateshortname2","stateshortname3","stateshortname4","stateshortname5"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,stateFullNameArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnState.setAdapter(adapter);

        spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,stateShortNameArray[position],Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.