AdapterView 和 addView 的问题

发布于 2024-10-17 20:08:14 字数 3322 浏览 1 评论 0原文

我希望下面的类显示一些文本视图/按钮/旋转器,以及包含解析数据的 ListView。然而 listview/adapter/addview 造成了一些麻烦。 这是我收到的错误:

java.lang.UnsupportedOperationException:AdapterView 不支持 addView(View, LayoutParams)

我感觉它与我的 xml 文件有关,但我不太确定。 这是我的代码:

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

我的统计屏幕 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是一个简单的textView。

编辑:

好的,我更新了一些文件,并且收到运行时异常错误:

您的内容必须有一个属性为“android.R.id.list”的ListView

这里是更新的文件:

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

我的 statsscreenlayout stil 具有所有带有文本视图等的线性布局,我从中删除了它it:

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

那么我的另外两个布局是一个简单的textview(xmldatarow)和一个listview(xmllayout)。 所以为了澄清一下,我的统计屏幕布局中没有任何列表视图或任何“包含”。

有什么建议吗?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-10-24 20:08:14

您不能像这样将视图直接膨胀到列表视图中。我想您可能正在寻找 ListViewaddHeaderViewaddFooterView 方法。确保在调用 setListAdapter 之前调用它们。

编辑:要完整地拼写出来:假设您希望将 statsviewlayout 放在列表顶部:不要将 LinearLayout 直接放在 ListView 中元素(ListViews 不应有子元素)。将其放入一个单独 xml 文件(例如,statsview.xml)中,并在 onCreate 中执行类似的操作:

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

另请参阅这个旧的 android 初学者帖子

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文