Trouble with AdapterView and addView

11.6k views Asked by At

I want the class below to display some textviews/buttons/spinners, and also a ListView containing parsed data. However the listview/adapter/addview are causing some trouble. Heres the error I'm getting:

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

I've a feeling its to do with my xml files, but Im not too sure. Here's my code:

public class StatisticsScreen extends ListActivity{

private List<Message> messages; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statisticsscreen);

    loadFeed();

    //other textviews and listeners added
}

private void loadFeed() {

    try{
        BaseFeedParser parser = new BaseFeedParser();
        messages = parser.parse();
        List<String> titles = new ArrayList<String>(messages.size());
        for (Message msg : messages){
            titles.add(msg.getTitle());
        }
        ArrayAdapter<String> adapter = 
        new ArrayAdapter<String>(this, R.layout.xmldatarow,titles);

        this.setListAdapter(adapter);           
    } catch (Throwable t){
        Log.e("AndroidNews",t.getMessage(),t);
    }       
}

My statisticsscreen xml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">

<LinearLayout android:id="@+id/statsviewlayout"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/black">
    //other layouts/textviews/buttons added
    <include  layout="@layout/xmllayout" android:id="@+id/xmllist" />

</LinearLayout>
</ListView>

xmllayout xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ListView android:id="@+id/xmllist" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>
    </LinearLayout>

xmldatarow is a simple textView.

EDIT:

Ok so ive updated some of the files and I'm getting a runtime exception error:

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

here are the updated files:

class:

setContentView(R.layout.statisticsscreen);    
loadFeed();        
getListView().addHeaderView(View.inflate(this, R.layout.xmllayout, null));

private void loadFeed() {
    try{
        BaseFeedParser parser = new BaseFeedParser();
        messages = parser.parse();
        List<String> titles = new ArrayList<String>(messages.size());
        for (Message msg : messages){
            titles.add(msg.getDate());
        }
        ArrayAdapter<String> adapter = 
            new ArrayAdapter<String>(this, R.layout.xmldatarow,titles);
        this.setListAdapter(adapter);
    } catch (Throwable t){
        Log.e("AndroidNews",t.getMessage(),t);
    }       
}

My statsscreenlayout stil has all of the linearlayouts with textviews etc. and I deleted this from it:

<include  layout="@layout/xmllayout" android:id="@+id/xmllist" />

Then my other two layouts are a simple textview(xmldatarow), and a listview(xmllayout). So just to clarify, there isnt any listview or any 'include' in my statsscreenlayout.

Any advice? Thanks

1

There are 1 answers

1
Yoni Samlan On BEST ANSWER

You can't inflate views directly into a listview like that. I think you may be looking for ListView's addHeaderView or addFooterView methods. Make sure to call them before calling setListAdapter.

EDIT: To spell it out fully: Assuming you wanted statsviewlayout at the top of your list: Don't put the LinearLayout directly in the ListView element (ListViews shouldn't have children). Put it in a separate xml file (say, statsview.xml) and do something like this in your onCreate:

getListView().addHeaderView(View.inflate(this, R.layout.statsview.xml, null));

Also see this old android-beginners post.