[英]how to show a list view with images in a fragment in android
我有一個帶有 4 個選項卡的片段,在一個選項卡中我需要顯示一個列表視圖。 我的代碼已經到了這一步,但有幾個錯誤,我不確定是我做的不對還是我遺漏了一些簡單的東西。 這是我正在使用的代碼
片段活性
public class SupportFragment extends SherlockFragment{
String[] supporters = new String[] {
"Gulf Shores",
"Central",
"Spanish Fort",
"Jackson Heights",
"Summerdale",
"Atlas",
"Robertsdale",
"Eastern Shore"
};
int[] images = new int[] {
R.drawable.gulfshores,
R.drawable.central,
R.drawable.spanishfort,
R.drawable.jacksonheights,
R.drawable.summerdale,
R.drawable.atlas,
R.drawable.robertsdale,
R.drawable.easternshore
};
String[] church = new String[]{
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.tab_support_layout, null);
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("sup", "Supporters : " + supporters[i]);
hm.put("chur","Church : " + church[i]);
hm.put("icon", Integer.toString(images[i]) );
aList.add(hm);
}
String[] from = {"sup", "chur", "icon"};
int[] to = {R.id.icon, R.id.sup, R.id.chur};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
return root;
}
}
我在這兩行代碼中收到錯誤消息
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
和
ListView listView = (ListView) findViewById(R.id.listview);
我得到的錯誤是:
未定義 SupportFragment 類型的 getBaseContext() 方法
未定義 SupportFragment 類型的 findViewById(int) 方法
用於列表視圖的 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/tableback" >
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
和列表視圖布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/sup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<TextView
android:id="@+id/chur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
非常感謝您能提供的任何幫助
你沒有FragmentActivity
你有一個Fragment
,所以當然你不能用getBaseContext()
獲得 Context ,你必須使用getActivity()
代替。 如果您想在 Fragment 中使用findViewById
,您還必須膨脹一個 View 。 但是,如果您想使用 Tabs,則需要一個FragmentActivity
來包含您的Fragments
。
你可以在你的片段onCreateView()
方法中像這樣膨脹一個視圖:
View view = inflater.inflate(R.layout.myLayout, null);
然后你可以像這樣調用findViewById
:
[...]view.findViewById(...);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.