2012-10-12 156 views
0

我有ListView中鏈接ListActivity中的ViewView的一個奇怪的行爲。奇怪的行爲與ListActivity

所以,我有列表活動,其中列表由3個文本字段和一個圖像組成。

<TextView android:id="@+id/me_games_won" 
    android:textSize="16sp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<TextView android:id="@+id/gamesmate_username" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<TextView android:id="@+id/him_games_won" 
    android:textSize="16sp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<ImageView 
    android:id="@+id/imageActionHomepage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/im_completed" /> 
</LinearLayout> 

我的適配器根據一些條件查看更改圖像。我有以下代碼:

public View getView(int position, View convertView, ViewGroup parent) { 
     // assign the view we are converting to a local variable 

     View v; 

     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.homepage_listitem, null); 

     /* 
     * Recall that the variable position is sent in as an argument to this method. 
     * The variable simply refers to the position of the current object in the list. (The ArrayAdapter 
     * iterates through the list we sent it) 
     * 
     * Therefore, i refers to the current Item object. 
     */ 
     JSONGame i = (JSONGame) objects.get(position); 

     if (i != null) { 
      // This is how you obtain a reference to the TextViews. 
      // These TextViews are created in the XML files we defined. 
      TextView tt = (TextView) v.findViewById(R.id.me_games_won); 
      TextView ttd = (TextView) v.findViewById(R.id.gamesmate_username); 
      TextView mt = (TextView) v.findViewById(R.id.him_games_won); 
      ImageView iv = (ImageView) v.findViewById(R.id.imageActionHomepage); 

      // check to see if each individual textview is null. 
      // if not, assign some text! 
      if (tt != null) { 
       tt.setText(Long.toString(i.getScore())); 
      } 
      if (ttd != null) { 
       ttd.setText(i.getGamemateUsername()); 
      } 
      if (mt != null) { 
       mt.setText(Long.toString(i.getGamemateScore())); 
      } 
      if (i.getAction().equals(JSONGame.GAME_NONE)) 
       iv.setImageResource(R.drawable.im_completed); 
      else if (i.getAction().equals(JSONGame.GAME_ACCEPT)) { 
       iv.setOnClickListener(new HomepageMenuAdapter(this.activity, position, R.drawable.im_accept)); 
       iv.setImageResource(R.drawable.im_accept); 
      } else if (i.getAction().equals(JSONGame.GAME_WAITING)) 
       iv.setImageResource(R.drawable.im_awaiting); 
      else 
       iv.setOnClickListener(new HomepageMenuAdapter(this.activity, position, R.drawable.im_continue)); 
       iv.setImageResource(R.drawable.im_continue); 
     } 

     // the view must be returned to our activity 
     return v; 
    } 

在列表框中顯示的圖像是隨機的,但往往等於最後一排。我每次都在充氣該行以確保創建該行的新實例。顯示的圖像不是佈局文件中設置的圖像。

乾杯。 David。

回答

2

看起來你缺少最後else段括號:

: 
    } else if (i.getAction().equals(JSONGame.GAME_WAITING)) 
      iv.setImageResource(R.drawable.im_awaiting); 
    else { // <-- missing 
      iv.setOnClickListener(new HomepageMenuAdapter(this.activity, position, R.drawable.im_continue)); 
      iv.setImageResource(R.drawable.im_continue); 
    } // <-- missing 
    :