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

标签 java android sorting hashmap

我正在使用 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/

相关文章:

java - 将两个字符串添加到 jtable 中,同时将总和添加到 jTextField 中

java - 甘力美 API : SFTP using

objective-c - "NSSet allObjects"是否随机排序?

python - 如果值相同,则对第二个变量进行排序但均匀分布

android - 自定义微调器 View (不想要弹出式微调器)

algorithm - 以递减和顺序生成笛卡尔积

java - 如何在Android中显示区域字符

java - 如何从 Firebase 存储获取 URL getDownloadURL

android - 如何去除顶部状态栏黑色背景

android - 您需要 java 6 来制作 Android 应用程序吗?