按日期和时间排序列表

时间:2018-06-25 14:37:39

标签: android

需要按日期和时间对ListView进行排序。有多个Voller请求,这两个请求都在一个ListView(Volley Second请求)上设置,如下所示(Volley请求一个)。和 仅使用一个ArrayList,(ArrayList newsItemsList;)需要使用日期和时间对列表进行排序。如何使用比较器?还是有其他方法可以做到这一点?因为所有设置在一个列表上,所以需要在适配器中排序 提前致谢。

日期格式(2018年6月16日星期六09:21:37)

ArrayList<NewsItem> newsItemsList;
 ListView list;
NewsAdaptor adapter;

MainActivity

 newsItemsList = new ArrayList<>();
 list  = (ListView)view. findViewById(R.id.listview);

RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest("https://www.express.pk/feed/", new Response.Listener <String>() {
    @Override
    public void onResponse ( String response ) {
        Document document = Jsoup.parse(response);
        Elements itemElements = document.getElementsByTag("item");
        for (int i = 0; i < itemElements.size(); i++) {
            Element item = itemElements.get(i);
            String title = rem(item.child(0).text());
            String pubDate =rem( item.child(4).text()) ;


            NewItem newitem = new NewItem();
            newsItem.setDate(item.child(3).text());
                newsItem.setTitle(item.child(2).text())
            newItemsList.add(newitem);}



   adapter = new MyAdaptor(getContext(), newItemsList);
            list.setAdapter(adapter);
  }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse ( VolleyError error ) {
    }
});
requestQueue2.add(stringRequest2);

//second Request
 RequestQueue requestQueue2 = Volley.newRequestQueue(getContext());
StringRequest stringRequest2 = new StringRequest("https://www.express.pk/feed/", new Response.Listener <String>() {
    @Override
    public void onResponse ( String response ) {
        Document document = Jsoup.parse(response);
        Elements itemElements = document.getElementsByTag("item");
        for (int i = 0; i < itemElements.size(); i++) {
            Element item = itemElements.get(i);
            String title = rem(item.child(0).text());
            String pubDate =rem( item.child(4).text()) ;


            NewItem newitem = new NewItem();
            newsItem.setDate(item.child(3).text());
                newsItem.setTitle(item.child(2).text())
            newItemsList.add(newitem);}



   adapter = new MyAdaptor(getContext(), newItemsList);
            list.setAdapter(adapter);
  }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse ( VolleyError error ) {
    }
});
requestQueue2.add(stringRequest2);

NewsItem.java

import java.io.Serializable;

public class NewsItem implements Serializable {

String title;
String date;

public String getTitle () {
    return title;
}

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



public String getDate () {
    return date;
}

public void setDate ( String date ) {
    this.date = date;
}

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

}

NewsAdapter

public class NewsAdaptor extends BaseAdapter {

Context context;
ArrayList<NewsItem> newsList;

public NewsAdaptor ( Context context, ArrayList <NewsItem> newsList ) {
    this.context = context;
    this.newsList = newsList;
}

@Override
public int getCount () {
    return newsList.size();
}

@Override
public Object getItem ( int position ) {
    return newsList.get(position);
}

@Override
public long getItemId ( int position ) {
    return 0;
}

@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
    if (convertView == null){
        convertView=View.inflate(context, R.layout.newsitemlist_layout,null);
    }
    NewsItem currentNews = newsList.get(position);

    TextView tvtitle = (TextView) convertView.findViewById(R.id.textView1id);
    TextView tvdate = (TextView) convertView.findViewById(R.id.pubDateid);

    tvtitle.setText(currentNews.title);
    tvdate.setText(currentNews.date);

    return convertView;
}

}

newsitemlist_layout.xml

     <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
   android:id="@+id/textView1id"
    android:layout_width="237dp"
    android:layout_height="83dp"
/>

<TextView
   android:id="@+id/pubDateid"
    android:layout_width="237dp"
    android:layout_height="83dp"
   />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

对列表进行排序,然后将其设置为适配器。

   private void sort(ArrayList<NewsItem> itemList) {
            Collections.sort(itemList, new Comparator<NewsItem>() {
                @Override
                public int compare(NewsItem o1, NewsItem o2) {
                    return o1.dateInMilliSec < o2.dateInMilliSec ? -1 :
                            o1.dateInMilliSec > o2.dateInMilliSec ? 1 : 0;
                }
            });
        }


public class NewsItem implements Serializable {

    String title;
    String date;
    long dateInMilliSec;

    private long getDateInMilliSec() {
        return dateInMilliSec;
    }

    private void setDateInMilliSec(long dateInMilliSec) {
        this.dateInMilliSec = dateInMilliSec;

    }




    public String getTitle() {
        return title;
    }

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


    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
        SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
        try {
            Date dateObj = sdf.parse(date);
            setDateInMilliSec(dateObj.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

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

答案 1 :(得分:0)

首先添加方法,使用以下方法将LocalDateTime从String返回到NewsItem类:

class NewsItem implements Serializable {

    String title;
    String date;

    public String getTitle () {
        return title;
    }

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

    public LocalDateTime getDateTime(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss");
        return LocalDateTime.from(formatter.parse(date));
    }

    public String getDate () {
        return date;
    }

    public void setDate ( String date ) {
        this.date = date;
    }

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

在使用以下方法将ArrayList发送到适配器之前对其进行排序:

Collections.sort(newsItemsList, (o1, o2) -> {
            return o1.getDateTime().compareTo(o2.getDateTime());
        });