Android列表项显示包名称和@而不是字符串值

时间:2016-02-23 09:02:36

标签: android listview android-fragments android-adapter retrofit2

我实现了parceable类来将一些数据从一个活动传递到另一个活动。我设法获得了课堂上的其他项目。但是该类有一个列表对象,我希望在另一个片段中显示。我认为问题与我的适配器有关。请帮助我。我已经附加了我的片段类和我的适配器类。

片段类

public class ForumDetailFragment extends Fragment {

private TextView titleTV;
private TextView timeTV;
private TextView dateTV;
private TextView detailsTV;
private ListView answerListView;
private LinearLayout themeLayout;
private ImageView themeIMG;
private StoredForum currentQuestion;
private AnswerAdapter adapter;

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);
    answerListView = (ListView) rootView.findViewById(R.id.answerListView);
    themeLayout = (LinearLayout) rootView.findViewById(R.id.eventTypeThemeLayout);
    themeIMG = (ImageView) rootView.findViewById(R.id.eventTypeThemeIMG);
    dateTV = (TextView) rootView.findViewById(R.id.dateTV);
    titleTV.setText(currentQuestion.getTitle());
    detailsTV.setText(currentQuestion.getDescription());

    timeTV.setText(formatTime.format(currentQuestion.getQuestionDate()));
    dateTV.setText(formatDate.format(currentQuestion.getQuestionDate()));

    setupTheme();
    setUpListView(rootView);

    updateAnswer();


    return rootView;
}

public void setUpListView(View rootView) {

    answerListView = (ListView) rootView.findViewById(R.id.answerListView);
    adapter = new AnswerAdapter(getActivity(), new ArrayList<Question>());
    answerListView.setAdapter(adapter);
}

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

public void updateAnswer() {

    AuthUser user = AuthUser.getInstance(getActivity());
    Retrofit retrofit = ApiHandle.getRetrofit(user.getToken());
    QuestionService service = retrofit.create(QuestionService.class);
    service.getQuestions().enqueue(new Callback<List<com.apps233.moja.packages.forum.Question>>() {
        @Override
        public void onResponse(Response<List<com.apps233.moja.packages.forum.Question>> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                adapter.clear();
                adapter.addAll(response.body());
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onFailure(Throwable t) {

        }
    });


}

}

适配器类

public class AnswerAdapter extends ArrayAdapter<Question> {


List<Question> answers = new ArrayList<Question>();


public AnswerAdapter(Context context, List<Question> answers) {
    super(context, R.layout.item_answer, answers);
    this.answers = answers;

}

public static class ViewHolder {

    private TextView titleTV;
    private TextView descriptionTV;

    public ViewHolder(View view) {
        titleTV = (TextView) view.findViewById(R.id.titleTV);
        descriptionTV = (TextView) view.findViewById(R.id.descriptionTV);
    }
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Question question = answers.get(position);

    ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_answer, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.titleTV.setText("Doctor");

    holder.descriptionTV.setText(question.getAnswers().toString());
    return convertView;
}
}

该列表显示在以下活动中

the list is displaying the package name @ some list of numbers

问题类

public class Question {
private Long id;

private String title;

private Long userId;

private String description;

private Date questionDate;

private List<Answer> answers;

public Long getId() {
    return id;
}

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

public String getTitle() {
    return title;
}

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

public Long getUserId() {
    return userId;
}

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

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

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

    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.writeString(questionDate.toString());
    } 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 Answer {

Long id;
Long userId;
Long questionId;
String description;

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 Long getQuestionId() {
    return questionId;
}

public void setQuestionId(Long questionId) {
    this.questionId = questionId;
}

public String getDescription() {
    return description;
}

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

}

1 个答案:

答案 0 :(得分:2)

您需要在public String toString()类中实现/覆盖Answer方法。

@Override
public String toString() {
    return description;
}