动态添加.xml视图

时间:2018-09-23 20:26:01

标签: android android-studio

我想将在单独的.xml文件(card.xml)中创建的视图动态添加到现有的LinearLayoutMainLinLay)中。 .xml目前包含两个TextViewsonCreate方法完成后,将进行添加。我只知道在收到第一条MQTT消息后将添加多少个视图。

我已经搜索了一个小时的解决方案/答案,但是找不到任何东西。

收到MQTT消息后,我将创建一个类的对象:

@Override
            public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
                if(connected && !busy){
                    busy = true;
                    if (startup){
                        if (mqttMessage.toString().contains("\"Online\":true")){
                            Log.w("Debug", "Message Online");
                            busy = false;
                            return;
                        }
                        else{
                            startup = false;
                            Log.w("Debug","createCards");
                            creatCards(mqttMessage.toString());
                            busy = false;
                        }

                    }
                    else{
                        Log.w("Debug","update");
                        busy = false;
                        //update(mqttMessage.toString());
                    }
                }

            }

public void creatCards(String message){

    JSONObject jObject = null;
    try {
        jObject = new JSONObject(message);
    } catch (JSONException e) {
        e.printStackTrace();
        Log.w("JSON_err", e);
    }
    String nTemp="";
    try {
        JSONObject jobjValues = jObject.getJSONObject("Values");
        JSONObject jobjMeta = jObject.getJSONObject("MetaData");
        Iterator<String> names = jobjValues.keys();
        Log.w("Debug",message);
        int i=0;

        while(names.hasNext()){
            String name = names.next();
            Log.w("Debug",name);
            //Log.w("Context",this.getApplicationContext().toString());
            Card card = new Card(getApplication().getApplicationContext(), name);
            cards.add(card);


        }

    } catch (JSONException e) {
        e.printStackTrace();
        Log.w("JSON_err", e);
    }


}

然后在课堂上:

package com.henne.xxyy.homeautomation;


import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Card  extends LinearLayout{

    View rootView;
    View View1;
    TextView name;
    TextView value;
    LinearLayout mainLinLay;


    public Card(Context context, String message) {
        super(context);
        init(context, message);
    }

    public Card(Context context, AttributeSet attrs, String message) {
        super(context, attrs);
        init(context, message);
    }

    private void init(Context context, String message) {
        try{

            rootView = inflate(context, R.layout.card, this);
            View1 = inflate(context, R.layout.activity_home, null);
            name = (TextView) rootView.findViewById(R.id.Name);
            value = (TextView) rootView.findViewById(R.id.Value);




            name.setText("Name1");
            value.setText("234");

            mainLinLay = (LinearLayout) View1.findViewById(R.id.mainLinLay);

            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            mainLinLay.setLayoutParams(params);

            mainLinLay.addView(rootView);

            rootView.invalidate();
            mainLinLay.invalidate();
            View1.invalidate();
        }
        catch (Exception e){
            Log.w("class_ex",e);
        }
    }

    private void update(String message) {

    }


}

没有引发错误或异常。加上TextViews的两个只是不显示。

我想念什么?我希望我分享了足够的信息。

谢谢!

0 个答案:

没有答案
相关问题