Saturday, June 12, 2010

A simple ListView, extends ListActivity

It's the simplest form of ListView, extends ListActivity. The list is displayed as a single line of plain text, using the simple Android built-in layout "android.R.layout.simple_list_item_1".

A simple ListView, extends ListActivity

package com.exercise.AndroidList;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class AndroidList extends ListActivity {
 
 String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
   "Wednesday", "Thursday", "Friday", "Saturday"
 };
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter<String>(this, 
          android.R.layout.simple_list_item_1, DayOfWeek));
    }
}


Download the souce files.

next: ListView, with icon



6 comments:

Muritala Bakare said...

Hi Androic Eric,

Thanks for the tutorial, it was informative enough.
I followed the steps you take in creating the listview but when i opened emulator, it says "unfortunately, the app has stopped."
Am I doing something wrong? Please help. Thanks

Erik said...

Strong recommend NOT to test on Android Emulator, many weird exception will happen!

Muritala Bakare said...

Thanks Eric. What other ways to test my apps then. How would I know it is functioning well before i test it on my phone.

Muritala Bakare said...

Hi Eric, I have tried testing it on my phone but no luck.I installed the apk file on my android phone but gave same result as the emulator on eclipse.

Here is the code. Pls check if i have missed anything here. Thanks for your time.

package com.exercise.androidlist;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class AndroidList extends ListActivity {

String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, DayOfWeek));
}
}

Erik said...

hello Muritala Bakare,

for me, I will test on true device.

Erik said...

I tried to re-do it without problem. You can try it: http://adf.ly/dUdoQ