我无法使用Picasso ImageLoader显示图像

时间:2016-05-26 07:17:20

标签: android picasso

我正在使用recyclerview和cardview来使用Picasso Library加载一些图像。 我在这里粘贴我的代码。

这是我的DivineDestinationDistricWiseActivity。

public class DivineDestinationsDistrictWise extends Fragment {
RecyclerView all_divine_dest_places_list;
List<Places> placesList;
GridLayoutManager glm;


public DivineDestinationsDistrictWise() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_divine_destinations_district_wise, container, false);

   all_divine_dest_places_list = (RecyclerView) rootView.findViewById(R.id.all_divine_places_rv_id);
    glm = new GridLayoutManager(getActivity() , 2);
    all_divine_dest_places_list.setLayoutManager(glm);
    all_divine_dest_places_list.setHasFixedSize(true);
    intializeData();
    initializeAdapter();
    return rootView;
}

private void intializeData() {
    placesList = new ArrayList<>();
    placesList.add(new Places("Divine Destinations", R.drawable.hyd_balakampeta_ellamma));
    placesList.add(new Places("Adventure Journeys", R.drawable.hyd_birla_mandir));
    placesList.add(new Places("boatings", R.drawable.hyd_jagannatha_temple));
    placesList.add(new Places("Heritage Spots", R.drawable.hyd_mecca_masjid));
    placesList.add(new Places("Shopping", R.drawable.hyd_peddamma_temple));
    placesList.add(new Places("Nature Discovery", R.drawable.hyd_peddamma_temple));
    placesList.add(new Places("Wild Life", R.drawable.wild_life_main));

}
private void initializeAdapter()
{
    DDDWAdapter adapter =new DDDWAdapter(placesList);
    //  places_type_list_recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
    all_divine_dest_places_list.setAdapter(adapter);
}

}

这是我的适配器类。

public class DDDWAdapter extends RecyclerView.Adapter<DDDWAdapter.DivineDestViewHolder>  {
CardView cv;
Context context;



public class DivineDestViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView personName;
    ImageView photo_city;



    public DivineDestViewHolder(View itemView) {
        super(itemView);
        cv=(CardView)itemView.findViewById(R.id.cardview_places_id);
        personName=(TextView)itemView.findViewById(R.id.places_list_id_tv);
        photo_city=(ImageView)itemView.findViewById(R.id.image_places_list_id);
        context = itemView.getContext();
        photo_city.setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {

    }
}

List<Places> place;
DDDWAdapter(List<Places> place)
{

    this.place=place;
}


@Override
public DivineDestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_type_list, parent, false);
    DivineDestViewHolder pvh = new DivineDestViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
    holder.personName.setText(place.get(position).city_name);
    holder.photo_city.setImageResource(place.get(position).photo);

    //here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(R.id.image_places_list_id).into(holder.photo_city);


}

@Override
public int getItemCount() {
    return place.size();
}

}

这是我的Places课程。

  public class Places {
String city_name;
int photo;

Places(String city_name, int photo)
{
    this.city_name = city_name;
    this.photo=photo;
}

}

我试过插入如下所示如果我这样使用我得到的图像。但是当我滚动屏幕时图像滚动时会出现一些不稳定的行为。

 Picasso.with(context).load(place.get(position).photo).into(holder.photo_city);

请帮我解决这个问题......提前致谢.....

2 个答案:

答案 0 :(得分:0)

首先生成Places类的setter getter

public class Places  {
String city_name;
int photo;

public SampleClass(String city_name, int photo) {
    this.city_name = city_name;
    this.photo = photo;
}

public String getCity_name() {
    return city_name;
}

public void setCity_name(String city_name) {
    this.city_name = city_name;
}

public int getPhoto() {
    return photo;
}

public void setPhoto(int photo) {
    this.photo = photo;
}
}

然后初始化你的适配器并发送一个上下文,如

DDDWAdapter adapter =new DDDWAdapter(getActivity,placesList);

然后你的适配器看起来像这样

public class DDDWAdapter extends RecyclerView.Adapter<DDDWAdapter.DivineDestViewHolder>  {
CardView cv;
Context context;
List<Places> place;



public class DivineDestViewHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener{
TextView personName;
ImageView photo_city;



public DivineDestViewHolder(View itemView) {
    super(itemView);
    cv=(CardView)itemView.findViewById(R.id.cardview_places_id);
    personName=(TextView)itemView.findViewById(R.id.places_list_id_tv);
    photo_city=(ImageView)itemView.findViewById(R.id.image_places_list_id);
    context = itemView.getContext();
    photo_city.setOnClickListener(this);


}
@Override
public void onClick(View v) {

}
}


DDDWAdapter(Context context,List<Places> place)
{


this.context=context;
    this.place=place;

}


@Override
public DivineDestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_type_list, parent, false);
DivineDestViewHolder pvh = new DivineDestViewHolder(v);
return pvh;
}

@Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {

    holder.personName.setText(place.get(position).city_name());


//here i am trying to load images with Picasso Library but not getting Images.

Picasso.with(context).load(place.get(position).getPhoto()).into(holder.photo_city);


}

@Override

public int getItemCount() {
    return place.size();
}

答案 1 :(得分:0)

您正在将图片资源直接设置到您的imageView,然后您将分配 R.id 而不是 R.drawble 以通过Picasso加载

@Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
    holder.personName.setText(place.get(position).city_name);
    holder.photo_city.setImageResource(place.get(position).photo);

    //here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(R.id.image_places_list_id).into(holder.photo_city);

}

您需要像这样加载图片

@Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
    holder.personName.setText(place.get(position).city_name);

    //here is how you should be loading image
   Picasso.with(context).load(place.get(position).photo).into(holder.photo_city);

}