Android的定制适配器 [英] Android custom adapters

查看:153
本文介绍了Android的定制适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android的定制适配器以创建为我的应用程序的列表视图。我期待,以显示是否有人在第一,第二或第三位,也显示了每个人在列表中独特的徽章。下面的code工程的排名,但它显示了每个人相同的徽章。我不能完全似乎找出原因。如果一个人不赚徽章,他或她应该不会看到任何东西(当我设置阵列为null)每个人的徽章都设置为null。我已经调试这一段时间,似乎无法找出什么是错的。我想这件事情很简单。谢谢!

 公共类LeaderArrayAdapter扩展ArrayAdapter< UserStatus> {
        私人最终字符串LOG_TAG =LeaderArrayAdapter;
        私人最终上下文的背景下;
        // UserStatus类:包含一个文本值 - 用户名,
        //为地方(第一,第二,第三..)和一个整数数组用户整型值
        //徽章
        私人最终UserStatus []值;        //图标阵列重新present色带每个地方(第一,第二..)
        私有静态诠释[] = 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};        //这个变量数组包含布局每个徽章应该放在 - 多达9
        //徽章
        私有静态诠释[] = 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};        //这个数组包含重新present每个徽章在具体的R值
           可绘制文件夹
        私有静态诠释[] = 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};        公共LeaderArrayAdapter(上下文的背景下,UserStatus []值){
            超(背景下,R.layout.leaderboard_rowlayout,价值观);
            this.context =背景;
            this.values​​ =值;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            LayoutInflater吹气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            //每一行都将包含一个地方的图标,用户名,用户价值和用户
            //徽章。这些值来自UserStatus变量值
            查看rowView = inflater.inflate(R.layout.leaderboard_rowlayout,空,真);
            TextView中的userName =(TextView中)rowView.findViewById(R.id.userName1);
            ImageView的placeView =(ImageView的)rowView.findViewById(R.id.place);
            userName.setText(值[位置]。名称);
            placeView.setImageResource(placeIcons [位置]);            //如果没有与此相关的UserStatus徽章,遍历每个
            //徽章位置(只显示9),然后将相关的图标            //徽章的数量,确定了布局上的徽章位置
            //(badgeIconLayout - 也就是,位置1,2或3)
            //我们只想设置徽章图标与徽章的用户
            如果(值[位置] .badges!= NULL){
                的for(int i = 0; I<值[位置] .badges.length;我++){
                    如果(ⅰ&10 9){
                        //选择图标布局位置和设置图像资源
                        //因此
                        ImageView的badgeView =
                                    (ImageView的)rowView.findViewById(badgeIconLayout [I]);
                        Log.d(LOG_TAG,图像设置=+值[位置] .badges [I]);
                    badgeView.setImageResource(badgeIcons [值[位置] .badges [我]]);
                    }
                }
            }其他{
                / * ImageView的badgeView =(ImageView的)rowView.findViewById(badgeIconLayout [0]);
                actionView.setImageResource(R.drawable.icon);
                返回rowView; * /
            }
            返回rowView;
        }
    }

在ListActivity的关键部分是如下:

  // inputarray重新presents从我们的数据库返回的所有用户
            // PHP文件返回,以便所有用户(按第一,第二,第三等。
            //地方
            而(ⅰ&下; inputarray.length()){
                UserStatus用户=新UserStatus();
                user.name = inputarray.getJSONArray(1).getString(ⅰ);
                user.score = inputarray.getJSONArray(0).getString(ⅰ);                //此方法遍历服务器阵列存储徽章
                INT [] myArray的= getUserBadges(user.name);
                user.badges = myArray的;
                user.iconPlace = I;
                值[I] =用户;
                我++;            }            //一旦我们存储有关用户的所有信息,我们称之为setListAdapter
            //我检查,这按预期工作
            setListAdapter(新LeaderArrayAdapter(getApplicationContext(),值));


解决方案

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

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

所以在你的适配器(pseude- code):

 如果(user.getAward()== myRedValue){
    awardImageView.setBackgroundResource(R.drawable.red_award);
}否则如果(user.getAward()== myBlueValue){
    awardImageView.setBackgroundResource(R.drawable.blue_award);
}等等......

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!

   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;
        }
    }

The key part of the ListActivity is below:

            // 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));

解决方案

I also didn't look through your code, and would probably like to see the code for your custom adapter.

I would suggest in your custom adapter, that you determine the value of the award for that particular line item, and set the badge image specifically at that time.

so in your adapter (pseude-code):

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

这篇关于Android的定制适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆