Android 在';使用Arrayadapter无法显示任何内容
我试图使用ArrayAdapter填充包含ListView的片段,但一旦我运行应用程序,ListView就完全为空。我做错了什么 这是我的fragment_main.xmlAndroid 在';使用Arrayadapter无法显示任何内容,android,android-fragments,android-listview,Android,Android Fragments,Android Listview,我试图使用ArrayAdapter填充包含ListView的片段,但一旦我运行应用程序,ListView就完全为空。我做错了什么 这是我的fragment_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.medeasynetwork.MainActivity$PlaceholderFragment">
<ListView
android:id="@+id/myInfoLV"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
这也是我的MainActivity类中的占位符片段类:
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
private static int selection;
private static int fragmentId;
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
selection = sectionNumber;
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
ListView myInfoLV;
switch (selection) {
case 1:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
myInfoLV = (ListView) rootView.findViewById(R.id.myInfoLV);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, R.array.myInfoArray);
myInfoLV.setAdapter(adapter);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_my_directory, container, false);
break;
}
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
公共静态类占位符片段扩展片段{
/**
*表示此文件节号的片段参数
*碎片。
*/
私有静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
/**
*返回给定节的此片段的新实例
*号码。
*/
私有静态int选择;
私有静态int fragmentId;
公共静态占位符片段newInstance(int sectionNumber){
占位符片段=新占位符片段();
Bundle args=新Bundle();
参数Putin(参数段号,段号);
fragment.setArguments(args);
选择=区段编号;
返回片段;
}
公共占位符片段(){
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图rootView=null;
列表视图myInfoLV;
开关(选择){
案例1:
rootView=充气机。充气(R.layout.fragment_main,容器,假);
myInfoLV=(ListView)rootView.findviewbyd(R.id.myInfoLV);
ArrayAdapter=newArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,R.array.myInfoArray);
myInfoLV.setAdapter(适配器);
打破
案例2:
rootView=inflater.inflate(R.layout.fragment\u my\u目录,容器,false);
打破
}
返回rootView;
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
((主活动)活动)。已附加节(
getArguments().getInt(参数节号));
}
}
案例2工作正常,因为我在XML中填充了ListView,但是当我尝试使用ArrayAdapter时,ListView显示为空白。请帮我找出问题所在
提前谢谢 确定数组不是空的吗?非常确定,我使用相同的数组但通过XML填充了ListView。我使用的解决方法是在MainActivity中声明列表,但仍然不知道为什么它不能以另一种方式工作。