ViewHolder Listview - 自定义适配器

时间:2014-06-04 08:09:51

标签: android listview android-listview android-viewholder

我一直在阅读Viewholders以获取listview,并且它们可以帮助提高listview滚动的性能。我一直在尝试为我的CustomAdapter实现它,但似乎我和#39;我在某处做错了(不断崩溃)。那么我该如何在我的自定义适配器中实现一个视图?

                    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.activity_list_view,
                parent, false);
    } else {
        convertView = myInflater.inflate(R.layout.activity_list_view,
                parent, false);
    }
    TextView name = (TextView) convertView.findViewById(R.id.feed_post);
    final String isLiked;
    TextView date = (TextView) convertView.findViewById(R.id.duration);
    HashMap<String, String> Item = new HashMap<String, String>();
    ImageView image = (ImageView) convertView.findViewById(R.id.comments);
    ImageView image_thumbs = (ImageView) convertView
            .findViewById(R.id.thumbs);
    Item = arrayList.get(position);
    // Get the time
    time = Item.get("Time");
    // Break up the time
    new_time = time.split("T")[0];
    isLiked = Item.get("Liked");
    // Split more
    get_splitted_time = new_time.split("-");
    // Get the month
    Year = get_splitted_time[0];
    Day = get_splitted_time[2];
    if (isLiked == null || isLiked == "false") {
        image_thumbs.setImageDrawable(convertView.getResources()
                .getDrawable(R.drawable.thumbsup_liked_new_flattened));
    }
    if (isLiked == "true") {
        image_thumbs
                .setImageDrawable(convertView.getResources().getDrawable(
                        R.drawable.thumbsup_liked_state_new_flattened));
    }
    if (get_splitted_time[1].startsWith("0")) {
        Month = months[Integer.valueOf(get_splitted_time[1].substring(1)) - 1];
    } else {
        Month = months[Integer.valueOf(get_splitted_time[1]) - 1];
    }
    image_thumbs.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isLiked == "true") {
                ;
                HashMap<String, String> Item = new HashMap<String, String>();
                Item = arrayList.get(position);
                Item.put("Liked", "false");
                unlikePost(feed_item_post_id.get(position));
            }
            CustomAdapter.this.notifyDataSetChanged();
        }
    });
    // Onclick listener for comments
    image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(context, SinglePost.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            myIntent.putExtra("post_id", feed_item_post_id.get(position));
            myIntent.putExtra("liked", true);
            myIntent.putExtra("position", 0);
            context.startActivity(myIntent);

        }
    });
    final_time = Day + " " + Month + " " + Year;
    name.setText(Item.get("Message"));
    date.setText(final_time);
    return convertView;
}

2 个答案:

答案 0 :(得分:0)

有一个很好的教程,关于一个名叫lars vogel的人的名单。部分是持有人模式。这是直接链接:

http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder

在这个网站上,您可以找到很棒的Android教程,几乎可以找到任何东西。我强烈推荐该网站。如果您在阅读教程中的段落后仍然对持有者有疑问...只需在此处撰写评论。

喝彩!

答案 1 :(得分:0)

以下是如何实现ViewHolder的示例 只是给你一点提示。

    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.activity_list_view,
                    parent, false);
              ViewHolder holder = new ViewHolder();
              holder.name = (TextView) convertView.findViewById(R.id.feed_post);
              holder.date = (TextView) convertView.findViewById(R.id.duration);
              holder.image = (ImageView) convertView.findViewById(R.id.comments);
              convertView.setTag(holder); // dont forget this one

        } else {
            holder = (ViewHolder) convertView.getTag(); // recycle
        }

    ....
    // how to use it
        holder.name.setText(Item.get("Message"));
        holder.date.setText(final_time);
        return convertView;


    }

// define your holder class
        private static class ViewHolder{
            TextView name;
            TextView date;
            ImageView image;
        }