Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid custom adapters
    primarykey
    data
    text
    <p>I'm using Android's custom adapters to create a list view for my application. I'm looking to show whether or not someone's in first, second or third place and also show unique badges for each person in the list. The code below works for ranking but it shows the same badges for each person. I can't quite seem to figure out why. If a person doesn't earn a badge, he or she should not see anything (when I set the array to null) each person's badges are set to null. I've been debugging this for a while and can't seem to figure out what's wrong. I imagine it's something simple. Thanks!</p> <pre><code> public class LeaderArrayAdapter extends ArrayAdapter&lt;UserStatus&gt; { 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&lt;values[position].badges.length; i++){ if(i&lt;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; } } </code></pre> <p>The key part of the ListActivity is below:</p> <pre><code> // 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&lt;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)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload