2016-04-09 18 views
0

私のコードでは、オブジェクトの6つの配列(最後は5つが空である可能性があります)があります。各オブジェクトには名前があり、配列には各オブジェクトの多くが含まれています(すべての配列はソートされているため、すべてのアイテムがグループ化されます)。ImageView/TextViewを他のオブジェクトの右側に追加する方法ImageView/textViewを同じレイアウトにする

まず、私はこのxmlファイルを持っている:

<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> 

それは次のようになります。https://gyazo.com/17a1276dfff5521d540fb6dc953df424は、各アレイ内の各オブジェクト(1つの配列のために、私は何をしたいです

: 1つの線形レイアウト)を使用して、オブジェクトの数を表すtextViewとそのオブジェクトを表すimageViewを追加します。

次に、あなたはそれが本当に重要ではありません、私はすべてを並べ替える方法を見つけるだろうと思いますが、あなたは一見を与える必要があるでしょう、すべて理解したい場合: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); 

を今、私たちは私のために行きます問題は、コードのこの部分はすぐ隣、私が以前に入れたものとされ、ここで私は、各配列ついに

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++; 
      } 

とで同じオブジェクトの数を拾う方法ですが、私は本当に方法がわからない方法です手順:

  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); 
     } 

    } 

このコードでは、このアクティビティを実行するときに私は非常識な黒い画面が表示されます。ここ は https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc

は私がLayoutInflaterを行うにはいくつかのものを見つけた...私はオブジェの数と、物体の緑ImageViewの中に赤のTextViewにしてやりたいですが、私はそうではありませんこれらの成功した...あなたが私を助けることができれば多くありがとう。

PS:ミスのため申し訳ありませんが、私はたくさんあると思いますが、このような長い記事では、私の学校の英語は非常に効果的ではありません^^ 私は「場合おかげで再び:)

答えて

1

は私を修正間違っている。しかし、私が理解していることから、あなたはここに示されているものを達成する必要があるだけですhttps://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc

コードに基づいて、TextViewとImage Viewを追加する前に、各LinearLayoutに水平方向のLinearLayoutを追加するだけで済みます。

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); 

     } 

は、私はinnerLayoutを追加しているレイアウトが間違っている場合は、それを調整してお気軽に:

はここにあなたのコードを使用した例です。しかし基本的にはそれがアイデアです。

+0

"Optimisation.this" eclypseは、最適化をタイプに解決できないと言っていますが、それでもまだ機能しませんが、あなたに感謝したいアイデアをくれました:) – Kokodelo

+1

new LinearLayout(Optimisiation.this )これを次のように置き換えます:new LinearLayout(this) – ljpv14

+0

ボタンを押してこのアクティビティに移動すると、まだ黒い画面が表示され、アプリケーションが応答していないと表示されます。私のコードに間違っていますか? (すべてを並べ替える人ではなく、私は多くのテストを行いました。それは動作していますが、最後に貼り付けた3つのうちの1つを使用していますか?) – Kokodelo

関連する問題