我正在尝试将空页脚(LinearLayout
)添加到我的ListView
并在以下情况下用我自己的视图填充此页脚:
private View footerButton;
private LinearLayout footer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rlist);
footer=new LinearLayout(this);
footer.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT));
footerButton=((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_button, null, false);
ListView list=(ListView) findViewById(R.id.list);
list.addFooterView(footerButton,null,false);
final UsersListAdapter adapter = new UsersListAdapter(this);
list.setAdapter(adapter);
//...
new Thread() {
@Override
public void run() {
//Loading...
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
footer.removeAllViews();
footer.addView(footerButton);
}
});
}
}.start();
}
但我在行“footer.addView(footerButton);”中得到例外:“
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
为什么呢?如您所知,我在添加任何视图之前调用removeAllViews()
。如何解决?
答案 0 :(得分:1)
这是因为您在ListView
对象中添加了 footerButton
查看此行..
list.addFooterView(footerButton,null,false);
所以,现在footerButton有一个父类,即列表..所以你在添加到LinearLayout
页脚时出错
尝试removeView()
列表...