我创建了一个有三个项目的微调器
每日
每周
每月
我在我的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方法(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> 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, 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,则根本不打印日志。位置不会被调用。即使只是位置记录也没有打印出来。
这真的很奇怪我不确定为什么会这样?有人可以帮我吗?
谢谢!
答案 0 :(得分:1)
如果要在片段中创建微调器,则必须在Fragment onCreatedView()中声明它。不在你的FragmentActivity的onCreate()中。
OnItemSelectedListener如果要在Fragment内的微调器上使用侦听器,则必须在FragmentMain内部而不是在Activity内部实现。
您的代码的这些更改已经过测试。
您的上下文返回null(无论如何,在我的测试代码中):
public class InformationList extends Activity implements OnItemSelectedListener {
.....
所以改为:
TitleNavigationAdapter adapter = new TitleNavigationAdapter(this,navSpinner); //添加了此
您的活动是否实施OnItemSelectedListener?:
@Override
public SpinnerNavItem getItem(int index) //SpinnerNavItem from Object
{
return spinnerNavItem.get(index);
}
我也会在TitleNavigationAdapter中更改此内容:
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
<强>结果:强>
RequestHandle
正常运作。
答案 1 :(得分:0)
首先删除getApplicationContext()
adapter = new TitleNavigationAdapter(getActivity(), navSpinner);
我认为它会被调用,但是因为你的Log
参数not filtering why?而是,所有Log
行尝试这个
Log.e("Position", "= " + String.valueOf(position));
在设置onitemSelectedListener
将初始选择重新设置为0后尝试后,看看是否存在显着差异
希望有所帮助
答案 2 :(得分:0)
此代码也有效:
//inflate
View rootView = this.findViewById(android.R.id.content);
setContentView(R.layout.list_item_title);
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)));
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 中的
<强> SpinnerNavItem:强> 公共类SpinnerNavItem
{ // this.icon = icon;
} } ** list_item_title.xml ** public class TitleNavigationAdapter extends ArrayAdapter<SpinnerNavItem>
{
public TitleNavigationAdapter(Context context,int resource ,ArrayList<SpinnerNavItem> 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;
}
}
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;
public String getTitle()
{
return this.title;
}
public int getIcon()
{
return this.icon;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Title"
android:id="@+id/txtTitle"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="Choose Time"
android:id="@+id/txtheading"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
答案 3 :(得分:0)
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);
好像你在任何你应该调用的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 {......}
以及上面的所有代码都在此类内部,或者您休息与此类相关的代码
或者你可以这样做
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的日志
答案 4 :(得分:0)
spbBloodGrp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ String bloodgroup =&#34;&#34 ;;
@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
}
});
它会帮助你,祝你好运