Android ListView error



下面是我复制和编辑的一些代码。如果我让ColorsActivity扩展活动,一切都会很好。但如果它扩展了ListActivity,就会给我一个错误,"你的内容必须有一个ID属性为android.R.ID.List的listview"。

有人能给我解释一下吗?

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>

使用此

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

使用此更改您的列表视图id

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

如果你的课程扩展了"活动",那么就把它改为

public class className extends ListActivity

您可以通过调用方法来获得ListView

ListView listView = getListView();

指示

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

如果你想扩展List Activity,那么你的xml文件必须包含一个id为"@android:id/List"的ListView对象有关更多详细信息,请参阅

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

最新更新