一个活动到另一个Activity,使用Array List生成动态按钮

时间:2014-07-08 14:46:55

标签: android arraylist

我有一个带有HashMap的ArrayList作为Enumerator。现在ArrayList正在生成一个数据取决于我需要生成动态按钮。 Button的名称将出现在列表中。因此我使用KEY和Value获得了名称。但动态按钮不是创建。这是我的代码。 这是主要活动oncreate Class

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类,我根据Array List生成动态Button

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);

                }
            }
        }
    });
  }
}

该程序工作正常,但我无法生成按钮。输出的屏幕截图位于MainActivity SecondActivity AfterClickingButton

点击No Button正在生成。请帮助我。

0 个答案:

没有答案