2012-10-12 99 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 
    :