0

I wanted to generate a list view using below code. But after running this code the screen just shows a blank screen and the "hello" text which is there in main layout

package com.android.test1;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Toast;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;

public class HelloListView extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        String a[] = new String[]{"a", "b", "c", "d"};
         setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));

          ListView lv = getListView();

          lv.setTextFilterEnabled(true);

    }
}

and the main layout code is as given below

<?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" >
    <TextView 
        android:text="hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFFFF">
</ListView>

</LinearLayout>
Housefly
  • 4,324
  • 11
  • 43
  • 70

4 Answers4

2

Your ListView in XML should have this android:id="@android:id/list"

and remove that background attribute..

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • and shud i use that id anywhere?? – Housefly Apr 02 '12 at 11:00
  • No... It should be in ListView of your **main.xml** – ngesh Apr 02 '12 at 11:01
  • If you call setListAdapter() on the ListActivity, you are telling it to set the adapter to a ListView with id "@android:id/list", as the answer says. Instead, you can also use an id like "@+id/listview", get it via "findViewById(R.id.listview)", and set the adapter directly on this object. Then you can also remove inheritance from ListActivity and just subclass from Activity. – manmal Apr 02 '12 at 11:22
  • @manmal.. The 2nd thing you told works with ACtivities and not with ListActivity.. – ngesh Apr 02 '12 at 11:36
0

Archie.bpgc see this tutorial http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGallery.php#ListView

if you extend Listactivity you do not add

setContentView(R.layout.main);

and refer it like

ListView listView = (ListView) findViewById(R.id.list_view);

and if you do it like this

public class Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        ListView listView = (ListView) findViewById(R.id.list_view);
        String a[] = new String[]{"a", "b", "c", "d"};
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                android.R.id.text1 ,

                a));

//          ListView lv = getListView();

        listView.setTextFilterEnabled(true);

    }
}

you have set the list background

android:background="#FFFFFFFF"

By default the color of list text is white and you have given white background to list so the items does not appear

Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
0

You are setting the adapter to the current activity, but it's view isn't a ListView but a LinearLayout. Try this to set the adapter to the correct view.

String a[] = new String[]{"a", "b", "c", "d"};
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));
Jesse van Assen
  • 2,240
  • 2
  • 16
  • 19
  • 1
    When we are using `ListActivity`, we can use `setListAdapter(adapter);`, it automatically sets the adapter to `Listview` – Adil Soomro Apr 02 '12 at 11:20
  • Yes, I see now. According to the Google documentation [here](http://developer.android.com/reference/android/app/ListActivity.html), your answer should be correct. – Jesse van Assen Apr 02 '12 at 11:23
0

In your code Just comment the below line

setContentView(R.layout.main);

In this case use the below link for using custom adapter.

LINK

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • @Agarwal.. It will show him only a list... Most of the time you need something else too... – ngesh Apr 02 '12 at 11:38