Here is my code:
public class MainActivity extends Activity {
Spinner spin;
TextView tex;
String[] country = {"A", "Afghanistan", "Albania", "Etc"};// A to Z country names
String[] code = {"+93", "+91", "Etc"}; // A to Z country Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner)findViewById(R.id.spinner1);
tex = (TextView)findViewById(R.id.tex);
ArrayAdapter aa1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, country);
aa1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setPrompt("Select the Country");
spin.setAdapter(aa1);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tex.setText(code[arg2]);
// tex.setText(country[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
I want to display the country's list in alphabetic order on spinner. And before that it should display the A, B, C up to Z. But This A to Z must be unselectable mode in spinner list. How can I achieve that?
You'll have to create your custom adapter that extends the ArrayAdapter.
it will probably be very easy, something like:
// the get view on your adapter
getView(LayoutInflater, etc, etc){
convertView = super.getView(inflater, etc, etc);
if(getItem(position).equals("A") || getItem(position).equals("B") || // etc, or create some clever way to go through a ArrayList with just the letters ){
convertView. // set not clickable stuff
}
}
but I reckon the Spinner still will close the list whenever the user clicks. Maybe you must override the spinner OnItemClick to get a coherent behaviour.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.