2012-02-07 212 views
0

我正在使用Android的自定义适配器为我的应用程序创建列表视图。我希望展示某人是否在第一,第二或第三位,并且还为列表中的每个人显示独特的徽章。下面的代码适用于排名,但它为每个人显示相同的徽章。我似乎无法弄清楚为什么。如果一个人没有获得徽章,他/她就不应该看到任何东西(当我将数组设置为空时),每个人的徽章都设置为空。我一直在调试这一段时间,似乎无法弄清楚什么是错的。我想这很简单。谢谢!Android自定义适配器

public class LeaderArrayAdapter extends ArrayAdapter<UserStatus> { 
     private final String LOG_TAG = "LeaderArrayAdapter"; 
     private final Context context; 
     // UserStatus class: contains a text value - user name, an 
     // integer value for the place (1st,2nd,3rd..) and an integer array for user 
     // badges 
     private final UserStatus[] values; 

     // array of icons to represent a ribbon for each place (1st,2nd..) 
     private static int[] placeIcons ={ R.drawable.first_ribbon1, 
      R.drawable.ribbon_2, R.drawable.ribbon_3, R.drawable.ribbon_4,     
      R.drawable.ribbon_5, R.drawable.ribbon_6, R.drawable.ribbon_7, 
      R.drawable.ribbon_8, R.drawable.ribbon_9, R.drawable.ribbon_10, 
      R.drawable.ribbon_11, R.drawable.ribbon_12, R.drawable.ribbon_13, 
      R.drawable.ribbon_14, R.drawable.ribbon_15}; 

     // This variable array contains the layout each badge should be placed - up to 9 
     // badges 
     private static int[] badgeIconLayout = {R.id.action1, R.id.action2, 
      R.id.action3, R.id.action4, R.id.action5, R.id.action6, R.id.action7, 
      R.id.action8, R.id.action9}; 

     // this array contains the specific R values that represent each badge in the 
      drawables folder 
     private static int[] badgeIcons = {R.drawable.aa, R.drawable.ab, R.drawable.ac, 
      R.drawable.ad, R.drawable.ae,R.drawable.af, R.drawable.ag, R.drawable.ah, 
      R.drawable.ai, R.drawable.aj, R.drawable.ak, R.drawable.al, R.drawable.am, 
      R.drawable.an}; 

     public LeaderArrayAdapter(Context context, UserStatus[] values) { 
      super(context, R.layout.leaderboard_rowlayout, values); 
      this.context = context; 
      this.values = values; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      // Each row will contain a place icon, user name, user value, and user 
      // badges. These values come from the UserStatus variable values 
      View rowView = inflater.inflate(R.layout.leaderboard_rowlayout, null, true); 
      TextView userName = (TextView) rowView.findViewById(R.id.userName1); 
      ImageView placeView = (ImageView) rowView.findViewById(R.id.place); 
      userName.setText(values[position].name); 
      placeView.setImageResource(placeIcons[position]); 

      // If there are badges associated with this UserStatus, loop through each 
      // badge position (only show 9) and place the associated icon 

      // The number of badges, identifies the badge location on the layout 
      // (badgeIconLayout - ie, position 1, 2, or 3) 
      // We only want to set badge icons for those users with badges 
      if(values[position].badges != null){ 
       for(int i=0; i<values[position].badges.length; i++){ 
        if(i<9){ 
         // Select the icon layout position and set the image resource 
         // accordingly 
         ImageView badgeView = 
            (ImageView)rowView.findViewById(badgeIconLayout[i]); 
         Log.d(LOG_TAG, "images to set = " + values[position].badges[i]); 
        badgeView.setImageResource(badgeIcons[values[position].badges[i]]); 
        } 
       } 
      }else{ 
       /*ImageView badgeView = (ImageView)rowView.findViewById(badgeIconLayout[0]); 
       actionView.setImageResource(R.drawable.icon); 
       return rowView;*/ 
      } 
      return rowView; 
     } 
    } 

的ListActivity的关键部分是如下:

  // inputarray represents all of the users returned from our database 
      // the php file returns all users in order (according to 1st, 2nd, 3rd, etc. 
      // place 
      while(i<inputarray.length()){ 
       UserStatus user = new UserStatus(); 
       user.name = inputarray.getJSONArray(1).getString(i); 
       user.score=inputarray.getJSONArray(0).getString(i); 

       // this method loops through the server array to store badges 
       int [] myArray = getUserBadges(user.name); 
       user.badges = myArray; 
       user.iconPlace = i; 
       values[i]=user; 
       i++; 

      } 

      // Once we've stored all information about the users, we call setListAdapter 
      // I've checked that this works as expected 
      setListAdapter(new LeaderArrayAdapter(getApplicationContext(),values)); 
+0

代码有点难以理解。评论会有帮助。任何你设置徽章视图9次的原因?无论如何,它将始终设置为最后一个值。 – 2012-02-07 23:05:45

+0

对不起,我添加了评论(和ListView的代码)。 9是我将展示的徽章的最大数量。 IE,我显示了9个不同的徽章。它实际上设置为第一个徽章值。 – 2012-02-08 02:26:58

+0

嗯,是有办法,我关闭了这个问题?我发现了这个问题,与我的预期完全不同。 – 2012-02-08 06:50:32

回答

0

我也没有通过您的代码看,可能会看到代码为您定制的适配器。

我建议在您的自定义适配器,你确定奖特定行项目的价值,并在那个时候专门设置的徽章图像。

所以在您的适配器(pseude码):

if (user.getAward() == myRedValue) { 
    awardImageView.setBackgroundResource(R.drawable.red_award); 
} else if (user.getAward() == myBlueValue) { 
    awardImageView.setBackgroundResource(R.drawable.blue_award); 
} etc... 
+0

嗯,我发送了自定义适配器的代码。我添加了ListView的代码。 – 2012-02-08 02:28:05