2012-02-07 4 views
0

Android의 맞춤 어댑터를 사용하여 내 애플리케이션의 목록보기를 만듭니다. 다른 사람이 1 순위, 2 순위 또는 3 순위에 있는지 여부를 보여주고 목록에있는 각 사람에게 고유 한 배지를 표시하려고합니다. 아래 코드는 순위 지정에 사용되지만 각 사람마다 동일한 배지가 표시됩니다. 나는 이유를 이해하는 것처럼 보일 수 없다. 한 사람이 배지를 얻지 못하면 아무 것도 보지 않아야합니다. 배열을 null로 설정하면 각 사람의 배지는 null로 설정됩니다. 나는 이것을 잠시 동안 디버깅 해왔고 무엇이 잘못되었는지를 파악하지 못했습니다. 나는 그것이 단순한 것이라고 상상한다. 감사!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 번 설정하는 이유는 무엇입니까? 어쨌든 항상 마지막 값으로 설정됩니다. –

+0

죄송합니다. 의견 (및 ListView 코드)을 추가했습니다. 9는 내가 보여줄 배지의 최대 수입니다. IE에서는 최대 9 개의 다른 배지를 보여줍니다. 실제로 첫 번째 배지 값으로 설정됩니다. –

+0

흠,이 질문을 끝내는 방법이 있습니까? 나는 그 문제를 확인했는데 그것은 내가 기대했던 것과는 완전히 다른 것이다. –

답변

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에 대한 코드를 추가했습니다. –