Spinner ComboBox DropDown List Android example code
Spinner in Android application is equivalent of ComboBox in WinApi.
Spinner get selected item to string example.
Spinner get selected item position to int example.
Main activity class MainComboBox.java
File MyOnItemSelectedListener.java
strings.xml
main.xml
Spinner get selected item to string example.
Spinner get selected item position to int example.
Main activity class MainComboBox.java
public class MainComboBox extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Button myButton =(Button)findViewById(R.id.button1);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Spinner sp = (Spinner)findViewById(R.id.spinner);
String spinnerString = null;
spinnerString = sp.getSelectedItem().toString();
int nPos = sp.getSelectedItemPosition();
Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos,
Toast.LENGTH_LONG).show();
}
});
}
}
File MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "Item is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
strings.xml
<resources>
<string name="hello">Hello World, MainComboBox!</string>
<string name="app_name">ComboBox</string>
<string name="prompt">Choose a country</string>
<string-array name="countries_array">
<item>China</item>
<item>India</item>
<item>USA</item>
<item>Indonesia</item>
<item>Brazil</item>
<item>Pakistan</item>
<item>Nigeria</item>
<item>Bangladesh</item>
<item>Russia</item>
</string-array>
</resources>
main.xml
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/prompt"
/>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
396LW NO topic_id
AD
Další témata ....(Topics)
int occupy 4 bytes (32 bits) in memory
int in Java example of using Integer and Array of Integer and Iteger to String
int in Java example of using Integer and Array of Integer and Iteger to String
// get max end min values of int in Java example 4 bytes (32 bits) PC architecture
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
// members variable
private int mProgress = 10;
//integer to string java
int myInteger = 8;
String myString = Integer.toString(myInteger);
// a final variable can only be initialized once
static final int NUM_PARTICLES = 15;
for (int i = 0; i < NUM_PARTICLES ; i++) {
// do something
}
// int as return value of function
public int getCount() {
return 5;
}
// int as a parametr of function
public float getFloatFromInt(int i) {
float fRet = (float) i;
return fRet;
}
//array of int
int[] anArray; // declares an array of integers
anArray = new int[2]; // allocates memory for 2 integers
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
for (int i = 0; i < anArray.length; i++) {
// print out values from anArray
System.out.println("Index: " + i);
System.out.println("Value: " + anArray[i]);
}
Unable to resolve target android-7
Try this solution:
Select project from tree (project explorer)
- right click on project
- properties
- select Android from tree
- change Project Build Target to higher (or change project build target)
- selct from menu: Project-Clean ( select your project - OK)
Try this solution:
Select project from tree (project explorer)
- right click on project
- properties
- select Android from tree
- change Project Build Target to higher (or change project build target)
- selct from menu: Project-Clean ( select your project - OK)
Hmotnost 160 g
Procesor 1.4 GHz Qualcomm Scorpion
Displej 800 x 480, 4.3 in. ClearBlack AMOLED capacitive touchscreen, 16m-color WVGA, Corning® Gorilla® Glass (odolný povrch)
Velikost displeje v palcích 4,3
Rozměry 128 x 68 x 11,5
Fotoaparát 8 Mpix, 3264x2448 pixelů, dual-LED flash, autofocus Carl Zeiss optika, 720p video capture
HD video, natačení videosekvencí, editace fotografií, přední kamera
Operační system Windows Phone 7
Datové funkce GPS modul, WiFi, Bluetooth, NFC, GPRS, EDGE, HSCSD
Přední kamera 1 Megapixel, 1280x720 pixelů
Připojení Bluetooth 2.1, 802.11b/g/n, dual mode A-GPS/GLONASS, micro-USB, 3.5mm audio jack, FM radio
Výhody svižný operační systém, editace MS Office dokumentů, napojení na cloudové uložiště SkyDrive (25GB prostoru zdarma), integrace sociálních sítí
Nevýhody v porovnání s Androidem méně aplikaci ke stažení zdarma
Procesor 1.4 GHz Qualcomm Scorpion
Displej 800 x 480, 4.3 in. ClearBlack AMOLED capacitive touchscreen, 16m-color WVGA, Corning® Gorilla® Glass (odolný povrch)
Velikost displeje v palcích 4,3
Rozměry 128 x 68 x 11,5
Fotoaparát 8 Mpix, 3264x2448 pixelů, dual-LED flash, autofocus Carl Zeiss optika, 720p video capture
HD video, natačení videosekvencí, editace fotografií, přední kamera
Operační system Windows Phone 7
Datové funkce GPS modul, WiFi, Bluetooth, NFC, GPRS, EDGE, HSCSD
Přední kamera 1 Megapixel, 1280x720 pixelů
Připojení Bluetooth 2.1, 802.11b/g/n, dual mode A-GPS/GLONASS, micro-USB, 3.5mm audio jack, FM radio
Výhody svižný operační systém, editace MS Office dokumentů, napojení na cloudové uložiště SkyDrive (25GB prostoru zdarma), integrace sociálních sítí
Nevýhody v porovnání s Androidem méně aplikaci ke stažení zdarma
[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Error parsing the sdk.
[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Failed to create C:\Program Files\android\sdk\add-ons.
[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Unable to parse SDK content.
You have to create add-ons folder as admin of PC
[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Failed to create C:\Program Files\android\sdk\add-ons.
[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Unable to parse SDK content.
You have to create add-ons folder as admin of PC
Replace diacritic marks: Á Č Ď É Ě Í Ň Ó Ř Š Ť Ú Ů Ý Ž
á č ď é ě í ň ó ř š ť ú ů ý ž
á č ď é ě í ň ó ř š ť ú ů ý ž
public String replaceDiacritic (String inputStr) {
Map<String, String> replacements = new LinkedHashMap<String,String>() {{
//Velká
put("Á","A");
put("Č","C");
put("Ď","D");
put("É","E");
put("Ě","E");
put("Í","I");
put("Ň","N");
put("Ó","O");
put("Ř","R");
put("Š","S");
put("Ť","T");
put("Ú","U");
put("Ů","U");
put("Ý","Y");
put("Ž","Z");
//Malá "," ");
put("á","a");
put("č","c");
put("ď","d");
put("é","e");
put("ě","e");
put("í","i");
put("ň","n");
put("ó","o");
put("ř","r");
put("š","s");
put("ť","t");
put("ú","u");
put("ů","u");
put("ý","y");
put("ž","z");
}
};
for(Map.Entry<String, String> entry : replacements.entrySet()) {
inputStr = inputStr.replaceAll(entry.getKey(), entry.getValue());
}
return inputStr;
}
Editace: 2011-09-26 20:46:41
Počet článků v kategorii: 396
Url:spinner-combobox-dropdown-list-android-code-example