使用LayoutInflater添加RelativeLayout并将Text设置为TextView

时间:2015-08-21 20:04:01

标签: android json android-layout

我想根据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的长度是四。有什么建议?感谢..

1 个答案:

答案 0 :(得分:0)

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

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