在我尝试在片段中的微调器上设置适配器并运行应用程序后,一切正常但旋转器中的列表为空。在搜索了很多像我的问题之后,我仍然无法弄清楚原因。 任何建议将不胜感激。
public class DrinkFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_drink, container, false);
mSpDrinkItem = (Spinner)v.findViewById(R.id.sp_drink_list);
ArrayAdapter<CharSequence> drinkAd = ArrayAdapter.createFromResource(getActivity(), R.array.drink_item, android.R.layout.simple_spinner_item);
drinkAd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpDrinkItem.setAdapter(drinkAd);
mSpTemp = (Spinner)v.findViewById(R.id.sp_temp_list);
mBtnOrder = (Button)v.findViewById(R.id.btn_order);
mTxvOrderedList = (TextView)v.findViewById(R.id.txv_ordered_list);
setListeners();
return v;
}
答案 0 :(得分:0)
嗯......我发现你发布的代码没有任何问题。我想知道你的fragment_drink.xml文件是否存在问题?
无论如何,我根据你的数据整理了一个工作实例。
MainActivity:
package com.example.spinnerfragmenttest;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
DrinkFragment:
package com.example.spinnerfragmenttest;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class DrinkFragment extends Fragment
{
private Spinner mSpDrinkItem;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_drink, container, false);
mSpDrinkItem = (Spinner)v.findViewById(R.id.sp_drink_list);
ArrayAdapter<CharSequence> drinkAd = ArrayAdapter.createFromResource(getActivity(), R.array.drink_item, android.R.layout.simple_spinner_item);
drinkAd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpDrinkItem.setAdapter(drinkAd);
return v;
}
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment android:name="com.example.spinnerfragmenttest.DrinkFragment"
android:id="@+id/drink_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
fragment_drink.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner android:id="@+id/sp_drink_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="drink_item">
<item>珍珠奶茶</item>
<item>波霸奶茶</item>
<item>仙草凍奶茶</item>
<item>檸檬汁</item>
</string-array>
</resources>
这将生成以下微调器(显示为正常和下拉模式)