获取适配器中的项目视图类型仅返回1项

时间:2014-10-10 20:52:40

标签: android android-listview android-adapter custom-adapter

我有一个自定义适配器,它会根据特定条件对两个布局进行充气。 GetItemViewType只返回位置中的1个项目,当有两个时,当我在列表中显示时,适配器膨胀一个布局,另一个不是,我可以在我的列表中看到剩下的空间但它的空白。 这是我的代码

enter code here
public class ListViewAdapter extends ArrayAdapter<NewsFeed>
{
    private static final int missionLayout = 0;
    private static final int badgeLayout = 1;
    private TreeSet mSeparatorsSet = new TreeSet();
    public ListViewAdapter ()
    {
        super(CityNewsFeedActivity.this,R.layout.activity_activity_city_news_feed,newsfeed);
    }
    @Override
    public int getItemViewType(int position)
    {
        return mSeparatorsSet.contains(position) ? missionLayout : badgeLayout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder=null;

        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView + " type = " + type);
        if (convertView == null)
        {
            NewsFeed obj = newsfeed.get(position);
            holder = new ViewHolder();
            switch (type)
            {
                case missionLayout:
                    Log.d("fet 3ala mission layout","tahet il case");
                    convertView = getLayoutInflater().inflate(R.layout.city_news_single_item, parent,false);
                    holder.civilianImage = (ImageView) convertView.findViewById(R.id.imageView2);
                    holder.heroName = (TextView) convertView.findViewById(R.id.badgename);
                    holder.description = (TextView) convertView.findViewById(R.id.description);
                    holder.timeRemaining = (TextView) convertView.findViewById(R.id.postedSince);
                    holder.relativeLayoutMission = (RelativeLayout) convertView.findViewById(R.id.relativeLayout);

                    if (obj.category.equals("mission"))
                    {

                        if (obj.accepted_rejected.equals("success"))
                        {
                            holder.relativeLayoutMission.setBackgroundResource(R.drawable.mission_successful);
                            Float alpha = Float.valueOf("0.9");
                            holder.relativeLayoutMission.setAlpha(alpha);
                            byte[] decodedString = Base64.decode(obj.picture, Base64.DEFAULT);
                            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                            Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
                            BitmapShader shader = new BitmapShader(decodedByte, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                            Paint paint = new Paint();
                            paint.setShader(shader);
                            Canvas c = new Canvas(circleBitmap);
                            c.drawCircle(decodedByte.getWidth() / 2, decodedByte.getHeight() / 2, decodedByte.getWidth() / 2, paint);
                            holder.civilianImage.setImageBitmap(circleBitmap);
                            Drawable d = getResources().getDrawable(
                                    R.drawable.circle_shape_for_civilian_success);
                            holder.civilianImage.setBackgroundDrawable(d);
                            String hero = obj.mission_nickname;

                            holder.heroName.setText(Html.fromHtml("<font color='#FAD84F'>" + hero + "</font>" + " saved " + obj.firstname + " " + obj.lastname));
                            holder.description.setText(obj.description);
                            holder.timeRemaining.setText(obj.mission_postedSince);
                           }
                       }
                      break;
               case badgeLayout:

                    convertView =   getLayoutInflater().inflate(R.layout.badge_single_item, null);
                    holder.relativeLayoutBadge = (RelativeLayout) convertView.findViewById(R.id.relativeLayout);
                    holder.BadgeName = (TextView) convertView.findViewById(R.id.badgename);
                    holder.badgeImage = (ImageView) convertView.findViewById(R.id.imageView2);
                    holder.timeRemaining = (TextView) convertView.findViewById(R.id.postedSince);

                    if (obj.category.equals("badge"))
                    {
                        holder.relativeLayoutBadge.setBackgroundResource(R.drawable.earned_badge_post);
                        Float alpha = Float.valueOf("0.9");
                        holder.relativeLayoutBadge.setAlpha(alpha);
                        String hero = obj.nickname;
                        holder.BadgeName.setText(Html.fromHtml("<font color='#FAD84F'>" + hero + "</font>" + " earns a new badge! "));
                        Drawable d = getResources().getDrawable(R.drawable.circle_shape_for_badge);
                        holder.badgeImage.setBackgroundDrawable(d);
                        String imageUrl = "http://iamheroic.net/iamheroic/badge_images/" + obj.icon;
                        imageLoader.displayImage(imageUrl, holder.badgeImage, options);
                        holder.timeRemaining.setText(obj.postedSince);
                    }
                    break;

            }
            if (convertView != null)
            {
                convertView.setTag(holder);
            }
        }
        return convertView;
    }
}

当我运行应用程序时,列表显示类型1的一个布局,并为类型0留下空间,但未显示。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您还必须覆盖getViewTypeCount(),告诉ListView预期的项目类型数量。它这样做是为了处理每种类型的回收视图。

@Override
public int getViewTypeCount() {
    return 2;
}