Tuesday, March 1, 2011

android: having two listviews in two listactivities didn't work

I guess my previous question wasn't clear enough ( http://stackoverflow.com/questions/2549585/android-failed-to-setcontentview-when-switching-to-listactivity ), so I explain as follows.

In my app I have two listactivities which uses two different listviews:

public class Activity1 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    try{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview1); 
}
public class Activity2 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    try{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview2); 
}

}

As required by android, listview must have an ID which is exactly "@android:id/list". If I set the listview in both listview1 and listview2 with the same ID, then they will end up using the same format of listview, which is not what I want. But if I set one of the IDs to be sth like "@+id/listview2", android gave me the error: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

How do I handle this dilema?

btw: my listview is sort of complicated, customed list with image icons and text, so in my code, I also extended the ListAdapter:

this.mListAdapter = new myListAdapter(this, 
            R.layout.listview1, mTiebaInfo);
     setListAdapter(this.mListAdapter);
From stackoverflow
  • Don't use ListActivities. Use Activities and have a ListView in the each xml which you can name however you want.

    Yang : can you be more concreate? The reason I am using ListView is because my list is complicated, and I also extended ListAdapter.
    jqpubliq : Complexity should have nothing to do with it. You can extend `BaseAdapter` and override getView() as you were showed previously to return any kind of view you want. Then simply get a reference to the ListView you defined and set its adapter to your custom one by calling setAdapter(adapter) on it.
    Yang : I unserstand what you are saying. I've already extended my adapter based on ListAdapter. The thing is, If I dont extend ListActivity, I won't be able to call setListAdapter(). If I only extend Activity and setAdapter() as you mentioned, how can I make it a list?
    jqpubliq : If you read [this](http://developer.android.com/resources/tutorials/views/hello-listview.html) you'll see that setListApapter just adds a list to the view and then sets the lists adapter. So if you just add your own list set to fill_parent and call setAdapter() on it, it is equivalent.

0 comments:

Post a Comment