在Android中的Spinner中没有选择位置0

前端之家收集整理的这篇文章主要介绍了在Android中的Spinner中没有选择位置0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个有三个项目的微调器

日常

每周

每月一次

我在我的java文件中执行了以下操作:

navSpinner = new ArrayList<SpinnerNavItem>();
        navSpinner.add(new SpinnerNavItem(getResources().getString(R.string.dailyview)));
        navSpinner.add(new SpinnerNavItem(getResources().getString(R.string.weekview)));
        navSpinner.add(new SpinnerNavItem(getResources().getString(R.string.monthview)));

        adapter = new TitleNavigationAdapter(getActivity().getApplicationContext(),navSpinner);
        mSpinner = (Spinner) rootView.findViewById(R.id.spinner);
        mSpinner.setAdapter(adapter);
        mSpinner.setOnItemSelectedListener(this);

OnItemSelected方法(实现AdapterView.OnItemSelectedListener)

public void onItemSelected(AdapterView<?> parentView,View v,int position,long id) {

        Log.e("Position","= " + position);

        if (mNaviFirstHit)
        {
            mNaviFirstHit = false;
        }
        else
        {

            Fragment fragment = null;
            switch (position) {
                case 0:

                    Log.e("Week","= " + position);
                    break;

                case 1:

                    backspace = 1;

                    Log.e("Week","= " + position);

                    break;

                case 2:

                    backspace = 1;

                    Log.e("Week","= " + position);

                    break;

                default:
                    break;
            }
        }
    }

    public void onNothingSelected(AdapterView<?> parentView) {

    }

适配器:

public class TitleNavigationAdapter extends BaseAdapter 
{
    private TextView txtTitle;
    private ArrayList<SpinnerNavItem> spinnerNavItem;
    private Context context;
    private TextView txtHeading;
    private SharedPreferences pref;

    public TitleNavigationAdapter(Context context,ArrayList<SpinnerNavItem> spinnerNavItem) 
    {
        this.spinnerNavItem = spinnerNavItem;
        this.context = context;
    }

    @Override
    public int getCount() 
    {
        return spinnerNavItem.size();
    }

    @Override
    public Object getItem(int index) 
    {
        return spinnerNavItem.get(index);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) 
    { 
        if (convertView == null) 
        {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item_title,null);
        }

        txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
        txtTitle.setText(spinnerNavItem.get(position).getTitle());

        txtTitle.setTextColor(context.getResources().getColor(R.color.orangeText));
        txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);

        pref = context.getSharedPreferences("MyPref",Context.MODE_PRIVATE);
        String text = pref.getString("selectedItem",context.getResources().getString(R.string.transaction_main_gridview));

        if (text.equalsIgnoreCase(convertView.getResources().getString(R.string.transaction_main_gridview)))
        {
            txtTitle.setText(convertView.getResources().getString(R.string.transaction_main_gridview_text));
        }
        else if (text.equalsIgnoreCase(convertView.getResources().getString(R.string.transaction_main_weekview)))
        {
            txtTitle.setText(convertView.getResources().getString(R.string.transaction_main_weekly_text));
        }
        else if (text.equalsIgnoreCase(convertView.getResources().getString(R.string.transaction_main_monthview)))
        {
            txtTitle.setText(convertView.getResources().getString(R.string.transaction_main_monthly_text));
        }

        return convertView;
    }


    @Override
    public View getDropDownView(int position,ViewGroup parent) 
    {
        if (convertView == null) 
        {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item_title,null);
        }

        txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
        txtTitle.setPadding(20,20,20);

        txtHeading = (TextView) convertView.findViewById(R.id.txtheading);
        txtHeading.setVisibility(View.GONE);

        txtTitle.setText(spinnerNavItem.get(position).getTitle());
        return convertView;
    }

}

当我选择位置1或2(我正确地记录日志)然后从位置1或2,如果我选择位置0日志没有打印,上述代码工作正常.位置不被调用.即使位置记录没有被打印.

这真的很奇怪,我不知道为什么会发生这种情况?有人可以帮我吗?

谢谢!

解决方法

如果要在片段中创建一个Spinner,您必须在Fragment onCreatedView()中声明它.不在你的FragmentActivity onCreate().

OnItemSelectedListener如果要在Fragment中的旋转器上使用侦听器,则必须在FragmentMain内部实现,而不是在Activity中.

您的代码的这些更改已经过测试.

你的上下文返回null(在我的测试代码中):

Context mContext = getActivity().getApplicationContext();// returns null !

所以改为:

TitleNavigationAdapter adapter = new TitleNavigationAdapter(this,navSpinner); //添加此项

你的活动是否包含OnItemSelectedListener?:

public class InformationList  extends Activity implements OnItemSelectedListener {
    .....

我也会在TitleNavigationAdapter中改变这一点:

@Override
public SpinnerNavItem getItem(int index) //SpinnerNavItem from Object
    {
        return spinnerNavItem.get(index);
    }

结果:

07-18 19:46:53.468: E/Position(27325): = 0
07-18 19:46:53.468: E/day(27325): = 0
07-18 19:47:04.487: E/Position(27325): = 1
07-18 19:47:04.488: E/Week(27325): = 1
07-18 19:47:06.426: E/Position(27325): = 2
07-18 19:47:06.426: E/month(27325): = 2

正常运行

猜你在找的Android相关文章