gpt4 book ai didi

java - 基于 arraylist 的排序索引的 Hashmap 排序

转载 作者:行者123 更新时间:2023-12-02 02:18:18 25 4
gpt4 key购买 nike

我正在使用 firebase 数据库。并从 firebase 获取一些数据。我想在 ListView 中显示数据。来自 firebase 的文档包括 3 个字段。每个字段包括日期、标题和描述。我想按日期的降序显示数据。

我刚刚调用了 Collection.sort(datelist) 但只对日期进行了排序。其余的保持随机。


private void eventforloop() {
for (z = 0; z < jas1.length; z++) {
jas1[z] = jas1[z].trim();

DocumentReference data = db.collection("schools").document(Sname).collection("anouncements").document(jas1[z]);
data.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
adate = String.valueOf(documentSnapshot.get("date")).trim();
adescr = String.valueOf(documentSnapshot.get("desc")).trim();
aheaing = String.valueOf(documentSnapshot.get("heading")).trim();

datelist.add(adate);
desclist.add(adescr);
headinglist.add(aheaing);

newEvent();
}
});
}
}

public void newEvent() {
for (int p = 0; p < datelist.size(); p++) {
customClass = new CustomClass();
customClass.setAdate(getDate(adate));
customClass.setAdescr(adescr);
customClass.setAheaing(aheaing);

ArrayList<CustomClass> newList = new ArrayList<CustomClass>();
newList.add(customClass);

Collections.sort(newList, new Comparator<CustomClass>() {
@Override
public int compare(CustomClass o1, CustomClass o2) {
return o1.getAdate().compareTo(o2.getAdate());
}
});

UsersAdapter usersAdapter = new UsersAdapter(getApplicationContext(), newList);
anouncement_list.setAdapter(usersAdapter);
progressBar.setVisibility(View.INVISIBLE);
}
}

public class UsersAdapter extends ArrayAdapter<CustomClass> {
public UsersAdapter(Context context, ArrayList<CustomClass> customClass) {
super(context, 0, customClass);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position

// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.anouncementxml1, parent, false);
}

// Lookup view for data population
TextView tDate = convertView.findViewById(R.id.anndate);
TextView theading = convertView.findViewById(R.id.annheading);
JustifiedTextView tdesc = convertView.findViewById(R.id.anndescri);
CustomClass customClass = getItem(position);

android.text.format.DateFormat df = new android.text.format.DateFormat();
tDate.setText(df.format("dd-MM-yyyy",customClass.adate).toString());
theading.setText(customClass.aheaing);
tdesc.setText(customClass.adescr);
return convertView;
}
}

private Date getDate(String yooo) {
SimpleDateFormat spf = new SimpleDateFormat("dd-MMM-yyyy");
Date newDate = null;
try {
newDate = spf.parse(yooo);
} catch (ParseException e) {
e.printStackTrace();
}

return newDate;
}

最佳答案

在所有评论后进行编辑

ArrayList<CustomClass> newList = new ArrayList<CustomClass>();

private void eventforloop() {
final UsersAdapter usersAdapter = new UsersAdapter(getApplicationContext(), newList);
anouncement_list.setAdapter(usersAdapter);
progressBar.setVisibility(View.INVISIBLE);

for (z = 0; z < jas1.length; z++) {
jas1[z] = jas1[z].trim();

DocumentReference data = db.collection("schools").document(Sname).collection("anouncements").document(jas1[z]);
data.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
String adate = String.valueOf(documentSnapshot.get("date")).trim();
String adescr = String.valueOf(documentSnapshot.get("desc")).trim();
String aheaing = String.valueOf(documentSnapshot.get("heading")).trim();

CustomClass customClass = new CustomClass();
customClass.setAdate(getDate(adate));
customClass.setAdescr(adescr);
customClass.setAheaing(aheaing);

newList.add(customClass);
Collections.sort(newList, new Comparator<CustomClass>() {
@Override
public int compare(CustomClass o1, CustomClass o2) {
return o1.getAdate().compareTo(o2.getAdate());
}
});

usersAdapter.notifyDataSetChanged();
}
});

}

}

public class UsersAdapter extends ArrayAdapter<CustomClass> {
public UsersAdapter(Context context, ArrayList<CustomClass> customClass) {
super(context, 0, customClass);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position

// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.anouncementxml1, parent, false);
}

// Lookup view for data population
TextView tDate = convertView.findViewById(R.id.anndate);
TextView theading = convertView.findViewById(R.id.annheading);
JustifiedTextView tdesc = convertView.findViewById(R.id.anndescri);
CustomClass customClass = getItem(position);

android.text.format.DateFormat df = new android.text.format.DateFormat();
tDate.setText(df.format("dd-MM-yyyy",customClass.adate).toString());
theading.setText(customClass.aheaing);
tdesc.setText(customClass.adescr);
return convertView;
}
}

private Date getDate(String yooo) {
SimpleDateFormat spf = new SimpleDateFormat("dd-MMM-yyyy");
Date newDate = null;
try {
newDate = spf.parse(yooo);
} catch (ParseException e) {
e.printStackTrace();
}

return newDate;
}

关于java - 基于 arraylist 的排序索引的 Hashmap 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57289109/

25 4 0