AdapterView 和 addView 的问题
我希望下面的类显示一些文本视图/按钮/旋转器,以及包含解析数据的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能像这样将视图直接膨胀到列表视图中。我想您可能正在寻找
ListView
的addHeaderView
或addFooterView
方法。确保在调用setListAdapter
之前调用它们。编辑:要完整地拼写出来:假设您希望将
statsviewlayout
放在列表顶部:不要将LinearLayout
直接放在ListView
中元素(ListViews
不应有子元素)。将其放入一个单独 xml 文件(例如,statsview.xml
)中,并在 onCreate 中执行类似的操作:另请参阅这个旧的 android 初学者帖子。
You can't inflate views directly into a listview like that. I think you may be looking for
ListView
'saddHeaderView
oraddFooterView
methods. Make sure to call them before callingsetListAdapter
.EDIT: To spell it out fully: Assuming you wanted
statsviewlayout
at the top of your list: Don't put theLinearLayout
directly in theListView
element (ListViews
shouldn't have children). Put it in a separate xml file (say,statsview.xml
) and do something like this in your onCreate:Also see this old android-beginners post.