无法在android中解决picasso的上下文

时间:2016-11-16 10:23:52

标签: android picasso android-viewholder

我有一个自定义适配器,其中包含来自json的数据,包括图像URL。问题是我在onBindViewHolder()中的上下文值上得到“无法解析符号”...

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {

    ClubFieldsModel value = Items.get(i);
    //Uri uri = Uri.parse(value.getFieldImageThumb().toString());
    viewHolder.title.setText(value.getTitle());
    viewHolder.desc.setText(value.getDesc());
    String imageUrl = value.getFieldImageThumb();

   //context "cant resolve symbol" error ...
   Picasso.with(context).load(imageUrl).into(viewHolder.field_image_thumb);

}

不明白为什么问题会存在,一切都已初始化......

编辑:完整适配器代码..

package app.sync.myapp.adapters;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.net.URI;
import java.util.List;


import app.sync.myapp.R;
import app.sync.myapp.model.ClubFieldsModel;

import static android.support.v7.appcompat.R.id.action_context_bar;
import static android.support.v7.appcompat.R.id.image;
import static app.sync.myapp.R.id.field_image_thumb;

public class ClubFieldsAdapter extends RecyclerView.Adapter<ClubFieldsAdapter.ViewHolder> {

List<ClubFieldsModel> Items;

public ClubFieldsAdapter(List<ClubFieldsModel> Items) {
    this.Items = Items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.club_fields_item, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {

    ClubFieldsModel value = Items.get(i);
    //Uri uri = Uri.parse(value.getFieldImageThumb().toString());
    viewHolder.title.setText(value.getTitle());
    viewHolder.desc.setText(value.getDesc());
    String imageUrl = value.getFieldImageThumb();

     Picasso.with(context).load(imageUrl).into(viewHolder.field_image_thumb);

}

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

class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView field_image_thumb;
    public TextView title;
    public TextView desc;

    public ViewHolder(View itemView) {

        super(itemView);
        title = (TextView) itemView.findViewById(R.id.title);
        desc = (TextView) itemView.findViewById(R.id.desc);
        field_image_thumb = (ImageView) itemView.findViewById(R.id.field_image_thumb);


        }


    }
}

3 个答案:

答案 0 :(得分:2)

您可以使用@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_your); Toolbar actionbar = (Toolbar) findViewById(R.id.actionbar); if (null != actionbar) { actionbar.setNavigationIcon(R.drawable.arrow_icon); actionbar.setTitle(R.string.title_activity); actionbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NavUtils.navigateUpFromSameTask(yourActivity.this); } }); } } ,其中itemView是您查看的viewHolder.itemView.getContext()。看看here。 E.g。

onCreateViewHolder

答案 1 :(得分:0)

Picasso - 适配器使用(ListView,GridView,...) https://futurestud.io/tutorials/picasso-adapter-use-for-listview-gridview-etc

答案 2 :(得分:0)

您需要将上下文发送到适配器的构造函数。将其更改为:

private Context context;

public ClubFieldsAdapter(List<ClubFieldsModel> Items, Context context) {
this.Items = Items;
this.context = context;
}

在调用Adapter Class时添加上下文。