动态更新带有文本视图的android线性布局的问题

时间:2017-05-25 19:13:01

标签: android android-linearlayout

我试图动态地将文本视图添加到线性布局以显示消息传递对话。 但是,即使在使视图组无效之后,屏幕上也不会显示任何内容。 我无法发现错误。 任何帮助表示赞赏。

MesssageBodyFile.java

public class MessageBodyFragment extends AppCompatActivity {

LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_messagebody);
    Intent intent = getIntent();
    String screenname_to = intent.getStringExtra("to");
    fetchConversation(screenname_to);
}


public void update()
{
    Log.d("pavan3", "back here");

    ViewGroup vg = (ViewGroup) findViewById (R.id.mainLayout);
    vg.invalidate();
    Log.d("pavan3", "invalidated");
}

private void fetchConversation(String screenname_to) {

    Log.d("pavan3", "inside fetchconversation "+screenname_to);
    final HTTPRequestHandler httpRequestHandler = HTTPRequestHandler.getInstance();
    SessionManager sessionManager = new SessionManager(HTTPRequestHandler.getMyContext());

    JSONObject resultObject = new JSONObject();
    JSONObject dataObject = new JSONObject();
    try {
        dataObject.put("screenName", sessionManager.getScreenname());
        dataObject.put("toScreenName", screenname_to);
        resultObject.put("data", dataObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    httpRequestHandler.sendHTTPRequest("/getIndividualMessage", resultObject, new HTTPRequestHandler.VolleyCallback(){
        @Override
        public void onSuccess(JSONObject result) throws JSONException {
            Log.d("pavan3", "result here "+result.toString());
            linearLayout = (LinearLayout) findViewById(R.id.layout1);
            LinearLayout main = (LinearLayout) findViewById(R.id.mainLayout);
            JSONArray jsonArray = result.getJSONArray("result");

            TextView mytv = new TextView(HTTPRequestHandler.getMyContext());
            mytv.setText("yofdksjfhdskj");
            mytv.setTextSize(35);
            linearLayout.addView(mytv);
            update();

            /*
            for(int i = 0 ; i < jsonArray.length() ; i++)
            {
                Log.d("pavan3", "inside");
                JSONObject resultJSON = jsonArray.getJSONObject(i);
                Log.d("pavan3", "resultJSON "+resultJSON);
                JSONObject messageJSON = resultJSON.getJSONObject("message");
                Log.d("pavan3", "messageJSON "+messageJSON);
                String subject = messageJSON.getString("subject");
                String message = messageJSON.getString("message");
                Log.d("pavan3", "message "+message);

                TextView mytv = new TextView(HTTPRequestHandler.getMyContext());
                mytv.setText(message);
                mytv.setTextSize(35);
                linearLayout.addView(mytv);

                update();
            }
            */
        }
    });

}

}

布局文件 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:context=".MessageBodyFragment">

<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>

<!--
<ScrollView
    android:layout_width="match_parent"
    android:layout_weight="20"
    android:layout_height="wrap_content">
</ScrollView>
-->

<include
    layout="@layout/type_message_area"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="bottom" />

1 个答案:

答案 0 :(得分:0)

设置textview的LayoutParams,如下所示:

mytv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

并且还使用“this”而不是“HTTPRequestHandler.getMyContext()”。