1
我有一個帶有4個選項卡的片段,在一個選項卡中,我需要顯示列表視圖。我在代碼中得到了這麼多,但有一些錯誤,我不確定我是不是正確地做對了,或者我錯過了一些簡單的事情。這裏是我的代碼,我使用如何顯示列表視圖與Android中的片段中的圖像
片段活動
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);
得到一個錯誤我得到的錯誤是:
方法getBaseContext()是未定義的類型SupportFragment
方法findViewById(INT)是未定義的類型SupportFragment
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>
任何幫助你可以給予大大讚賞
您的權利,我不知道爲什麼我說片段活動,getActivity()工作來解決這個錯誤,但我不是100%肯定我如何膨脹視圖來使用findViewById。我甚至不確定這是否是一種很好的方法,我正在使用它來執行此操作。對此有任何想法,或者你能指點我能夠誇大視圖 – Bryan
我確實有一個片段活動,它處理我所有的片段,這是它處理的片段之一 – Bryan
@Bryan我編輯了帖子 – Ahmad