0

I want to apply a .xml layout to a grid view...

This is the 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>

and then in main activity (i'm using fragments):

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;
}

but the programs prints error

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

I have tried

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

And it worked fine....

Please help and point out my mistakes!! thanks!

manlio
  • 18,345
  • 14
  • 76
  • 126
MW_hk
  • 1,181
  • 4
  • 14
  • 39

1 Answers1

0

I suggest that you should first read API documents!

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

you should use this constructor

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);
  • yes, your solution works too! I'll guess I'll stick with your solution as it allows me to further develop the layout, thanks =] – MW_hk Feb 26 '13 at 05:46