How do I setText in textView with data I took from

2019-08-24 10:34发布

问题:

I have tried to set it with normal setText method but the value are null and the textView are set to blank no word on it,I took the json from API and here's my code -

public class Child extends AppCompatActivity {

TextView mTitle;
TextView mDescription;
TextView mReleased;
ImageView mCover;
TextView mRating;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child);

    GsonBuilder gsonBuilder = new GsonBuilder();
    String Obj = getIntent().getStringExtra("movie");
    Movie data = gsonBuilder.create().fromJson(Obj, Movie.class);

    mTitle = (TextView) findViewById(R.id.title);
    mTitle.setText(data.getTitle());
    mDescription = (TextView) findViewById(R.id.description);
    mDescription.setText(data.getDescription());
    mReleased = (TextView) findViewById(R.id.released);
    mReleased.setText(data.getReleased());
    mCover = (ImageView) findViewById(R.id.cover);
    mRating = (TextView) findViewById(R.id.rating);
    mRating.setText(data.getRating());
}}

And this is Movie class -

public class Movie {
public String TMDB_IMAGE_PATH = "http://image.tmdb.org/t/p/w500";

private String title;

@SerializedName("poster_path")
private String poster;

@SerializedName("overview")
private String description;

@SerializedName("backdrop_path")
private String released;

private String rating;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getPoster() {
    return TMDB_IMAGE_PATH + poster;
}

public void setPoster(String poster) {
    this.poster = poster;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getReleased() {
    return released;
}

public void setReleased(String released) {
    this.released = released;
}

public String getRating() {
    return rating;
}

public void setRating(String rating) {
    this.rating = rating;
}

public static class result {
    private List<Movie> results;

    public List<Movie> getResults() {
        return results;
    }
}}

And last this is the detail in MainActivity -

 Movie data = movies.get(position);
                    final Context context = view.getContext();
                    Intent intent = new Intent(context, Child.class);
                    GsonBuilder gsonBuilder = new GsonBuilder();
                    intent.putExtra("movie", gsonBuilder.create().toJson(data, Movie.class));

                    context.startActivity(intent);

回答1:

Forget about converting Object into JSON and back. Instead, implement Parcelable interface by Movie.class, and send it as object between activities, for example. This is faster, cleaner and the way it should be done.

You can find AndroidStudio plugins for Parcelable. Follow this tutorial.



回答2:

Could you try these:

  1. Cast to Movie Movie data = (Movie) gsonBuilder.create().fromJson(Obj, Movie.class);
  2. Make sure the data received is as expected -> Print the String Obj . Logcat.d("MOVIE APP", Obj); & show me the result