日常
每周
每月一次
我在我的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中改变这一点:
@OverrIDepublic SpinnerNavItem getItem(int index) //SpinnerNavItem from Object { return spinnerNavItem.get(index); }
结果:
07-18 19:46:53.468: E/position(27325): = 007-18 19:46:53.468: E/day(27325): = 007-18 19:47:04.487: E/position(27325): = 107-18 19:47:04.488: E/Week(27325): = 107-18 19:47:06.426: E/position(27325): = 207-18 19:47:06.426: E/month(27325): = 2
正常运行
总结以上是内存溢出为你收集整理的在Android中的Spinner中没有选择位置0全部内容,希望文章能够帮你解决在Android中的Spinner中没有选择位置0所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)