Spectrem
Spectrem

Reputation: 712

Android ListView error

Below is some code that I copied and edited. If I make the ColorsActivity extend activity, everything works fine. But if it extends ListActivity, it gives me the error, "Your content must have a listview who's ID attribute is android.R.Id.List".

Can someone please explain this to me?

public class ColorsActivity extends Activity {
/** Called when the activity is first created. */
//ListView that will hold our items references back to main.xml
ListView lstTest;
//Array Adapter that will hold our ArrayList and display the items on the ListView
SeekBarAdaptor seekBarAdaptor;

//List that will  host our items and allow us to modify that array adapter
ArrayList<SeekBar> seekBarArrayList=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.seekbarlist);
    //Initialize ListView
    lstTest= (ListView)findViewById(R.id.myList);

     //Initialize our ArrayList
    seekBarArrayList = new ArrayList<SeekBar>();
    //Initialize our array adapter notice how it references the listitems.xml layout
    seekBarAdaptor = new SeekBarAdaptor(ColorsActivity.this, R.layout.seekbars,seekBarArrayList);
    SeekBar red = new SeekBar(ColorsActivity.this);
    SeekBar blue = new SeekBar(ColorsActivity.this);
    //Set the above adapter as the adapter of choice for our list
    lstTest.setAdapter(seekBarAdaptor);
    seekBarArrayList.add(red);
    seekBarArrayList.add(blue);
    //Instantiate the Web Service Class with he URL of the web service not that you must pass
    //WebService webService = new WebService("http://www.sumasoftware.com/alerts/GetAlerts.php");

    //Pass the parameters if needed , if not then pass dummy one as follows
    Map<String, String> params = new HashMap<String, String>();
    params.put("var", "");

    //Get JSON response from server the "" are where the method name would normally go if needed example
    // webService.webGet("getMoreAllerts", params);
    //String response = webService.webGet("", params);

    /*try
    {
        //Parse Response into our object
        Type collectionType = new TypeToken<ArrayList<alerts>>(){}.getType();

        //JSON expects an list so can't use our ArrayList from the lstart
        List<alerts> lst= new Gson().fromJson(response, collectionType);

        //Now that we have that list lets add it to the ArrayList which will hold our items.
        for(alerts l : lst)
        {
            alrts.add(l);
        }

        //Since we've modified the arrayList we now need to notify the adapter that
        //its data has changed so that it updates the UI
        arrayAdapter.notifyDataSetChanged();
    }
    catch(Exception e)
    {
        Log.d("Error: ", e.getMessage());
    }*/
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>

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

</LinearLayout>

Upvotes: 1

Views: 2710

Answers (3)

almuneef
almuneef

Reputation: 126

If you want to extend List Activity, then your xml file MUST contain a ListView object with the id "@android:id/list" for more details refer

http://developer.android.com/reference/android/app/ListActivity.html

Upvotes: 1

Deepak
Deepak

Reputation: 2009

Change your listview id with this

android:id="@android:id/list"

and if your class extends Activity then change it to

public class className extends ListActivity

and you can get your ListView by calling the method

ListView listView = getListView();

insted of

lstTest= (ListView)findViewById(R.id.myList);

Upvotes: 1

user1203673
user1203673

Reputation: 1015

use this

 <ListView
        android:id="@android:id/list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

Upvotes: 3

Related Questions