【问题标题】:Adding RelativeLayout with LayoutInflater and set Text to TextView使用 LayoutInflater 添加 RelativeLayout 并将 Text 设置为 TextView
【发布时间】:2015-08-22 04:08:25
【问题描述】:

我想根据 json 数组长度添加一个 RelativeLayout XML 文件。

for (int i = 0; i < jsonArray.length(); i++) {
                        Log.i("Length", "Is " + jsonArray.length());

                        JSONObject customer = jsonArray.getJSONObject(i);
                        Log.e("Customer", "is " + jsonArray.getJSONObject(i));
                        RelativeLayout rl[] = new RelativeLayout[jsonArray.length()];
                        rl[i] = new RelativeLayout(ChatActivity.this);
                        if (sharedPrefsUser.getInt(SharedPrefsInformation.ID_FROM_LOGGED_IN_USER, -1) != sender) {
                            rl[i] = (RelativeLayout) getLayoutInflater().inflate(R.layout.message_left, null);
                        } else {
                            rl[i] = (RelativeLayout) getLayoutInflater().inflate(R.layout.message_right, null);
                        }
                        TextView textViewMessage = (TextView) rl[i].findViewById(R.id.txtMessage);
                        textViewMessage.setText(customer.getString("message"));sharedPrefsUser.getInt(SharedPrefsInformation.ID_FROM_LOGGED_IN_USER, -1));
//                    Log.i("")

                        Log.e("Message", "is " + customer.getString("message"));
                        linearLayoutWithMessages.addView(rl[i]);
                    }

但现在只会添加一次... json 数组长度为四。有什么建议么?谢谢..

【问题讨论】:

    标签: android json android-layout


    【解决方案1】:

    您可以尝试在 for 循环之外创建 RelativeLayout 数组 (rl)。

     RelativeLayout rl[] = new RelativeLayout[jsonArray.length()];
    for (int i = 0; i < jsonArray.length(); i++) {
    }
    

    【讨论】:

    • 很遗憾没有:-(
    • 它没有给出预期的结果还是不能在for循环之外创建RelativeLayout数组?现在您每次都创建一个新数组并向其中添加一项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多