gpt4 book ai didi

Android 未排序列表

转载 作者:行者123 更新时间:2023-11-29 22:32:57 25 4
gpt4 key购买 nike

我用我自己的 ListAdapter 创建了一个 listActivity。问题是列表一旦启动就会按顺序查看。但是当我向下滚动或从另一个 Activity 返回时,listView 完全乱序。

我认为问题出在 ArrayList 但不是,列表已排序并且我确信这一点,因为当我遍历 ArrayList 中的所有元素时,它们以与插入它们相同的方式打印在日志中。

我在下面粘贴了适配器代码,以防有人想检查它。

package com.anubis.mail;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


public class EmailAdapter extends BaseAdapter {

private ArrayList<EmailModel> elements;
private Context c;

public EmailAdapter(Context c, ArrayList<EmailModel> Emails) {

this.elements = Emails;
this.c = c;
}
public int getCount() {
return elements.size();
}

public Object getItem(int position) {
return elements.get(position);
}

public long getItemId(int id) {
return id;
}

public void Remove(int id) {
notifyDataSetChanged();
}

public void Add(EmailModel email) {

this.elements.add(email);

for (EmailModel e : elements){

Log.v("EmailAdapter", e.getSubject());
}

notifyDataSetChanged();
}

public View getView(int position, View convertView, ViewGroup parent) {

LinearLayout rowLayout;
EmailModel email = elements.get(position);

if (convertView == null) {

rowLayout = (LinearLayout) LayoutInflater.from(c).inflate (R.layout.inbox_item, parent, false);

TextView subject_textview = (TextView)rowLayout.findViewById(R.id.subject_textview);
subject_textview.setText(email.getSubject());

String body_hint = " - " + email.getBodyHint();

TextView bodyhint_textview = (TextView)rowLayout.findViewById(R.id.body_hint_textview);
bodyhint_textview.setText(body_hint);


String sender_name = get_sender_name(email.getSender());

TextView sender_name_textview = (TextView)rowLayout.findViewById(R.id.sender_textview);
sender_name_textview.setText(sender_name);

TextView date_time_textview = (TextView)rowLayout.findViewById(R.id.date_time_textview);
date_time_textview.setText(email.getTime());

} else {
rowLayout = (LinearLayout) convertView;
}
return rowLayout;
}

private String get_sender_name(String from) {

String[] sender = from.split("<");
String sender_name;

try {
sender_name = sender[0];
} catch (Exception e) {
sender_name = sender[1];
}
return sender_name;
}
}

最佳答案

你需要在 IF 之后移动代码

if (convertView == null) {
rowLayout = (LinearLayout) LayoutInflater.from(c).inflate (R.layout.inbox_item, parent, false);
} else {
rowLayout = (LinearLayout) convertView;
}
TextView subject_textview = (TextView)rowLayout.findViewById(R.id.subject_textview);
subject_textview.setText(email.getSubject());

String body_hint = " - " + email.getBodyHint();

TextView bodyhint_textview = (TextView)rowLayout.findViewById(R.id.body_hint_textview);
bodyhint_textview.setText(body_hint);


String sender_name = get_sender_name(email.getSender());

TextView sender_name_textview = (TextView)rowLayout.findViewById(R.id.sender_textview);
sender_name_textview.setText(sender_name);

TextView date_time_textview = (TextView)rowLayout.findViewById(R.id.date_time_textview);
date_time_textview.setText(email.getTime());
return rowLayout;

关于Android 未排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3255226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com