Pergunta

Eu quero a classe abaixo para visualizar alguns textviews/botões/spinners, e também um ListView que contém dados analisados.No entanto, o listview/adaptador/addview estão causando alguns problemas.Aqui o erro que eu estou ficando:

o java.lang.UnsupportedOperationException:addView(Ver, LayoutParams) não é suportado no AdapterView

Eu tenho um sentimento a sua para fazer com meus arquivos xml, mas não estou muito certo.Aqui está o meu código:

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);
    }       
}

Meu 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 é um simples textView.

EDITAR:

Ok, então eu atualizada alguns dos ficheiros e eu estou recebendo uma exceção de tempo de execução erro:

O seu conteúdo deve ter um ListView cujo atributo é 'android.R.id.lista'

aqui estão os arquivos atualizados:

classe:

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);
    }       
}

Meu statsscreenlayout stil tem todos os linearlayouts com os textviews, etc.e eu apaguei essa de que:

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

Em seguida, meus outros dois layouts são uma simples textview(xmldatarow), e um listview(xmllayout).Então, só para esclarecer, não há qualquer listview ou qualquer 'incluir' na minha statsscreenlayout.

Algum conselho?Obrigado

Foi útil?

Solução

Você não pode inflar vistas diretamente em um listview assim.Eu acho que você pode estar olhando para ListView's addHeaderView ou addFooterView métodos.Certifique-se de chamá-los antes de chamar setListAdapter.

EDITAR:Para escrevê-lo totalmente:Supondo que você queria statsviewlayout no topo de sua lista:Não coloque o LinearLayout diretamente no ListView elemento (ListViews não deve ter filhos).Coloque-a em um separado arquivo xml (por exemplo, statsview.xml) e fazer algo parecido com isso em sua onCreate:

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

Veja também este velho android-iniciantes post.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top