0

这是我的代码:

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
            }
        });
    }
}

我想在微调器上按字母顺序显示国家/地区列表。在此之前它应该显示 A、B、C 到 Z。但是这个 A 到 Z 在微调器列表中必须是不可选择的模式。我怎样才能做到这一点?

4

1 回答 1

0

您必须创建扩展 ArrayAdapter 的自定义适配器。

这可能很容易,例如:

 // 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
     }
}

但我认为 Spinner 仍然会在用户单击时关闭列表。也许您必须覆盖微调器 OnItemClick 以获得连贯的行为。

于 2013-02-21T11:25:29.690 回答