Android 列表项显示包名称和 @ 而不是字符串值 [英] Android List item displaying package name and @ instead of string value

查看:70
本文介绍了Android 列表项显示包名称和 @ 而不是字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了 parceable 类以将一些数据从一个活动传递到另一个活动.我设法拿到了班上的其余物品.但是那个类有一个列表对象,我想在另一个片段中显示.我认为问题与我的适配器有关.请帮帮我.我还附上了我的片段类和我的适配器类.

I implemented parceable class to pass some data from one activity to the other. I managed to get the rest of the items in the class. But that class has a list object i want to display in another fragment. i think the problem has to do with my adapter. kindly help me out. i have attached my fragment class and my adapter class as well.

片段类

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 displayed in the activity below

列表显示包名称@一些数字列表

问题类

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 类

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

}

推荐答案

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

You need to implement/override public String toString() method in Answer class.

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

这篇关于Android 列表项显示包名称和 @ 而不是字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆