向Android listview中添加button
2010-06-04 14:15:00 来源:WEB开发网public Context context;
public List< ImageItem > items;
public textAdapter(Context ctx) {
context = ctx;
items = new ArrayList< ImageItem >();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TestItemView iiv;
if (convertView==null)
{
iiv = new TestItemView(context, items.get(position).text,items.get(position).btnString);
}
else
{
iiv = (TestItemView)convertView;
iiv.setTitle(items.get(position).text);
iiv.setButton(items.get(position).btnString);
}
return iiv;
}
3.创建一个类,也算是一个结构体,为了传参方便
public class ImageItem {
public String text;
public String btnString;
public ImageItem(String text,String btnString) {
super();
this.text = text;
this.btnString = btnString;
}
}
4.将你的view显示在listview中
public class testList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textAdapter adapter = new textAdapter(this);
adapter.items.add(new ImageItem("你好","开始"));
adapter.items.add(new ImageItem("你好","开始"));
更多精彩
赞助商链接