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
-
1Post your code what you have tried.Hamid Shatu– Hamid Shatu2014-05-14 06:39:04 +00:00Commented May 14, 2014 at 6:39
-
Get index of selected item of spinner and use this index to get value from other array...Hareshkumar Chhelana– Hareshkumar Chhelana2014-05-14 06:42:54 +00:00Commented May 14, 2014 at 6:42
-
how to do it @Haresh pls can u explain it??anuj– anuj2014-05-14 06:43:52 +00:00Commented May 14, 2014 at 6:43
-
have you done any code for this then plz post ?Hareshkumar Chhelana– Hareshkumar Chhelana2014-05-14 06:46:38 +00:00Commented 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.Hulk– Hulk2014-05-14 06:46:59 +00:00Commented May 14, 2014 at 6:46
Add a comment
|
2 Answers
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
}
});
Comments
// 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) {
}
});
}
}