主要课程:
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
recyclerView.setLayoutManager(layoutManager);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.2/littlezebra/scripts/")
.addConverterFactory(GsonConverterFactory.create())
.build();
login service = retrofit.create(login.class);
Call<dashboard> call = service.dash();
call.enqueue(new Callback<dashboard>() {
@Override
public void onResponse(Call<dashboard> call, Response<dashboard> response) {
dashboard dashboard = response.body();
data = new ArrayList<>(Arrays.asList(dashboard. getDashboard_info()));
adapter = new DataAdapter(data,context);
recyclerView.setAdapter(adapter);
}
适配器类:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<dashboard.Dashboard_info> android;
private Context context;
public DataAdapter(ArrayList<dashboard.Dashboard_info> android,Context context) {
this.android = android;
this.context = context;
}
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(android.get(i).getId());
Picasso.with(context).load(android.get(i)
.getWeek_image())
.resize(250,200) //to resize Image
.into(viewHolder.img_android);
viewHolder.tv_api_level.setText(android.get(i).getWeek_name());
}
@Override
public int getItemCount() {
return android.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name;
private ImageView img_android;
private TextView tv_api_level;
public ViewHolder(View view) {
super(view);
tv_name = (TextView)view.findViewById(R.id.tv_name);
img_android = (ImageView) view.findViewById(R.id.img_android);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
}
}
过程:
com.example.admin.myappl,PID:5047 java.lang.IllegalArgumentException:Context不能为null。在com.squareup.picasso.Picasso $ Builder。(Picasso.java:701)com.squareup.picasso.Picasso.with(Picasso.java:662)at com.example.admin.myappl.Adapter.DataAdapter.onBindViewHolder( DataAdapter.java:37)在com.example.admin.myappl.Adapter.DataAdapter.onBindViewHolder(DataAdapter.java:18)在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6400)在android .support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6433)at android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5377)at android.support.v7.widget.RecyclerView android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5482)在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java)上的$ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5640) :5478)在android.support.v7.widget.Grid的android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2215)在Android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)的android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502)上的LayoutManager.layoutChunk(GridLayoutManager.java:556)位于android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3625)的android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView)中的.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170) .java:3354)在android.view.View.layout(ViewGroup)的android.view.View.layout(View.java:17838)的android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3886)。 java:5754)在android.view.View.layout(ViewGroup.java:5754)的android.view.View.layout(View.java:17838)的android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)在android.support.design.widget.ViewOffsetBehavior.on上的android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131) LayoutChild(ViewOffsetBehavior.java:42)在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1389)在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:868)在android .view.View.layout(View.java:17838)在android.view.Layout.onLayout(DrawerLayout.java:1192)的android.view.ViewGroup.layout(ViewGroup.java:5754)。在Android.widget.FrameLayout.onLayout的android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)的android.view.ViewGroup.layout(ViewGroup.java:5754)的view.View.layout(View.java:17838) (FrameLayout.java:321)在android.view.Loutout.setChildFrame的android.view.View.layout(ViewGroup.java:5754)上的android.view.View.layout(View.java:17838)(LinearLayout.java: 1982年在android.widget.LinearLayout.onLayout(LinearLayout.java:1735)的android.widget.LinealLayout.onoutVertical(LinearLayout.java:1835)安卓android.view.View.layout(View.java:17838)。 view.Vi ewGroup.layout(ViewGroup.java:5754)在Android.widget.FrameLayout.layout中的android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)处于android.view.View.layout(ViewLayout.java:321) .java:17838)在Android.widget.LinearLayout.setoutVertical(LinearLayout.java:1826)的android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)的android.view.ViewGroup.layout(ViewGroup.java:5754)在android.widget.Linout.onLayout(LinearLayout.java:1735)的android.view.Viewout上的android.view.View.layout(View.java:17838)处于android.widget上。在android.view.View的com.android.internal.policy.DecorView.onLayout(DecorView.java:751)的android.widget.FrameLayout.onLayout(FrameLayout.java:321)中的FrameLayout.layoutChildren(FrameLayout.java:383) .layout(View.java:17838)08-16 17:01:40.928 5047-5047 / com.example.admin.myappl E / AndroidRuntime:在Android的android.view.ViewGroup.layout(ViewGroup.java:5754)。 view.ViewRootImpl.performLayout(ViewRootImpl.java:2722)
在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1473)的android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6976)上的android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2395)
答案 0 :(得分:0)
请
也许问题出在这里: Picasso.with(上下文).load( “http://i.imgur.com/DvpvklR.png”)代入(ImageView的);
答案 1 :(得分:0)
如错误所示,您将null
上下文对象传递给Picasso.with()
方法。
要解决此问题,您可以在onBindViewHolder()
方法中执行类似操作:
//inside onBindViewHolder ()
Picasso.with(holder.itemView.getContext()).load("url here").into(yourImageView);
因此,您可以看到,您甚至不需要在构造函数中将上下文传递给适配器。
答案 2 :(得分:0)
将 adapter = new DataAdapter(data,context);
行更改为adapter = new DataAdapter(data,YourActivity.this);
然后,转到适配器类,
并粘贴
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<dashboard.Dashboard_info> android;
private Activity activity;
public DataAdapter(ArrayList<dashboard.Dashboard_info> android,Activity activity) {
this.android = android;
this.activity = activity;
}
现在使用毕加索
Picasso.with(activity).load(android.get(i)
.getWeek_image())
.resize(250,200)
.into(viewHolder.img_android);