私はアプリケーションのリストビューを作成するために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));
コードが従うことが少し難しいです。コメントは役に立ちます。バッジビューを9回設定する理由は何ですか?それは常に最後の値に設定されます。 –
申し訳ありませんが、私はコメント(およびListViewのコード)を追加しました。 9が表示されるバッジの最大数です。 IEでは、9種類のバッジが表示されます。実際には最初のバッジ値に設定されます。 –
ええ、この質問を閉じる方法はありますか?私は問題を特定しましたが、それは私が期待したものとはまったく異なります。 –