ListView(活动源)有20种不同类型的行,每行有20个不同的视图

时间:2013-12-31 10:16:30

标签: android listview android-listview

一周前,我在stackoverflow上询问了一个关于列表视图中包含不同类型项目的问题。

I know how to populate a list with custom items via custom adapter. But how do I populate it with a few different types of items?

当时我不知道它会有所作为所以我提到我只有4种类型的列表项不会使事情复杂化。我得到了一些回答,解释了极其简单的案例(2种类型的项目,每个项目内有一个视图)。我不认为我可以实现我得到的示例中解释的那些方法,我需要一种更面向对象的方法。

我想有一个单独的类,它采用XML布局并处理其视图和按钮。

这样的事情:

public class MyProfileCommentAdapter extends BaseAdapter {

    private ArrayList<String> itemNames =  new ArrayList<String>();
    private ArrayList<Double> itemRatings = new ArrayList<Double>();
    private ArrayList<String> itemComments  = new ArrayList<String>();
    private ArrayList<String> itemCommentDates   = new ArrayList<String>();
    private ArrayList<Integer> itemIds = new ArrayList<Integer>();
    private Context context;
    private LayoutInflater layoutInflater;

    private ImageButton iMyCommentsCommentedProductImage;
    private TextView tvMyCommentsCommentedItemName;
    private TextView tvMyCommentsCommentedItemDate;
    private TextView tvMyCommentsCommentedItemContent;
    private TextView tvMyCommentsCommentedItemRating;
    private Button bMyCommentsEditComment;
    private Button bMyCommentsDeleteComment;

    public MyProfileCommentAdapter(Context context, ArrayList<String> itemNames, ArrayList<Double> itemRatings, ArrayList<String> itemComments, ArrayList<String> itemCommentDates, ArrayList<Integer> itemIds){

        this.context = context;
        this.itemNames = itemNames;
        this.itemComments = itemComments;
        this.itemCommentDates = itemCommentDates;
        this.itemRatings = itemRatings;
        this.itemIds = itemIds;

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return itemIds.size();
    }

    @Override
    public Object getItem(int arg0) {
        return itemIds.get(arg0)    ;
    }

    @Override
    public long getItemId(int position) {
        return itemIds.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null) {
            convertView = layoutInflater.inflate(R.layout.item_adapterable_my_profile_comment, parent, false);
        }

        iMyCommentsCommentedProductImage = (ImageButton) convertView.findViewById(R.id.iMyCommentsCommentedProductImage);
        tvMyCommentsCommentedItemName = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemName);
        tvMyCommentsCommentedItemDate = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemDate);
        tvMyCommentsCommentedItemContent = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemContent);
        tvMyCommentsCommentedItemRating = (TextView) convertView.findViewById(R.id.tvMyCommentsCommentedItemRating);
        bMyCommentsEditComment = (Button) convertView.findViewById(R.id.bMyCommentsEditComment);
        bMyCommentsDeleteComment = (Button) convertView.findViewById(R.id.bMyCommentsDeleteComment);    

        tvMyCommentsCommentedItemName.setText(itemNames.get(position));
        tvMyCommentsCommentedItemDate.setText(itemCommentDates.get(position));
        tvMyCommentsCommentedItemRating.setText(String.valueOf(itemRatings.get(position)));
        tvMyCommentsCommentedItemContent.setText(itemComments.get(position));

        return convertView;
    }
}

我需要大约20个并将它们全部放在列表视图(我的应用的活动源)中,具体取决于我的应用中最近发生的活动。每个不同的活动都有一个单独的XML,其中包含尽可能多的视图元素,就像这个一样,都在做和显示不同的东西。

现在,如果我要使用建议的方法,

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            Log.v(LOG_TAG, "getView " + position + " " + convertView + " type = " + type);
            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.item, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.separator, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    break;
                default:
                    break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.textView.setText(mData.get(position));
            return convertView;
        }

看起来很简单,不是吗? 但就我而言,我应该在SWITCH中有20个不同的案例,每个案例中应该有20-30行代码。我不希望以这种方式实现它。可悲的是,我不知道该怎么做......

1 个答案:

答案 0 :(得分:1)

请检查以下链接,希望它有助于解决您的问题: Magic Happens here