2016-04-09 18 views
0

Also in meinem Code habe ich 6 Arrays (die letzten 5 könnten leer sein) von Objekten. Jedes Objekt hat einen Namen und das Array könnte viele von jedem Objekt haben (jedes Array ist so sortiert, dass jedes Objekt gruppiert ist).Wie man ImageView/textView auf der rechten Seite von anderen hinzufügen kann ImageView/textView in einem Layout

Zuerst habe ich diese XML-Datei:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/fond4" 
android:orientation="vertical" 
tools:context="${relativePackage}.${activityClass}" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="20dp" 
    android:background="@color/white" 
    android:gravity="center" 
    android:text="@string/commentOpti" 
    android:textSize="20sp" /> 

<Button 
    android:id="@+id/choisirTroupe" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="20dp" 
    android:onClick="soumission" 
    android:text="@string/lancer" /> 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     tools:context="${relativePackage}.${activityClass}" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="20dp" 
      android:background="@color/white" 
      android:gravity="center" 
      android:text="@string/casernes" 
      android:textSize="20sp" /> 

     <!-- Caserne 1 --> 

     <LinearLayout 
      android:id="@+id/caserne1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:background="@color/white" 
      android:orientation="horizontal" > 

      <ImageView 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_marginLeft="10dp" 
       android:background="@drawable/caserne" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:text="@string/deuxPoints" 
       android:textSize="25sp" /> 
     </LinearLayout> 

     //Then 5 other like this 1st linearLayou 
     //The ids are incremented each time like caserne2 for the 2nd etc... 


</ScrollView> 

Es wird wie folgt aussehen: https://gyazo.com/17a1276dfff5521d540fb6dc953df424

Was ich tun möchte, ist, für jedes Objekt in jedem Array (ein Array: ein lineares Layout), um eine textView mit der Nummer von Item und eine imageView, die das Objekt darstellen, hinzuzufügen.

Als nächstes werden Sie feststellen, wie ich alles sortieren, das ist nicht wirklich wichtig, denke ich, aber wenn man alles verstehen wollen, werden Sie einen Blick geben müssen: p

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_optimisation); 

    LinearLayout caserne1 = (LinearLayout) findViewById(R.id.caserne1); 
    //...Doing this for the 5 others... 

    Intent intent = getIntent(); 


    //Instanciation of my object 
    OptiClashOfClans o = new OptiClashOfClans(); 

    //In fact, the number of linearLayout i'll have to work with 
    o.setNbCasernes(this.nbCaserne); 

    if (this.nbBarbares > 0) 
     o.ajouterFormation(1, this.nbBarbares); 
    //...Adding each object in on array...  
    o.setAFormer(o.triTroupe()); 

    //And finally here i'll put each object in the good array 
    o.orgaCaserne(o); 

Nun denn wir gehen meine Problem, dieser Teil des Codes direkt neben dem einen, die ich vorher gesagt, hier ist, wie ich in jedem Array

for (int cptCaserne = 0; cptCaserne < this.nbCaserne; cptCaserne++) { 


     // Here I pick up the array of object I'll work with 
     Caserne casTmp = o.getCaserneNum(cptCaserne); 


     // Here I pick every object which are in the previous array 
     ArrayList<Troupe> enFormTmp = new ArrayList<Troupe>(); 
     enFormTmp = casTmp.getEnFormation(); 

     // To count where I am in the array of object 
     int cptAFormer = 0;   

     //To know if the next object is different from the one I'm working with 
     Troupe troupTmp = enFormTmp.get(cptAFormer); 

     int cptTroupTmp = 0; 

     //For the object t in the array of object 
     for (Troupe t : enFormTmp) { 
      cptTroupTmp = 0; 

      //While the object is the same from the one we're working with 
      while (!troupTmp.equals(t)) { 
       //Incrementing the previous counter 
       cptTroupTmp++; 
       cptAFormer++; 
      } 

die Anzahl des gleichen Objekts abholen Und schließlich ist hier, wie ich nicht wirklich wissen, wie zu tun:

  ImageView iView = new ImageView(Optimisation.this); 

      //To know which background i'll add to the image view 
      //I'll do this for each different object 
      if (t.getNom().equals("Barbare")) 
       iView.setImageResource(R.drawable.barbare); 
      //... 

      //The text view with the number of object I found 
      TextView tView = new TextView(Optimisation.this); 
      tView.setText("" + cptTroupTmp); 


      //And now it's here where I want to add the different views 
      switch (cptCaserne) { 
      case 0: 
       caserne1.addView(tView); 
       caserne1.addView(iView); 
      case 1: 
       caserne2.addView(tView); 
       caserne1.addView(iView); 
      case 2: 
       caserne3.addView(tView); 
       caserne1.addView(iView); 
      case 3: 
       caserne4.addView(tView); 
       caserne1.addView(iView); 

      } 
      troupTmp = enFormTmp.get(cptAFormer); 
     } 

    } 

Mit diesem Code habe ich einen wahnsinnigen schwarzen Bildschirm, wenn ich diese Aktivität mache. Hier ist, was ich mit in rot der Textview mit der Anzahl der objet und in Grün der Imageview des Objekts machen will ... https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc

ich ein paar Sachen gefunden habe, mit LayoutInflater zu tun, aber ich habe nicht war erfolgreich mit diesen ... Vielen Dank, wenn Sie mir helfen könnten.

PS: sorry für die Fehler, ich denke, es gibt eine Menge, aber in langen Pfosten wie diese, meine Schule Englisch ist nicht sehr effektiv ^^ Thanks again :)

Antwort

1

mich korrigieren, wenn ich Ich liege falsch. Aber von dem, was ich verstanden habe, müssen Sie nur erreichen, was hier gezeigt wird https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc?

Basis auf Ihrem Code müssen Sie nur eine zusätzliche LinearLayout mit einer horizontalen Ausrichtung auf jedem LinearLayout hinzufügen, bevor Sie die TextView und Image View hinzufügen.

Hier ist ein Beispiel Ihr Code verwendet:

LinearLayout innerLayout = new LinearLayout(Optimisation.this) 
innerLayout.setOrientation(LinearLayout.HORIZONTAL); 
ImageView iView = new ImageView(Optimisation.this); 

     //To know which background i'll add to the image view 
     //I'll do this for each different object 
     if (t.getNom().equals("Barbare")) 
      iView.setImageResource(R.drawable.barbare); 
     //... 

     //The text view with the number of object I found 
     TextView tView = new TextView(Optimisation.this); 
     tView.setText("" + cptTroupTmp); 


     innerLayout.addView(tView); 
     innerLayout.addView(iView); 

     //And now it's here where I want to add the different views 
     switch (cptCaserne) { 
     case 0: 
      caserne1.addView(innerLayout); 
     case 1: 
      caserne2.addView(innerLayout); 
     case 2: 
      caserne3.addView(innerLayout); 
     case 3: 
      caserne4.addView(innerLayout); 

     } 

Fühlen Sie sich frei, es zu ändern, wenn das Layout, das ich die innerLayout bin Zugabe in falsch ist. Aber im Grunde ist das die Idee.

+0

mit "Optimisation.this" eclypse sagen mir, dass die Optimierung konnte nicht zu einem Typ aufgelöst werden, aber es funktioniert immer noch nicht, aber Sie gaben mir eine Idee, die ich versuchen werde vielen Dank :) – Kokodelo

+1

new LinearLayout (Optimisation.this) Ersetzen Sie es mit diesem: new LinearLayout (this) – ljpv14

+0

Wenn ich die Taste drücken, um zu dieser Aktivität zu gehen, habe ich immer noch einen schwarzen Bildschirm und dann sagt es, dass die Anwendung nicht reagiert ... Und das ist mit Ihrer Antwort, könnte es komme von etwas falsch mit meinem Code? (nicht derjenige, der alles sortiert, ich habe eine Menge Tests gemacht und es funktioniert, aber mit einem der letzten drei, die ich unten eingefügt habe?) – Kokodelo

Verwandte Themen