2016-09-14 70 views
0

我得到無法解析ListView和ArrayAdapter的符號,試圖解決很長時間,建議PLZ。 RES /佈局包含activity_main.xmlfragment.xmllayout_textview.xml無法解析符號ListView和ArrayAdapter

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

什麼是你的片段的'import'語句? –

+0

謝謝你,包括ListView和ArrayAdapter的導入語句,以便它現在可以工作。 –

回答

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; 
    } 

試試這個代碼

+0

謝謝你,在listView.setAdapter(適配器)中有錯誤; –