0

I have a class that implements parceable. My question is that i am able to get values from the individual fields and display in the Fragment View.But My method getAnswers() is unable to display the string value of this array. in that same Fragment View.

This is my parceable class

public class StoredForum implements Parcelable {
public static final String QUESTION_ID = "QUESTION_ID";
public static final String QUESTION_CLASS = "QUESTION";
public static final String FORUM_QUESTION = "forum-chat";
Long id;
Long userId;
String title;
String description;
Date questionDate = Calendar.getInstance().getTime();

List<Answer> answers;


public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public Long getUserId() {
    return userId;
}

public void setUserId(Long userId) {
    this.userId = userId;
}

public String getTitle() {
    return title;
}

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


public String getDescription() {
    return description;
}

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

public Date getQuestionDate() {

    return questionDate;
}

public void setQuestionDate(Date questionDate) {
    this.questionDate = questionDate;
}

public List<Answer> getAnswers() {
    return answers;
}

public void setAnswers(List<Answer> answers){
    this.answers = answers;
}


private StoredForum() {

}

public static StoredForum fromQuestion(Question question) {
    StoredForum storedForum = new StoredForum();
    storedForum.setId(question.getId());
    storedForum.setUserId(question.getUserId());
    storedForum.setTitle(question.getTitle());
    storedForum.setDescription(question.getDescription());
    storedForum.setQuestionDate(question.getQuestionDate());
    storedForum.setAnswers(question.getAnswers());


    return storedForum;
}

protected StoredForum(Parcel in) {
    id = in.readByte() == 0x00 ? null : in.readLong();
    userId = in.readByte() == 0x00 ? null : in.readLong();
    title = in.readString();
    description = in.readString();
    //questionDate = new Date(in.readString());
    this.questionDate = new Date(in.readLong());

    answers = new ArrayList<Answer>();
    answers = in.readArrayList(Answer.class.getClassLoader());

}




@Override
public int describeContents() {
    return 0;
}


@Override
public void writeToParcel(Parcel dest, int flags) {
    if (id == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeLong(id);
    }
    if (userId == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeLong(userId);
    }

    dest.writeString(title);

    dest.writeString(description);
    if(questionDate != null) {
        dest.writeLong(this.questionDate.getTime());
    } else {
        dest.writeString("0");
    }
    answers = new ArrayList<Answer>();
    dest.writeList(answers);
}

public static final Parcelable.Creator<StoredForum> CREATOR = new Parcelable.Creator<StoredForum>() {
    @Override
    public StoredForum createFromParcel(Parcel in) {

        return new StoredForum(in);
    }

    @Override
    public StoredForum[] newArray(int size) {

        return new StoredForum[size];
    }
};
}

This is my Fragment Class that displays the view

public class ForumDetailFragment extends Fragment {

private TextView titleTV;
private TextView timeTV;
private TextView dateTV;
private TextView detailsTV;
private LinearLayout themeLayout;
private ImageView themeIMG;
private StoredForum currentQuestion;
private Button scheduleButton;
private TextView answerTV;

SimpleDateFormat formatDate = new SimpleDateFormat("MMM-dd-yyyy");

SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm aaa");

public ForumDetailFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_forum_detail, container, false);
    currentQuestion = getArguments().getParcelable(StoredForum.QUESTION_CLASS);
    titleTV = (TextView) rootView.findViewById(R.id.titleTV);
    timeTV = (TextView) rootView.findViewById(R.id.timeTV);
    detailsTV = (TextView) rootView.findViewById(R.id.detailsTV);
    themeLayout = (LinearLayout) rootView.findViewById(R.id.eventTypeThemeLayout);
    themeIMG = (ImageView) rootView.findViewById(R.id.eventTypeThemeIMG);
    dateTV = (TextView) rootView.findViewById(R.id.dateTV);
    answerTV = (TextView) rootView.findViewById(R.id.answerTV);
    titleTV.setText(currentQuestion.getTitle());
    detailsTV.setText(currentQuestion.getDescription());
    answerTV.setText(String.valueOf(currentQuestion.getAnswers().size()));
    timeTV.setText(formatTime.format(currentQuestion.getQuestionDate()));
    dateTV.setText(formatDate.format(currentQuestion.getQuestionDate()));

    setupTheme();
    //setupButtons(rootView);


    return rootView;
}

private void setupTheme() {
    if (currentQuestion.getDescription().equals(StoredForum.FORUM_QUESTION)) {
        themeLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.pink));
        themeIMG.setImageResource(R.drawable.abc_ic_menu_copy_mtrl_am_alpha);
    } else {
        themeLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.orange));
        themeIMG.setImageResource(R.drawable.abc_ic_menu_paste_mtrl_am_alpha);
    }
}

}

This is where i have setArguments to my fragment instance

public class ForumDetail extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forum_detail);

    Intent intent = getIntent();
    //ArrayList<String> test = data.getStringArrayListExtra("test");
    StoredForum question = intent.getParcelableExtra(StoredForum.QUESTION_CLASS);
    if (savedInstanceState == null) {
        ForumDetailFragment fragment = new ForumDetailFragment();
        Bundle args = new Bundle();
        args.putParcelable(StoredForum.QUESTION_CLASS, question);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, fragment)
                .commit();
    }


}
}

This is how i set up my ForumDetail

public void setUpListView(View rootView) {
    questionListView = (ListView) rootView.findViewById(R.id.questionListView);
    adapter = new ForumAdapter(getActivity(), new ArrayList<Question>());

    questionListView.setAdapter(adapter);
    questionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Question question = (Question) adapterView.getItemAtPosition(position);
            Intent intent = new Intent(getActivity(), ForumDetail.class);
            intent.putExtra(StoredForum.QUESTION_CLASS, StoredForum.fromQuestion(question));
            getActivity().startActivity(intent);
        }
    });

}
jonathan
  • 29
  • 1
  • 11
  • @Rohit That is my ForumDetail activity that i updated earlier. – jonathan Apr 05 '16 at 12:22
  • sorry, I meant, post code for how are you creating `ForumDetail` activity. I want to see how are you setting data to intent. – Rohit Arya Apr 05 '16 at 12:25
  • It's already in the question. But you are calling `getIntent()` in the activity. I want to check how are you sending that. So post code for that. Activity creation code. – Rohit Arya Apr 05 '16 at 12:31
  • @RohitArya This is how i set it – jonathan Apr 05 '16 at 12:32
  • I have updated my question. Kindly check how i send the intent – jonathan Apr 05 '16 at 12:35
  • @RohitArya can you see my updated question? – jonathan Apr 05 '16 at 12:41
  • @RohitArya Question question = (Question) adapterView.getItemAtPosition(position); Intent intent = new Intent(getActivity(), ForumDetail.class); intent.putExtra(StoredForum.QUESTION_CLASS, StoredForum.fromQuestion(question)); getActivity().startActivity(intent); – jonathan Apr 05 '16 at 12:55

0 Answers0