標籤:android spinner spinneradapter
彈窗常用AlertDialog來襪,因為AlertDialog可以很方便的添加所要的項。比如確定/取消按鈕,自訂的標題列、自訂的資訊布局等。在自訂布局時,比如我們採用LinearLayout來布局,然後再往LinearLayout中添加一些項,比如TextView、EditText等。這些都很容易實現。
不過,如果添加spinner,可能就會出問題了。找了很久,發現是context引起的。因為spinner在點擊展開下拉式清單時,需要依賴於表單,而使用context只是提供了一個上下文,並沒有提供表單,所以會造成錯誤。下面是已實現的代碼:
package com.sparkle;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.Color;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.Spinner;import android.widget.SpinnerAdapter;import android.widget.TextView;public class Contury {private Activity _activity = null;private Context _context = null;private Spinner _spinner = null;public Contury(Activity activity) {_activity = activity;_context = activity;}private LinearLayout GenerateLinearLayout(int orientation) {LinearLayout linearLayout = new LinearLayout(_context);linearLayout.setPadding(16, 16, 16, 16);linearLayout.setOrientation(orientation);return linearLayout;}@SuppressWarnings("deprecation")private Spinner GenerateSpinner() {Spinner spinner = new Spinner(_activity);LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);spinner.setLayoutParams(layoutParams);return spinner;}public void Popup() {Builder alertDialogBuilder = new AlertDialog.Builder(_activity);View view = GenerateLayout();alertDialogBuilder.setView(view);alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Log.e("TEST","OK");}});alertDialogBuilder.setNegativeButton("Cancel", null);Dialog alertDialog = alertDialogBuilder.create();alertDialog.show();}private View GenerateLayout() {LinearLayout linearLayout = GenerateLinearLayout(LinearLayout.VERTICAL);LinearLayout messageLinearLayout = GenerateMessageLayout();linearLayout.addView(messageLinearLayout);return linearLayout;}private LinearLayout GenerateMessageLayout() {LinearLayout linearLayout = GenerateLinearLayout(LinearLayout.HORIZONTAL);_spinner = GenerateSpinner();_spinner.setAdapter(new CityAdapter(_activity));_spinner.setSelection(0);linearLayout.addView(_spinner);return linearLayout;}private class CityAdapter extends BaseAdapter implements SpinnerAdapter {private Context _con=null;private String[] _cities=null;public CityAdapter(Context context){_con=context;_cities=_con.getResources().getStringArray(R.array.cities);} @Override public int getCount() { if(_cities==null) { return 0; } return _cities.length; } @Override public Object getItem(int position) { if(_cities==null||_cities.length<=0) { return null; } return _cities[position]; } @Override public long getItemId(int position) { return position; } @Overridepublic View getView(int position, View convertView, ViewGroup parent) {TextView textView=new TextView(_con);textView.setTextColor(Color.BLACK);textView.setTextSize(20);textView.setText(_cities[position]);return textView;} }}
註:
1、spinner和AlertDialog的new需使用activity,使用context將在彈出下拉式功能表時造成錯誤。
2、_con.getResources().getStringArray(R.array.cities)是載入xml中定義的數組。
city.xml(位於values檔案夾下)
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="cities"> <item>廈門</item> <item>北京</item> <item>上海</item> </string-array></resources>
Android彈窗中的spinner異常解決辦法