我有一个带有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>


和listview布局

<?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()获取上下文,而必须使用getActivity()
如果要在片段中执行此操作,则还必须将View膨胀以使用findViewById。但是,如果您想使用制表符,则需要一个FragmentActivity来简化您的Fragments

您可以在片段onCreateView()方法中为这样的视图充气:

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


然后,您可以像这样调用findViewById

[...]view.findViewById(...);

09-10 06:15
查看更多