Showing posts with label ListActivity. Show all posts
Showing posts with label ListActivity. Show all posts

setListAdapter - Bind ArrayAdapter to ListView

setListAdapter was added in API Level 1
Provides the cursor for the list view. (ListActivity.setListAdapter)

1. Link a String Array to an ArrayAdapter named myArrayAdapter

2. Bind myArrayAdapter to the ListView by adding this line of code to the onCreate method.

setListAdapter(myArrayAdapter);

3. Compile and run!

Resources:
http://developer.android.com/reference/android/app/ListActivity.html#setListAdapter(android.widget.ListAdapter)


Add a ListView using ListActivity

How to Add a ListView using ListActivity

1. Add a ListView to XML file.

2. Set the id to android:id="@android:id/list" in the ListView.

3. Set android:drawSelectorOnTop = "fasle" in the ListView.

4. Now open the MainActivity.java file and add the below import to the imports section.

import android.widget.ListView;
import android.app.ListActivity;

5. You will need to extend the ListActivity in your MainActivity.java file. To do this
change this:

public class MainActivity extends Activity{

to this:

public class MainActivity extends ListActivity{

6. Add the line: EditText editText between the public class and the onCreate method.
Defining the editText variable here will make this available throughout the whole class.

public class MainActivity extends Activity {

EditText editText;

    public void onCreate(Bundle savedInstanceState) {

7. Add the bolded code below in the OnCreate method after setContentView and before the end of the method }. This code will link your editText in your main.xml file to the editText in code.

          editText = (EditText) findViewById(R.id.editText);

8. Run your app to test it. It should display an EditText.

9. Set the default text for the EditText by adding to the main.xml file EditText section, anywhere between the <EditText and the /> tags.

<EditText
...
    android:text="your text here"
...
/>

10. Compile and run!

Solution for: Your content must have a ListView whose id attribute is 'android.R.id.list'


Problem: 

W/dalvikvm(17155): threadid=1: thread exiting with uncaught exception (group=0x40a351f8) E/AndroidRuntime(17155): FATAL EXCEPTION: main
E/AndroidRuntime(17155): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akp.ToDoList/com.akp.ToDoList.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Solution:
1. Make sure the android:id line is as displayed below.

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView>


2. Test project. 

Solution: error: cannot find symbol, "ListActivity"


Problem:
    [javac] Compiling 3 source files to C:\xxx\Workspace\MyApp\bin\classes
    [javac] C:\xxx\Workspace\MyApp\src\com\akp\MyApp\MainActivity.java:12: error: cannot find symbol
    [javac] public class MainActivity extends ListActivity{

Quick Solution: 

1. Open MainActivity.java and add the below import for the ListActivity

import android.app.ListActivity;

2. Compile and test app.