我正在创建一个目录电影应用程序,我有细节问题,我的问题是,当我点击第一部电影,它的成功显示电影细节,然后我按回按钮,然后我点击另一部电影,但它仍然显示我点击的第一个电影细节,我看到日志,标题和任何,更新,但活动中的textview没有更新,我该如何解决这个问题?
DetailActivity
public class DetailActivity extends AppCompatActivity {
TextView txt_title,txt_desc,txt_release;
ImageView img_posterd;
private Context context;
public static List<String> LIST = new ArrayList<String>();
public static String API_KEY = "f00e74c69ff0512cf9e5bf12856******";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent i = getIntent();
String idmov = i.getStringExtra("idmovie");
String url = "https://api.themoviedb.org/3/movie/"+idmov+"?api_key="+API_KEY+"&language=en-US";
txt_title = (TextView) findViewById(R.id.tv_title_detail);
txt_desc = (TextView) findViewById(R.id.tv_desc_detail);
txt_release = (TextView) findViewById(R.id.tv_release);
img_posterd = (ImageView) findViewById(R.id.img_poster_detail);
new GetMovieTask(txt_title,txt_desc,txt_release,img_posterd).execute(url);
}
private class GetMovieTask extends AsyncTask<String, List<String>, List<String>>{
TextView txt_title,txt_desc,txt_release;
ImageView img_poster;
public GetMovieTask(TextView txt_title, TextView txt_desc, TextView txt_release,ImageView img_poster) {
this.txt_title = txt_title;
this.txt_desc = txt_desc;
this.txt_release = txt_release;
this.img_poster = img_poster;
}
@Override
protected List<String> doInBackground(String... strings) {
String title;
String desc;
String release;
String posterUrl;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}
JSONObject object = new JSONObject(builder.toString());
title = String.valueOf(object.getString("title"));
desc = String.valueOf(object.getString("overview"));
release = String.valueOf(object.getString("release_date"));
posterUrl = String.valueOf(object.getString("backdrop_path"));
String finalPos = posterUrl = POSTER_BASE_URL + "original" + posterUrl;
LIST.add(title);
LIST.add(desc);
LIST.add(release);
LIST.add(finalPos);
Log.d("titlenya", title);
Log.d("desc", desc);
Log.d("release", release);
Log.d("posterUrl", finalPos);
urlConnection.disconnect();
}catch (IOException | JSONException e){
e.printStackTrace();
}
return LIST;
}
@Override
protected void onPostExecute(List<String> result) {
super.onPostExecute(result);
String title = result.get(0);
String desc = result.get(1);
String release = result.get(2);
String poster = result.get(3);
txt_title.setText("Title : " +title);
txt_desc.setText("Description : " +desc);
txt_release.setText("Release Date : " +release);
Picasso.with(context).load(poster).into(img_posterd);
}
}
答案 0 :(得分:0)
您好请查找DetailActivity.java的更新版本
public class DetailActivity extends AppCompatActivity {
TextView txt_title, txt_desc, txt_release;
ImageView img_posterd;
public static String API_KEY = "******";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent i = getIntent();
String idmov = i.getStringExtra("idmovie");
String url = "https://api.themoviedb.org/3/movie/" + idmov + "?api_key=" + API_KEY + "&language=en-US";
txt_title = (TextView) findViewById(R.id.tv_title_detail);
txt_desc = (TextView) findViewById(R.id.tv_desc_detail);
txt_release = (TextView) findViewById(R.id.tv_release);
img_posterd = (ImageView) findViewById(R.id.img_poster_detail);
new GetMovieTask().execute(url);
}
private class GetMovieTask extends AsyncTask<String, List<String>, List<String>> {
@Override
protected List<String> doInBackground(String... strings) {
List<String> detailsList = new ArrayList<>();
String title;
String desc;
String release;
String posterUrl;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}
JSONObject object = new JSONObject(builder.toString());
title = String.valueOf(object.getString("title"));
desc = String.valueOf(object.getString("overview"));
release = String.valueOf(object.getString("release_date"));
posterUrl = String.valueOf(object.getString("backdrop_path"));
String finalPos = posterUrl = "http://image.tmdb.org/t/p/" + "original" + posterUrl;
detailsList.add(title);
detailsList.add(desc);
detailsList.add(release);
detailsList.add(finalPos);
Log.d("titlenya", title);
Log.d("desc", desc);
Log.d("release", release);
Log.d("posterUrl", finalPos);
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return detailsList;
}
@Override
protected void onPostExecute(List<String> result) {
super.onPostExecute(result);
String title = result.get(0);
String desc = result.get(1);
String release = result.get(2);
String poster = result.get(3);
txt_title.setText("Title : " + title);
txt_desc.setText("Description : " + desc);
txt_release.setText("Release Date : " + release);
Picasso.with(DetailActivity.this).load(poster).into(img_posterd);
}
}
}
您使用的是静态LIST,这就是问题的原因。你得到的详细信息如LIST.get(0),LIST.get(1).....所以由于列表是静态的,以前的数据仍然存在,你总是得到你打开的第一部电影的细节。 / p>
此外,您已将所有textview和imageview作为全局对象,因此无需将它们传递给AsyncTask的构造函数