我已经阅读了几篇有类似问题的帖子,但我还是无法修复。 我正在尝试构建的应用程序应该从链接中检索图像并显示它们。这是通过Picasso完成的,并且应该以GridView格式显示。
这是我的MovieAdapter.java:
package com.example.android.popularmovies.utilities;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.example.android.popularmovies.R;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by Jonathan on 6/18/2017.
*/
public class MovieAdapter extends ArrayAdapter<String>{
Context mContext;
int mLayoutResourceId;
ArrayList<String> mData = new ArrayList<>();
public MovieAdapter(Context context, int layoutResourceId,
ArrayList<String> data) {
super(context, layoutResourceId, data);
mLayoutResourceId = layoutResourceId;
mContext = context;
mData = data;
}
public void setData(ArrayList<String> mGridData) {
mData=mGridData;
notifyDataSetChanged();
}
//TODO code is working. Posters arent displayed :(
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(mLayoutResourceId, parent, false);
ImageView image = (ImageView) convertView.findViewById(R.id.iv_moviePoster);
convertView.setTag(image);
try {
String poster = NetworkUtils.posterBuilder(mData.get(position));
Picasso.with(mContext).
load(poster).
into(image);
}
catch(NullPointerException e) { e.printStackTrace(); }
return image;
}
@Override
public int getCount() {
return mData.size();
}
@Nullable
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
运行代码时,不显示任何内容。请帮忙,谢谢
更新 activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/iv_moviePoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gv_movieData"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
<ProgressBar
android:id="@+id/pb_loadingBar"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible"/>
</LinearLayout>
权限:
<uses-permission android:name="android.permission.INTERNET"/>