2016-04-08 14 views
0

Ich versuche, ein bestimmtes Layout relativ XML programmgesteuert hinzuzufügen. Es besteht aus einem ImageView, EditText und einem TextView. Das Problem scheint zu sein, dass ich versuche, es mit dieser "onActivityResult" -Methode zu tun, nachdem ich von einer anderen Aktivität zurückgekehrt bin. Wenn ich es irgendwo anders mache, funktioniert es und das Layout, das ich erstellen möchte, erscheint korrekt (zum Beispiel habe ich erfolgreich "onCreate" und per Knopfdruck versucht), aber wenn "onActivityResult" nicht funktioniert. Ich brauche es, um da zu sein.Relative Layout hinzugefügt programmgesteuert wird nicht angezeigt

Die Ausdrucke werden in beiden Fällen erfolgreich ausgegeben, es liegt kein Fehler oder Absturz vor, das Layout wird jedoch nicht angezeigt. Irgendwelche Vorschläge werden geschätzt.

Dies ist die XML-Datei, die ich erstellen möchten:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="260dip"> 

    <ImageView 
     android:id="@+id/damagePicture" 
     android:layout_width="match_parent" 
     android:layout_height="210dip" 
     android:scaleType="centerCrop" 
     android:layout_marginTop="10dip" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:src="@drawable/nocar"/> 

    <EditText 
     android:id="@+id/orderTextfield" 
     android:layout_height="40dip" 
     android:layout_width="match_parent" 
     android:gravity="left|center" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="45dip" 
     android:hint="Enter damage description" 
     android:textSize="14sp" 
     android:layout_alignParentLeft="true" 
     android:textColor="@color/black" 
     android:textColorHint="@color/gray_vin_search_text" 
     android:background="@null" 
     android:singleLine="true" 
     android:editable="true" 
     android:layout_below="@+id/damagePicture"/> 

    <TextView 
     android:id="@+id/removePicture" 
     android:layout_width="40dip" 
     android:layout_height="40dip" 
     android:text="@string/discardPicture" 
     android:gravity="center|center" 
     android:textColor="@color/black" 
     android:textSize="24sp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" 
     android:clickable="true" 
     android:onClick="removePicture"/> 

    <View 
     android:id="@+id/bottomLine" 
     android:layout_width="match_parent" 
     android:layout_height="1dip" 
     android:layout_alignParentBottom="true" 
     android:background="@color/gray_for_line"></View> 

</RelativeLayout> 

Ein hier sind die relevanten Teile des Codes:

public void addResultingDamagePicture(String pathToFile) { 
     System.out.println("ADD RESULTING DAMAGE PICTURE"); 
     LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE); 
     View damagePictureSet = layoutInflater.from(this).inflate(R.layout.damage_picture_set, picturesCell, false); 
     ((TextView) damagePictureSet.findViewById(R.id.removePicture)).setTypeface(fontAwesome); 
     TextView remove = (TextView)damagePictureSet.findViewById(R.id.removePicture); 
     remove.setTypeface(fontAwesome); 
     File imgFile = new File(pathToFile); 
     if(imgFile.exists()) { 
      System.out.println("ADD RESULTING DAMAGE PICTURE - FILE OK"); 
      BitmapFactory.Options opt = new BitmapFactory.Options(); 
      opt.inSampleSize = 4; 
      final Bitmap thebmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), opt); 
      ((ImageView) damagePictureSet.findViewById(R.id.damagePicture)).setImageBitmap(thebmp); 
      picturesCell.addView(damagePictureSet); 
     } else { 
      System.out.println("ADD RESULTING DAMAGE PICTURE - NO FILE EXISTS!!!"); 
     } 
     return; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == SHOOT_DAMAGE_PICTURE && resultCode == RESULT_OK) { 
      final String encodedPicture = data.getStringExtra("PATH_TO_PICTURE"); 
      System.out.println("PICTURE RESULT = " + encodedPicture); 
      addResultingDamagePicture(encodedPicture); 
     } else { 
      System.out.println("ON ACTIVITY RESULT = NOT OK"); 
      return; 
     } 
    } 
+0

Auf den ersten Blick fehlt es Ihnen, um Ihre Sicht aufzufrischen, vgl. http://stackoverflow.com/questions/5991968/how-to-force-an-tire-layout-view-refresh – TAM

+0

Sie können Hierarchy Viewer versuchen, um zu sehen, wo die Ansicht ging: https://developer.android.com/ tools/help/hierarchy-viewer.html –

+0

Hat 'View damagePictureSet = layoutInflater.from (this) .inflate (R.layout.damage_picture_set, imagesCell, true);' statt 'DamagePictureSet = layoutInflater.from (this) .inflate anzeigen (R.layout.damage_picture_set, BilderCell, false); 'Hilfe? – jaibatrik

Antwort

1

In einigen Szenarien die onCreate Methode kann nach onActivityResult aufgerufen Methode. In diesem Fall wird die Methode setContentView erneut aufgerufen, wodurch das ursprüngliche Layout erneut gerendert wird. Vielleicht möchten Sie einige Flags setzen oder Ihre Daten unabhängig von Ihrer Activity aktualisieren, so dass Sie die View je nach Flag/Daten neu erstellen können.

+0

Das war das Problem, danke. Ich habe die relevanten Daten unabhängig von der Aktivität gemacht, dann hat es funktioniert. –

Verwandte Themen