Android 一个活动到另一个活动,使用数组列表动态生成按钮
我得到了一个以HashMap作为枚举器的ArrayList。现在ArrayList正在生成数据,这取决于我需要生成动态按钮。按钮的名称将出现在列表中。因此,我使用KEY和Value得到了这个名称。但动态按钮并没有创建。这是我的密码。 这是创建类的主要活动Android 一个活动到另一个活动,使用数组列表动态生成按钮,android,arraylist,Android,Arraylist,我得到了一个以HashMap作为枚举器的ArrayList。现在ArrayList正在生成数据,这取决于我需要生成动态按钮。按钮的名称将出现在列表中。因此,我使用KEY和Value得到了这个名称。但动态按钮并没有创建。这是我的密码。 这是创建类的主要活动 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.enteraip);
ed = et.getText();
bt = (Button) findViewById(R.id.sub);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// when button is clicked the process will start
free = ed.toString();
url = "some url";
Log.i(MESSAGE, "we got this string" + free);
Log.i("The button got", url);
Intent in = new Intent(getApplicationContext(),ViewLight.class);
in.putExtra(DATA,url);
startActivity(in);
}
});
}
这是我的viewLight类,我在其中基于数组列表生成动态按钮
public class ViewLight extends Activity {
public static final String msg = "ViewLightMain";
static final String KEY_LIGHTING = "Lighting";
static final String KEY_LID = "Light_Id";
static final String KEY_BID = "Box_Id";
static final String KEY_DID = "Device_Id";
static final String KEY_LN = "Light_Name";
static final String KEY_CT = "Control_Type";
static final String KEY_RM = "Room";
static final String KEY_STAT = "Status";
static final String KEY_CL = "Current_Level";
Button b;
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_light);
Intent in = getIntent();
url = in.getStringExtra(MainActivity.DATA);
Log.i(msg, url);
b = (Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout l = (LinearLayout)findViewById(R.id.main);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
XmlParser x = new XmlParser();
String xml = x.Getaip(url);
ArrayList<HashMap<String,String>> a = new ArrayList<HashMap<String,String>>();
Document doc = x.getDom(xml);
NodeList nl = doc.getElementsByTagName(KEY_LIGHTING);
for (int i = 0; i < nl.getLength(); i++) {
HashMap< String, String> h = new HashMap<String, String>();
Element e = (Element)nl.item(i);
h.put(KEY_LID,x.getValue(e,KEY_LID ));
h.put(KEY_BID, x.getValue(e, KEY_BID));
h.put(KEY_DID, x.getValue(e, KEY_DID));
h.put(KEY_LN, x.getValue(e, KEY_LN));
h.put(KEY_CT,x.getValue(e, KEY_CT));
h.put(KEY_RM, x.getValue(e, KEY_RM));
h.put(KEY_STAT, x.getValue(e, KEY_STAT));
h.put(KEY_CL, x.getValue(e, KEY_CL));
a.add(h);
}
Log.i(msg, "The arrayList is"+a.get(1));
for (HashMap<String, String> arr : a) {
for(Entry<String, String> ent: arr.entrySet()){
String key = ent.getKey();
String value = ent.getValue();
if(key.equals("Light_Name")){
Button b1 = new Button(ViewLight.this);
b1.setText(value);
l.addView(b1,lp);
Log.i("In FOr Loop", value);
}
Log.i(msg, "The key is"+key+"The value is"+value);
}
}
}
});
}
}
public类ViewLight扩展活动{
公共静态最终字符串msg=“ViewLightMain”;
静态最终字符串键\u LIGHTING=“LIGHTING”;
静态最终字符串键\u LID=“Light\u Id”;
静态最终字符串键\u BID=“Box\u Id”;
静态最终字符串键\u DID=“设备\u Id”;
静态最终字符串键\u LN=“Light\u Name”;
静态最终字符串键\u CT=“控制类型”;
静态最终字符串键\u RM=“房间”;
静态最终字符串键\u STAT=“Status”;
静态最终字符串键\u CL=“当前级别”;
按钮b;
字符串url;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u view\u灯);
Intent in=getIntent();
url=in.getStringExtra(MainActivity.DATA);
Log.i(msg,url);
b=(按钮)findViewById(R.id.Button);
b、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
LinearLayout l=(LinearLayout)findViewById(R.id.main);
LayoutParams lp=新的LayoutParams(LayoutParams.MATCH\u父级,LayoutParams.WRAP\u内容);
XmlParser x=新的XmlParser();
字符串xml=x.Getaip(url);
ArrayList a=新的ArrayList();
Document doc=x.getDom(xml);
NodeList nl=doc.getElementsByTagName(按键照明);
对于(int i=0;ih=newhashmap();
元素e=(元素)nl.第(i)项;
h、 put(KEY_LID,x.getValue(e,KEY_LID));
h、 put(KEY_BID,x.getValue(e,KEY_BID));
h、 put(KEY_DID,x.getValue(e,KEY_DID));
h、 put(KEY_LN,x.getValue(e,KEY_LN));
h、 put(KEY_-CT,x.getValue(e,KEY_-CT));
h、 put(KEY_RM,x.getValue(e,KEY_RM));
h、 put(KEY_STAT,x.getValue(e,KEY_STAT));
h、 put(KEY_CL,x.getValue(e,KEY_CL));
a、 加(h);
}
Log.i(msg,“arrayList是”+a.get(1));
for(HashMap arr:a){
for(Entry-ent:arr.entrySet()){
String key=ent.getKey();
字符串值=ent.getValue();
if(key.equals(“Light_Name”)){
按钮b1=新按钮(ViewLight.this);
b1.setText(值);
l、 addView(b1,lp);
Log.i(“FOr循环中”,值);
}
Log.i(消息,“键为“+key+”,值为“+value”);
}
}
}
});
}
}
程序运行良好,但我无法生成按钮。输出的屏幕截图在这里
单击后,没有按钮生成。请在这方面帮助我。您是否检查了日志或设置了断点,以查看您的列表是否按预期填充?@codeMagic我完全可以得到我想要的列表。我唯一的问题是按钮没有生成。剩下的逻辑正在运行fine@JDJ即使在定义了上面定义的setLayoutParams之后,我也得到了输出。我知道没有生成您的按钮,但我问了,因为如果列表为空,那么将永远不会进入循环。总之,您的
主线性布局是什么方向的?还有其他的视图吗?我的方向是垂直的。循环已生成正确的输出。循环中的print语句给出了我想要的输出。