2017-05-25 1 views
0

J'essaie d'ajouter dynamiquement des textviews à une mise en page linéaire pour afficher une conversation de messagerie. Cependant, même après l'invalidation du groupe de visualisation, rien ne s'affiche à l'écran. Je ne suis pas capable de repérer l'erreur. Toute aide est appréciée.Problèmes avec la mise à jour dynamique d'une mise en page android avec des vues de texte

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(); 
      } 
      */ 
     } 
    }); 

} 

}

fichier Mise en page -

<?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" /> 

Répondre

0

Définissez les LayoutParams de votre textview comme ceci:

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

Et utilisez aussi "this" au lieu de "HTTPRequestHandler.getMyContext()".