[英]Android - ListActivity, add Header and Footer view
I'm using ListActivity, listview. 我正在使用ListActivity,listview。
listView = getListView();
just working perfectly. 完美地工作。 I added footer view as 我添加了页脚视图为
LayoutInflater inflater = getLayoutInflater();
listView.addFooterView( inflater.inflate( R.layout.footer, null ), null, false);
and everything was shiny but ugly, so i wanted to add this footer view (which contains only 1 edittext and only 1 button ) to header of listView as 而且一切都很闪亮但很丑陋,所以我想将此页脚视图(仅包含1个edittext和仅1个button)添加到listView的标头中,如下所示:
LayoutInflater inflater = getLayoutInflater();
listView.addHeaderView( inflater.inflate( R.layout.footer, null ), null, false);
and suddenly everything goes wrong, and i get RuntimeException immediately. 突然一切都出错了,我立即得到RuntimeException。
Suspended(exception RuntimeException)
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.access$2200(ActivityThread, Activity$ActiviyRecord, Intent),
so on..
Why is it throws exception ? 为什么会引发异常? What is different between addFooterView and addHeaderView, and how can i add Header to ListActivity ? addFooterView和addHeaderView有什么区别,如何将Header添加到ListActivity?
UPDATE UPDATE
So as you can read in comments, my logcat still doesn't work, but i just tried next at this moment: 因此,如您所见,我的logcat仍然无法正常工作,但此刻我只是尝试了一下:
} catch(Exception e){
Writer result = new StringWriter();
PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String error = result.toString();
}
and afterward i put breakpoint, and i can read error in expressions section. 然后我放断点,我可以在表达式部分读取错误。 it said : 它说 :
java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
it was instructive for all of us. 这对我们所有人都是有益的。 After change sort of commands, it works perferctly. 更改各种命令后,它可以正常工作。
As you log 当您登录时
java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called. java.lang.IllegalStateException:无法将标头视图添加到列表中-已经调用了setAdapter。
Listview's method addHeaderView or addFooterView must be called before setAdapter . ListView的方法addHeaderView或addFooterView必须setAdapter之前被调用。
Create layout xml for header and footer 为页眉和页脚创建布局xml
header_layout.xml header_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="header"
/>
</RelativeLayout>
footer_layout.xml footer_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="footer"
/>
</RelativeLayout>
now in activity java file onCreate() method add 现在处于活动状态的java文件onCreate()方法添加
listView = (ListView) findViewById(R.id.listView1);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView,
false);
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
false);
listView.addHeaderView(header, null, false);
listView.addFooterView(footer, null, false);
listView.setAdapter(adapter);
因此,如果要将标题视图添加到listView,则应在使用setListAdapter()之前严格执行此操作,否则会导致IllegalStateException。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.