在Android中的Spinner中未选择位置0

我创建了一个有三个项目的微调器

日常

每周

每月一次

我在我的java文件中做了以下内容:

navSpinner = new ArrayList(); 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方法(Implements 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; private Context context; private TextView txtHeading; private SharedPreferences pref; public TitleNavigationAdapter(Context context, ArrayList 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, 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.setPadding(20, 20, 0, 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,则根本不打印日志。 位置不会被调用。 即使只是位置日志也没有打印出来。

这真的很奇怪我不知道为什么会这样呢? 有人可以帮我这个吗?

谢谢!

如果要在片段中创建一个微调器,则必须在Fragment onCreatedView()中声明它。 不在FragmentActivity的onCreate()中。

OnItemSelectedListener如果要在Fragment内的微调器上使用侦听器,则必须在FragmentMain内部而不是在Activity内部实现。

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

你的上下文返回null(无论如何我的测试代码):

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

所以改为:

TitleNavigationAdapter adapter = new TitleNavigationAdapter(this,navSpinner); //添加了这个

你的Activity实现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 

function正常。

微调

首先删除getApplicationContext()

 adapter = new TitleNavigationAdapter(getActivity(), navSpinner); 

另外我认为它被调用但是因为你的Log参数, 而不是为什么过滤 ?,对所有Log行尝试这个

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

设置onitemSelectedListener之后可以尝试将初始选择重新设置为0,以查看是否存在显着差异

希望能帮助到你

此代码也有效:

  //inflate View rootView = this.findViewById(android.R.id.content); setContentView(R.layout.list_item_title); navSpinner = new ArrayList(); 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))); if(navSpinner != null && navSpinner.size() > 0) { // Context mContext = getActivity().getApplicationContext();//jrg returns null ! TitleNavigationAdapter adapter = new TitleNavigationAdapter(this, android.R.layout.simple_spinner_item, navSpinner);//jrg adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Spinner mSpinner = (Spinner) rootView.findViewById(R.id.spinner); mSpinner.setAdapter(adapter); mSpinner.setOnItemSelectedListener(this); } }//OnCreate 

在TitleNavigationAdapter中

  public class TitleNavigationAdapter extends ArrayAdapter { public TitleNavigationAdapter(Context context,int resource ,ArrayList navSpinner) { super(context, resource , navSpinner);//jrg this.spinnerNavItem = navSpinner; this.context = context; } ..... @Override public void registerDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public int getItemViewType(int position) { // TODO Auto-generated method stub return 0; } @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 0; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return false; } } 

SpinnerNavItem:

公共类SpinnerNavItem {

 private String title; private int icon; public SpinnerNavItem(String title, int icon) { this.title = title; this.icon = icon; } public SpinnerNavItem(String title) { this.title = title; 

// this.icon = icon; }

 public String getTitle() { return this.title; } public int getIcon() { return this.icon; } 

}

** list_item_title.xml **

        
  navSpinner = new ArrayList(); 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);

上面的代码行是调用适配器的正确方法

就像你在任何Activity中调用适配器一样,你应该这样调用

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

要么

 adapter = new TitleNavigationAdapter(YourActivityNAme.this, navSpinner); 

或者你在调用这个adpater之类的地方就像在Fragment或者listView,Gridview等中从你调用的任何地方膨胀你需要传递Context的那个Activity或者那个Activity类引用的布局

 Context cntxt;//declare in class cntxt=getActivity()//in oncreate() in case or fragment cntxt=context;//by passing the Activity reference in the constructor of YourClassName class 

context是在构造函数中传递的值,如YourClassName(Context context ,...other parameters){....}

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

也在这里你在setOnItemSelectedListener中使用它,所以你需要在同一个类中提供这个监听器的实现,你在这里引用你的public class YourClassName extends YourExtendsClassName implements OnItemSelectedListener {......}所以public class YourClassName extends YourExtendsClassName implements OnItemSelectedListener {......} ,所有上面的代码都进入这个类或您rest与此类相关的代码,或者您可以执行此操作

  mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, 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; } } } @Override public void onNothingSelected(AdapterView parent) { // TODO Auto-generated method stub }}); 

而且你需要修改更安全的一面,你也应该在TitleNavigationAdapter中更改它:

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

并且您的代码也将运行位置0元素也请检查您在public void onItemSelected(AdapterView parent, View view, int position, long id){...}编写的逻辑,如果您需要真的很有帮助,请提供完整的信息,而不是破碎的代码

现在你也可以看到位置0的日志

spbBloodGrp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){String bloodgroup =“”;

  @Override public void onItemSelected(AdapterView arg0, View arg1, int position, long id) { // TODO Auto-generated method stub //String bloodgroup = spbBloodGrp.getSelectedItem().toString(); if (position != 0) { bloodgroup = spbBloodGrp.getSelectedItem().toString(); } // Toast.makeText(getBaseContext(), bloodgroup, Toast.LENGTH_SHORT).show(); etxtbloodGrp.setText(bloodgroup); } @Override public void onNothingSelected(AdapterView arg0) { // TODO Auto-generated method stub } }); 

祝你好运,祝你好运