0

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);
Community
  • 1
  • 1
  • Debug your code and make sure you didn't get null from data provider. And Check if you didn't config wrong name of data-fields – Nguyen Minh Binh Jul 10 '17 at 16:53
  • A few comments that do not answer your question but can improve your code: 1) You can use `savedInstanceState` instead of `getIntent()`. 2) You can use `this` instead of creating a `context` variable. – Code-Apprentice Jul 10 '17 at 16:54
  • As for your question, you need to learn how to use the Android Studio debugger. Set a breakpoint in the code from `MainActivity` to check that the movie data is what you expect. Then set a break point in `Child` to check that the data is received correctly. – Code-Apprentice Jul 10 '17 at 16:56
  • But if i change into savedInstanceState i cant use getStringExtra – Ramdani Jul 10 '17 at 17:01
  • I have tried debug, the position or the data are received correctly – Ramdani Jul 10 '17 at 17:03

2 Answers2

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.

Rade
  • 310
  • 2
  • 15
0

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