[英]Repeat view in a single activity
我有一个显示按钮,文本和列表的活动。 我想根据数据(SQLite)重复该模式。 如果我有5个唯一的类别,请显示5个按钮,5个文本和5个列表。 我怎么做 ? 如何重复图案?
在android中,您不能以相同的活动“重复视图”(一个孩子不能有超过1个父母),但是您总是可以解决。 您可以将方法编写为getview(),以返回包含按钮,文本等的父视图,并将其添加到顶层视图组中;
while(somelogic)
urparentview.addview(getview());
setcontentview(urparentview)
您的getviewmethod可能是这样的
view getview()
{
button b = new button(this);
edittext ed = new edittext(this);
//set orientations etc...
LinearLayout ll = new LineaLayout(this);
ll.addview(b);
ll.addView(ed);
return ll;
}
您可以将新的按钮文本和列表添加到同一LinearLayout中。 所以..
addStuffz(){
LinearLayout main = (LinearLayout)findViewById(R.id.main_linearLayout);
LinearLayout newLayout = new LinearLayout(context);
/**
* Do Init here
*/
Button anotherButton = new Button(context);
/**
* Do Init here
*/
TextView anotherText = new TextView(context);
/**
* Do Init here
*/
ListView anotherList = new ListView(context);
/**
* Do Init here
*/
newLayout.addView(anotherButton);
newLayout.addView(anotherText);
newLayout.addView(anotherList);
mainLayout.addView(newLayout);
}
标准设计模式是使用ListActivity(或带有listView的标准活动)并创建CursorAdapter来创建视图,并从记录集游标到视图的数据绑定值。
然后在ListActvity上调用setAdapter()。
视频
有关UI改进的Google IO 2009视频中讨论了列表适配器的使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.