1

4 つのタブを持つフラグメントがあり、1 つのタブにリストビューを表示する必要があります。私は自分のコードでここまでやってきましたが、いくつかのエラーがあり、正しく実行していないだけなのか、単純なものが欠けているのかわかりません。ここに私が使用している私のコードがあります

フラグメント アクティビティ

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;

 }



 }

これから、これらの 2 行のコードでエラーが発生します

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>

あなたが与えることができるどんな助けも大歓迎です

4

1 に答える 1

2

FragmentActivityを持っていないFragmentので、もちろん でコンテキストを取得することはできません。代わりgetBaseContext()に使用する必要があります。フラグメントでそれを行う場合は、getActivity()使用するビューをインフレートする必要もあります。findViewByIdただし、タブを使用する場合は、 が必要FragmentActivityですFragments

onCreateView()フラグメントメソッドで、次のように View をインフレートできます。

View view = inflater.inflate(R.layout.myLayout, null);

findViewById次に、次のように呼び出すことができます。

[...]view.findViewById(...); 
于 2012-11-10T00:33:21.237 に答える