2013-02-21 61 views
0

这里是我的代码:如何在排序时在Android中添加标题栏?

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必须是微调列表中的不可选模式。我怎样才能做到这一点?

回答

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

,但我估计微调仍将关闭列表每当用户点击。也许你必须重写微调OnItemClick才能获得一致的行为。

+0

我试试这个,但我得到错误。你能告诉我什么是等等等? – user1742971 2013-02-22 06:24:38

+0

只是Google'Android custom arrayadapter',你可以找到几个关于如何构建一个自定义适配器的教程,并且在你理解了那个部分之后,回到这里并重新阅读我给出的答案;) – Budius 2013-02-22 09:35:40

相关问题