如何在android中的片段中显示带有图像的列表视图

时间:2012-11-10 00:20:33

标签: android listview android-listview android-fragments

我有一个包含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 for listview

 <?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>

非常感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:2)

您没有FragmentActivity您拥有Fragment,因此您无法使用getBaseContext()获取上下文,您必须使用getActivity()代替。 如果要在片段中执行此操作,还必须使用findViewById来扩充视图。但是,如果您想使用标签,则需要使用FragmentActivity来处理您的Fragments

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

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

然后你可以这样打电话给findViewById

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