0

我想将 .xml 布局应用于网格视图...

这是 gridlayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

然后在主要活动中(我正在使用片段):

public class Grid extends Fragment {

private View vi;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle saved) {

    View c = inflater.inflate(R.layout.choose, container, false);
    vi = c;

    GridView gridView = (GridView) vi.findViewById(R.id.gridView1);
    String[] numbers = { "A", "B", "C", "D", "E", "F", "G",
            "H", "I" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity()
            .getApplicationContext(), 
            R.layout.gridlayout,
            numbers);

    gridView.setAdapter(adapter);

    return c;
}

但程序打印错误

AndroidRuntime(4302): java.lang.IllegalStateException: ArrayAdapter requires
the resource ID to be a TextView

我试过了

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity()
            .getApplicationContext(), android.R.layout.simple_list_item_1,
            numbers);

而且效果很好......

请帮忙指出我的错误!!谢谢!

4

1 回答 1

0

我建议你应该先阅读API文档!

http://developer.android.com/reference/android/widget/ArrayAdapter.html

你应该使用这个构造函数

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        R.layout.gridlayout, //layout ID
        R.id.textView1, //the id of textView in the layout
        numbers);
于 2013-02-26T05:29:22.847 回答