2013-02-08 36 views
0

我的代碼的一部分是LinearLayout,在ScrollView之內。 ScrollView有10行,每行4列。第一列是一個小的ImageView(紅色'x'或綠色複選標記)。從下圖中可以看出,只有第一個ImageView出於某種原因顯示。我想要顯示第一列中的每一行。10個ImageView中只有1個顯示 - Android

下面是XML代碼:

<ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="65" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <ImageView 
        android:id="@+id/q1Image" 
        android:layout_width="10dp" 
        android:layout_height="10dp" /> 

       <TextView 
        android:id="@+id/q1Question" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="48" 
        android:paddingLeft="5dp" 
        android:paddingBottom="6dp" /> 

       <TextView 
        android:id="@+id/q1Answer" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="27" 
        android:paddingBottom="6dp" /> 

       <TextView 
        android:id="@+id/q1Verse" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="25" 
        android:paddingBottom="6dp" /> 
      </LinearLayout> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp"  
      android:background="#C2BEBF" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <ImageView 
        android:id="@+id/q2Image" 
        android:layout_width="10dp" 
        android:layout_height="10dp" /> 

       <TextView 
        android:id="@+id/q2Question" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="48" 
        android:paddingLeft="5dp" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q2Answer" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="27" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q2Verse" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="25" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 
      </LinearLayout> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp"  
      android:background="#C2BEBF" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <ImageView 
        android:id="@+id/q3Image" 
        android:layout_width="10dp" 
        android:layout_height="10dp" /> 

       <TextView 
        android:id="@+id/q3Question" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="48" 
        android:paddingLeft="5dp" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q3Answer" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="27" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q3Verse" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="25" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 
      </LinearLayout> 

Java代碼

ImageView q1Image = (ImageView)findViewById(R.id.q1Image); 
     q1Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q2Image = (ImageView)findViewById(R.id.q2Image); 
     q2Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q3Image = (ImageView)findViewById(R.id.q3Image); 
     q3Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q4Image = (ImageView)findViewById(R.id.q4Image); 
     q4Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q5Image = (ImageView)findViewById(R.id.q5Image); 
     q5Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q6Image = (ImageView)findViewById(R.id.q6Image); 
     q6Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q7Image = (ImageView)findViewById(R.id.q7Image); 
     q7Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q8Image = (ImageView)findViewById(R.id.q8Image); 
     q8Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q9Image = (ImageView)findViewById(R.id.q9Image); 
     q9Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     ImageView q10Image = (ImageView)findViewById(R.id.q10Image); 
     q10Image.setScaleType(ImageView.ScaleType.FIT_CENTER); 

     ArrayList<ImageView> ivArray = new ArrayList<ImageView>(10); 
     ivArray.add(q1Image); 
     ivArray.add(q2Image); 
     ivArray.add(q3Image); 
     ivArray.add(q4Image); 
     ivArray.add(q5Image); 
     ivArray.add(q6Image); 
     ivArray.add(q7Image); 
     ivArray.add(q8Image); 
     ivArray.add(q9Image); 
     ivArray.add(q10Image); 
     for(int i = 0; i < 1; i++) { 
      if(qs.get(i).getSelectedAnswer() == qs.get(i).getCorrectAnswer()) { 
       ivArray.get(i).setBackgroundResource(R.drawable.green_check_mark); 
      } else { 
       ivArray.get(i).setBackgroundResource(R.drawable.red_x); 
      } 
     } 

enter image description here

回答

3

你應該實現一個ListView和一個適配器。正如您所看到的,手動管理將代碼添加到您的ScrollView並手動獲取每個ImageView參考的代碼是很痛苦的。

請參閱Building Layouts with an Adapter

請發表您有任何問題,我會很樂意擴大我的回答。

現在,更直接地回答你的問題......

您的循環只訪問索引0試試這個:

for (int i = 0; i < ivArray.size(); i++) 
+0

啊,我把這個循環改爲1進行測試,並且在測試完成後忘了將它改回來。謝謝你指出。至於''''Adapter'''',讓我研究一下你的鏈接和Google,看看我是否應該實現它。你是對的,改變''''ScrollView''''中的所有內容是一種痛苦。 – Matt 2013-02-08 18:18:53

3

這裏是在Java代碼中的bug:

for(int i = 0; i < 1; i++) { 
    if(qs.get(i).getSelectedAnswer() == qs.get(i).getCorrectAnswer()) { 
     vArray.get(i).setBackgroundResource(R.drawable.green_check_mark); 
     } 
// .... 

你的循環停止一t 1 (i < 1)。嘗試將其更改爲(i < 10)並查看它是否變得更好。

+0

而不是選擇任意數量爲什麼不使用的大小名單? – Sam 2013-02-08 18:01:30

+0

同意 - 我選擇專注於眼前的問題,而不是批評OP的編碼風格。如果我冒險走這條路線,我建議他/她用一個基於ListView和Adapter的方法替換所有嵌套的LinearLayout。 – mportuesisf 2013-02-08 18:12:26

+0

啊,我爲了測試目的將該循環更改爲1,並且在完成測試時忘記將其更改回來。謝謝你指出。對於'''''ListView''''和'''''''''''',我必須考慮到這一點。我被建議現在嘗試多次,但從未有過。 – Matt 2013-02-08 18:17:23