首页 > 代码库 > Android弹窗中的spinner异常解决办法

Android弹窗中的spinner异常解决办法

弹窗常用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() {
					@Override
					public 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;
        }

  
		@Override
		public 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异常解决办法