我是android新手,一直关注udacity的视频讲座。 列表视图不会被数组适配器和何时填充 我在移动设备上运行应用程序,应用程序只显示了主要的活动布局。
列表视图没有出现在手机屏幕上,任何人都可以告诉我这里的错误是什么,它是与帧布局相关的还是有一个 错误 数组适配器。 提前致谢
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class PlaceHolderFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public PlaceHolderFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray={
"Today","Tommorow","wed","Thursday","Friday","Saturday","Sunday"
};//creating some fake data to show in list view
List<String> fore=new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter=new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
ListView listview= (ListView)rootview.findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
return rootview;
}
}
}
activity_main_xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context="com.saxenarc.jeetesh.layouts.MainActivity">
</FrameLayout>
list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/list_item_forecast_textview"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
>
</TextView>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview_forecast"></ListView>
</FrameLayout>
在发布之前检查所有内容三次。
答案 0 :(得分:2)
您的代码和列表运行良好,但您尚未告知列表要显示的位置。您可以通过两种方式解决此问题。
<强>第一强>
在主要活动中添加片段,因为它不可见。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager.beginTransaction().replace(R.id.container, new PlaceHolderFragment).commit();
}
<强>第二强>
停止使用片段并使用主要活动
中的列表视图activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview_forecast"></ListView>
</FrameLayout>
MainActivity.java
List<String> fore = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(this,R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
ListView listview= (ListView) findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);