我得到的无法解析ListView和ArrayAdapter的符号,试图解决很长时间,建议plz。
res / layout包含activity_main.xml
,fragment.xml
和layout_textview.xml
fragment.xml之
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context="com.example.sunshine.BlankFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_gravity="center_vertical" >
</ListView>
</FrameLayout>
layout_textview.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="?attr/listPreferredItemHeight"
android:id="@+id/text_view">
</TextView>
BlankFragment.java
public class BlankFragment extends Fragment {
private View rootview;
private ListView listView;
private ArrayAdapter<String> itemAdapter;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_blank, container, false);
String[] forecastArray={"Monday-sunny","Tuesday-cloudy","wednesday-sunny","Thursday-rain","friday-cloudy","saturday-sunny","sunday-cloudy"};
List<String> weekforecast=new ArrayList<String>(Arrays.asList(forecastArray));
itemAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.layout_textview,R.id.text_view, weekforecast);
listView=(ListView)rootview.findViewById(R.id.listView);
listView.setAdapter(weekforecast);
return rootview;
}
}
答案 0 :(得分:1)
public class BlankFragment extends Fragment {
private View rootview;
private ListView listView;
private ArrayAdapter<String> itemAdapter;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_blank, container, false);
String[] forecastArray={"Monday-sunny","Tuesday-cloudy","wednesday-sunny","Thursday-rain","friday-cloudy","saturday-sunny","sunday-cloudy"};
final List<String> weekforecast=new ArrayList<String>();
for (int i = 0; i < forecastArray.length; ++i)
{
weekforecast.add(forecastArray[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, weekforecast);
listView=(ListView)rootview.findViewById(R.id.listView);
listView.setAdapter(adapter);
return rootview;
}
试试此代码