Android Open Source - droidcon-android-espresso Posts Adapter






From Project

Back to project page droidcon-android-espresso.

License

The source code is released under:

Apache License

If you think the Android project droidcon-android-espresso listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.xrigau.droidcon.espresso.presentation.adapter;
//from  w w w. j  a va2 s.  co  m
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xrigau.droidcon.core.model.Post;
import com.xrigau.droidcon.core.model.PostList;
import com.xrigau.droidcon.espresso.R;

public class PostsAdapter extends BaseAdapter {

    private final PostList posts;
    private final LayoutInflater inflater;
    private final Resources resources;

    public PostsAdapter(PostList posts, LayoutInflater inflater, Resources resources) {
        this.posts = posts;
        this.inflater = inflater;
        this.resources = resources;
    }

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

    @Override
    public Post getItem(int position) {
        return posts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.posts_list_item, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }

        ViewHolder holder = ViewHolder.from(convertView.getTag());
        Post post = getItem(position);
        populateView(holder, post);

        return convertView;
    }

    private void populateView(ViewHolder holder, Post post) {
        holder.title.setText(post.getTitle());
        holder.domain.setText(post.getDomain());
        updateCommentsVuew(holder, post.getCommentCount());
        holder.time.setText(post.getTime());
    }

    private void updateCommentsVuew(ViewHolder holder, int comments) {
        if (comments == 0) {
            holder.comments.setVisibility(View.GONE);
            return;
        }
        int stringResId = comments == 1 ? R.string.one_comment : R.string.many_comments;
        holder.comments.setText(resources.getString(stringResId, comments));

        holder.comments.setVisibility(View.VISIBLE);
    }

    private static class ViewHolder {

        private final TextView title;
        private final TextView domain;
        private final TextView comments;
        private final TextView time;

        public ViewHolder(View view) {
            title = (TextView) view.findViewById(R.id.title);
            domain = (TextView) view.findViewById(R.id.domain);
            comments = (TextView) view.findViewById(R.id.comments);
            time = (TextView) view.findViewById(R.id.time);
        }

        public static ViewHolder from(Object tag) {
            return (ViewHolder) tag;
        }
    }
}




Java Source Code List

com.xrigau.droidcon.core.debug.MockClient.java
com.xrigau.droidcon.core.model.PostList.java
com.xrigau.droidcon.core.model.PostShould.java
com.xrigau.droidcon.core.model.Post.java
com.xrigau.droidcon.core.service.Services.java
com.xrigau.droidcon.core.task.NewsTask.java
com.xrigau.droidcon.core.task.TaskExecutorShould.java
com.xrigau.droidcon.core.task.TaskExecutor.java
com.xrigau.droidcon.core.task.Task.java
com.xrigau.droidcon.espresso.AndroidUnitTest.java
com.xrigau.droidcon.espresso.helper.DrawableMatcher.java
com.xrigau.droidcon.espresso.helper.EspressoTestsMatchers.java
com.xrigau.droidcon.espresso.loader.LoaderListener.java
com.xrigau.droidcon.espresso.loader.PostsTaskLoaderCallbacks.java
com.xrigau.droidcon.espresso.loader.PostsTaskLoader.java
com.xrigau.droidcon.espresso.presentation.IdlingResourceActivityTest.java
com.xrigau.droidcon.espresso.presentation.IdlingResourceActivity.java
com.xrigau.droidcon.espresso.presentation.MainActivityTest.java
com.xrigau.droidcon.espresso.presentation.MainActivity.java
com.xrigau.droidcon.espresso.presentation.PostListActivityTest.java
com.xrigau.droidcon.espresso.presentation.PostListActivity.java
com.xrigau.droidcon.espresso.presentation.VisitorActivityTest.java
com.xrigau.droidcon.espresso.presentation.VisitorActivity.java
com.xrigau.droidcon.espresso.presentation.WorldDestructionCountdown.java
com.xrigau.droidcon.espresso.presentation.adapter.EmptyAdapter.java
com.xrigau.droidcon.espresso.presentation.adapter.PostsAdapter.java
com.xrigau.droidcon.espresso.util.DeveloperError.java