gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:05 25 4
gpt4 key购买 nike

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

每日

每周

每月

我在我的 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, 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(我正确获取日志)时,上面的代码工作正常,然后如果我选择位置 0,则从位置 1 或 2 根本不打印日志。位置没有被调用。即使只是位置日志也不会打印出来。

这真的很奇怪,我不确定为什么会这样?有人可以帮我吗?

谢谢!

最佳答案

如果你想在Fragment中制作一个Spinner,你必须在Fragment的onCreatedView()中声明它。不在 FragmentActivity 的 onCreate() 中。

OnItemSelectedListener 必须在 FragmentMain 内实现,而不是在 Activity 内实现,如果您想在 Fragment 内的微调器上使用监听器。

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

您的上下文返回 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

正常运行。

spinner

关于android - 在 Android 的 Spinner 中未选择位置 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31355630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com