使按键的新观点是针对Android的ArrayList [英] make new view of buttons from arraylist in android

查看:130
本文介绍了使按键的新观点是针对Android的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对象在我的Andr​​oid应用程序的ArrayList。我想用这个数组列表创建为ArrayList中的每个对象一个按钮,一个新的观点。每个按钮的文本将基于一个实例变量的对象时,它重新presents进行设置。新的视图应该能够反应以按钮presses为好。什么是实现这一目标的最佳方式是什么?感谢您的时间。

I have an arraylist of objects in my android app. I want to use this arraylist to create a new view with a button for each object in the arraylist. Each button's text would be set based on an instance variable in the object it represents. the new view should be able to react to button presses as well. What would be the best way to implement this? thank you for your time.

推荐答案

试试这个code会帮助你新的View附加到你的布局试试这个code澄清的理念,为您和随意喂我早在没有任何明显的事情。

Try this code will help you to append new View to your layout try this code clarify the idea for you and feel free to feed me back in any not obvious thing

布局code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/yourlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

和在的OnCreate 方法添加此code

and in your OnCreate method add this code

LinearLayout yourlayout= (LinearLayout) findViewById(R.id.yourlayout);

for (int i = 0; i < arraylist.size(); i++) {

                    Button btn = new Button (ActivityName.this);
                    btn.setWidth(40);
                    btn.setHeight(20);
                    btn.setText(arrylist.get(i).gettext());
                    yourlayout.addView(btn);
                }

这篇关于使按键的新观点是针对Android的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆