【发布时间】:2017-08-27 03:59:50
【问题描述】:
我正在创建一个目录电影应用程序,我在 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);
}
}
【问题讨论】:
-
请记录您在附加项中获得的 idmov,检查它是否是正确的值。
-
再次检查我的问题,我已经添加了图片
-
你有什么异常吗?以防万一您在 logcat 中添加了任何过滤器,请将其删除并再次观察
-
不,我没有得到任何异常
标签: android json android-asynctask textview