如何从TMDB API获取图像



我正在安卓工作室上创建一个应用程序,在那里我填充用电影名称和图像左右滑动的卡片。名字出现在卡片上没有问题,但当我使用glide添加URL(";https://image.tmdb.org/t/p/w500/"(在图像名称("poster_path"(之前,我得到错误

"您不能在尚未附加的视图或getActivity((返回null的片段上启动加载(这通常发生在附加片段之前或销毁片段之后调用getActivity((时(">

这是因为滑翔吗?有没有其他方法可以在不滑翔的情况下做到这一点?我没有使用回收器视图来填充我正在使用的github上找到的库中的卡。我也在使用Json URL。

这是我的适配器类

package com.project300.movieswipe;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends ArrayAdapter {
private Context context;
private List<MovieModelClass> objects;
public MyAdapter(Context context, int resource, ArrayList<MovieModelClass> objects) {
super(context, resource, objects);
}

public View getView(int position, View convertView, ViewGroup parent) {

MovieModelClass movies = (MovieModelClass) getItem(position);
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
}
ImageView img = (ImageView) convertView.findViewById(R.id.image);
Glide.with(context)
.load("https://image.tmdb.org/t/p/w500/"+ objects.get(position).getImg())
.into(img);

TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(movies.getName());
return convertView ;
}
}

以及主要活动

public class GetData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
String current = "";
try {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(JSON_URL);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int data = isr.read();
while (data != -1) {
current += (char) data;
data = isr.read();
}
return current;
} catch (MalformedURLException e) {
e.printStackTrace();
;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
//  urlConnection.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
}

return current;
}
@Override
protected void onPostExecute(String s) {

try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("results");

movieList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
MovieModelClass model = new MovieModelClass();

model.setName(jsonObject1.getString("title"));
model.setImg(jsonObject1.getString("poster_path"));

movieList.add(model);
}

adapter = new MyAdapter(MainActivity.this, R.layout.item, movieList);
flingContainer.setAdapter(adapter);
adapter.notifyDataSetChanged();

} catch (JSONException e) {
e.printStackTrace();
}

}
}

型号级

package com.project300.movieswipe;
import android.media.Image;
import android.widget.ImageView;
public class MovieModelClass {
String name;
String img;
public MovieModelClass(String name, String img) {
this.name = name;
this.img = img;
}
public MovieModelClass() {
}

public void setName(String name) {
this.name = name;
}
public void setImg(String img) {
this.img = img;
}

public String getName() {
return name;
}
public String getImg() {
return img;
}

}

我认为这是因为MyAdapter中的上下文为null,所以您需要根据构造函数设置上下文,如下所示:

public MyAdapter(Context context, int resource, ArrayList<MovieModelClass> objects) {
super(context, resource, objects);
this.context = context;
this.objects = objects;
}

相关内容

  • 没有找到相关文章

最新更新