0

I'm trying to print an Array String[] in the listView of my fragment and I always get an error. Here's my fragment class:

public class Fragment_Database extends Fragment

public Fragment_Database() {
    // Constructor
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_database, container, false);

    ListView list;
    list = (ListView) v.findViewById(R.id.listView1);

    String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
            "Ubuntu", "Solaris", "Android", "iPhone"};

    list.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.fragment_database, names));
    return v;
}

}

Here's the XML:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

If I put a ListView in the XML I get this error:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

And if I put a TextView in the XML, I get this one:

android.widget.TextView cannot be cast to android.widget.ListView

Jaythaking
  • 2,200
  • 4
  • 25
  • 67

2 Answers2

0

Instead of

list.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.fragment_database, names));

Try

list.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.id.text1, names));

Regarding the details of android.R.id.text1 found here.

Community
  • 1
  • 1
NullPointerException
  • 3,978
  • 4
  • 34
  • 52
0

your code is wrong.

list.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.fragment_database, names));

you use this code.

list.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, names));

R.layout.fragment_database is not ListviewItem's xml file.
You use custom item layout or simpleItemLayout.
read this page and try again.
http://www.vogella.com/tutorials/AndroidListView/article.html

Amadas
  • 703
  • 1
  • 5
  • 10
  • Thanks! :D It works... Which layout should i use if I want a gridView instead of a simplee_list... ? do you know? i'm closing the question after :P – Jaythaking Mar 26 '14 at 01:52
  • I'm not use GridView. but i can this page easily. [link]http://androidexample.com/Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=75&aaid=99 – Amadas Mar 26 '14 at 01:55