0

I'm trying to show a number of items on a listView but it's not showing on the screen. It's just dummy information but it's still not working. I've tried to see a bunch of tutorials but found nothing.

Here is the xml file

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:fitsSystemWindows="true"
android:id="@+id/container"
tools:context="com.example.pedrofialho.musicristo.SearchActivity">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="55dp" />

</RelativeLayout>

and here is the java code

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_actvity);
    container = (ViewGroup) findViewById(R.id.container);
    LayoutInflater inflater = LayoutInflater.from(this);
    View rootView = inflater.inflate(R.layout.listview_search,container,false);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    list = (ListView) rootView.findViewById(R.id.list);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    String[] items = {"Comunhão","Passo a Passo","Tudo bem?","Esta lista não tem sentido"};
    mArrayList = new ArrayList<>(Arrays.asList(items));
    mArrayAdapter = new ArrayAdapter<>(this, R.layout.activity_search_actvity, R.id.list, mArrayList);
    list.setAdapter(mArrayAdapter);
}

On the screen it shows nothing just a white screen. Thank you in advance

Raidline
  • 81
  • 9
  • Just to debug, try to remove the ListView from XML and put a TextView with some text in it and see if you can see it. – Nabin May 07 '16 at 15:23

4 Answers4

1
View rootView = inflater.inflate(R.layout.listview_search,container,false);

inflates a new screen, which is not at screen until you put it explicitly, even adding it to a container part of the current View hierarchy or replacing the current content (calling again setContentView). Accordingly to the layout you posted, activity_search_actvity.xml I assume, contains already a ListView, so get rid of the inflater, and simply use findViewById to retrive the object part of the layout you set with setContentView(R.layout.activity_search_actvity);. You also have instantiated the ArrayAdapter with the wrong parameters. The second parameter is the layout you want to use a list's item and the third is the id of a TextView you want to use to show your dataset.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Here is the proper way of ArrayAdapter initialization. You have to provide a layout containing eg. a TextView (if you want to show texts), so you should modify the line with new ArrayAdapter... to something like this: mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mArrayList);, because now your adapter gets the layout of your activity, not an item layout...
If you have to use a custom layout for each item, you should pass its id, and also the id of the TextView.
If you need a more complex item view, you can extend either ArrayAdapter or BaseAdapter classes to achieve the creation of item views.

nvi9
  • 1,733
  • 2
  • 15
  • 20
0

You are passing wrong parameter to array adapter. In Your case it will be as ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mArrayList)

For details ArrayAdapter in android to create simple listview

Use app:layout_behavior="@string/appbar_scrolling_view_behavior" in Relative layout.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="55dp" />

</RelativeLayout>

As explained by the support library guidelines:

Community
  • 1
  • 1
USKMobility
  • 5,721
  • 2
  • 27
  • 34
0

Use this

 ArrayAdapter <String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.array);

Don't forget to attach the list view to it.

 ListView listView=getListView();//Your activity shoudl extends ListActivity
 listView.setAdapter(adapter);