0
我想動態添加textviews的線性佈局,以顯示消息對話的是Android線性佈局。 但是,即使視圖組無效後,屏幕上也不顯示任何內容。 我無法發現錯誤。 任何幫助表示讚賞。問題與更新與文本視圖動態
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" />