42

I'm new to Android, and I really need to do it this way (I've considered doing it in another Activity), but can anyone show me a simple code (just the onCreate() method) that can do Listview without ListActivity?

THanks

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

5 Answers5

55

If you have an xml layout for the activity including a listView like this

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

<ListView android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_weight="fill_parent"

Then in your onCreate you could have something like this

setContentView(R.layout.the_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myList);
ListView lv = (ListView)findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
     @Override
     public void onItemClick(AdapterView<?> a, View v,int position, long id) 
     {
          Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
      }
});
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
Patrick Kafka
  • 9,795
  • 3
  • 29
  • 44
28

Include the following resource in your res/layout/main.xml file:

<ListView
  android:id="@+id/id_list_view"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

your_class.java

import android.widget.ListView;
import android.widget.ArrayAdapter;

public class your_class extends Activity
{
  private ListView m_listview;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    m_listview = (ListView) findViewById(R.id.id_list_view);

    String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
    ArrayAdapter<String> adapter =
      new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

    m_listview.setAdapter(adapter);
  }
}
John Leimon
  • 1,063
  • 1
  • 9
  • 13
11

The following creates a simple ListView programmatically:

public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         String[] myList = new String[] {"Hello","World","Foo","Bar"};              
         ListView lv = new ListView(this);
         lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
         setContentView(lv);
}
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • Hi Cchenesson...when I follow this code, I get a "Edit Source" in my Eclipse (which means it doesn't work.) Why is that? – TIMEX Feb 11 '10 at 23:04
  • Weird - I created a new android prject, copy/paste the above, run it without any problems. Can you post the whole code for the activity? Where do you see that "Edit Source" in Eclipse? – ccheneson Feb 11 '10 at 23:19
  • Is it this problem you have? http://android.opensourceror.org/2010/01/18/android-source/ – ccheneson Feb 11 '10 at 23:23
  • I think its for ListActivity and not for Activity – Manoj Kumar Aug 28 '12 at 10:19
1

You could also reference your layout, instantiate a layout object from your code, and then build the ListView in Java. This gives you some flexability in terms of setting dynamic height and width at runtime.

mobilekid
  • 1,629
  • 5
  • 19
  • 27
1

include the following resource file in your res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

      <ListView
         android:id="@+id/listView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
      </ListView>
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {
ListView listView;
String[] listPlanet={"mercury","Venus","Mars","Saturn","Neptune"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

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

  listview.setAdapter(adapter);

}

}
Rakesh Rangani
  • 1,039
  • 10
  • 13