使用实时数据和改造android时图像无法正确显示

时间:2019-07-02 19:20:51

标签: java android mvvm retrofit android-livedata

所有其他数据都在显示,除了单独的图像不能正确显示。

下面是代码。

public class MainActivity extends AppCompatActivity {
    ArrayList<Movies> articleArrayList = new ArrayList<>();
    MoviesAdapter newsAdapter;
    RecyclerView rvHeadline;
    MoviesViewModel newsViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rvHeadline = findViewById(R.id.rvNews);

        newsViewModel = ViewModelProviders.of(this).get(MoviesViewModel.class);
        newsViewModel.init();
        newsViewModel.getNewsRepository().observe(this, newsResponse -> {
            List<Movies> newsArticles = null;
            if (newsResponse != null) {
                newsArticles = newsResponse.getMovies();
                articleArrayList.addAll(newsArticles);
                newsAdapter.notifyDataSetChanged();
            }
        });
        setupRecyclerView();
    }

    private void setupRecyclerView() {
        if (newsAdapter == null) {
            newsAdapter = new MoviesAdapter(MainActivity.this, articleArrayList);
            rvHeadline.setLayoutManager(new LinearLayoutManager(this));
            rvHeadline.setAdapter(newsAdapter);
            rvHeadline.setItemAnimator(new DefaultItemAnimator());
            rvHeadline.setNestedScrollingEnabled(true);
        } else {
            newsAdapter.notifyDataSetChanged();
        }
    }
}


public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MoviesViewHolder> {
    Context context;
    ArrayList<Movies> articles;

    public MoviesAdapter(Context context, ArrayList<Movies> articles) {
        this.context = context;
        this.articles = articles;
    }

    @NonNull
    @Override
    public MoviesAdapter.MoviesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.movies_item, parent, false);
        return new MoviesViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull MoviesAdapter.MoviesViewHolder holder, int position) {
        holder.tvName.setText(articles.get(position).getTitle()); //this is working
        holder.tvDesCription.setText(articles.get(position).getDirector());
        String url = articles.get(position).getPoster();
        if(url!=null) {
           /* Glide.with(context) //passing context
                    .load(url) //passing your url to load image.
                    .dontAnimate()
                    .placeholder(context.getDrawable(R.drawable.ic_launcher_background)) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                    .centerCrop()//this method help to fit image into center of your ImageView
                    .into(holder.ivNews);*/ //pass imageView reference to appear the image.*/
            Picasso.get()
                    .load(url)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background)
                    .into(holder.ivNews);
        }
    }
    @Override
    public int getItemCount() {
        return articles.size();
    }

    public class MoviesViewHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvDesCription;
        ImageView ivNews;

        public MoviesViewHolder(@NonNull View itemView) {
            super(itemView);

            tvName = itemView.findViewById(R.id.tvName);
            tvDesCription = itemView.findViewById(R.id.tvDesCription);
            ivNews = itemView.findViewById(R.id.ivNews);

        }
    }
}

我无法弄清楚我在哪里做错了。因为来自响应的图像仅针对某些表显示,而其他图像则通过显示相同的图像进行复制,或者也没有显示。我也使用了Glide和Picasso,但仍然无法从某些列表的响应中显示图像。

1 个答案:

答案 0 :(得分:0)

适配器中的

当时同时使用if和else条件。 只需将此代码添加到您的项目中:

 if(url!=null){
        Picasso.get()
                .load(url)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_launcher_background)
                .into(holder.ivNews);
    } else {
        Picasso.get()
                .load(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_launcher_background)
                .into(holder.ivNews);
    }