从parceable类中获取数组列表项并显示在Fragment View中

时间:2016-04-05 11:49:45

标签: android listview android-fragments arraylist parcelable

我有一个实现parceable的类。我的问题是我能够从各个字段中获取值并显示在片段视图中。但是我的方法 getAnswers()无法显示此数组的字符串值。在同一片段视图中。

这是我的parceable类

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];
    }
};
}

这是我的片段类,显示视图

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);
    }
}

}

这是我对片段实例

setArguments 的地方
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();
    }


}
}

这就是我设置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);
        }
    });

}

0 个答案:

没有答案