0

I have a recyclerview that display news.

News is composed by name_news,image_news,time_news.

I am getting the data from mysqldatabse.

This is my adapter class:

public class PostAdapter2 extends RecyclerView.Adapter<PostAdapter2.ViewHolder>{

    public Context c;
    public FragmentManager mContext;
    public ArrayList<News_data> original_items = new ArrayList<>();
    public ArrayList<News_data> filtered_items = new ArrayList<>();

    public ArrayList<Simplenews_data> original_items2 = new ArrayList<>();
    public ArrayList<Simplenews_data> filtered_items2 = new ArrayList<>();
    //   ItemFilter mFilters = new ItemFilter();

    public PostAdapter2(Context c, ArrayList<News_data> postList) {
        this.c = c;
        this.original_items = postList;
        this.filtered_items = postList;

    }

    public PostAdapter2(FragmentManager mContext, ArrayList<Simplenews_data> postList) {
        this.mContext = mContext;
        this.original_items2 = postList;
        this.filtered_items2 = postList;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_news, parent, false);


        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        try {

            final Simplenews_data post = filtered_items2.get(position);

            PicassoClient.downloadImage(c, post.getImage_simplenews(), holder.image_news);
            holder.txt_news_title.setText(post.getName_simplenews());
            holder.txt_date.setText(post.getTime_simplenews());

            holder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick() {
                    Bundle x = new Bundle();
                    x.putString("news_title", post.getName_simplenews());
                    x.putString("news", post.getDesc_simplenews());
                    x.putString("image",post.getImage_simplenews());
                    x.putString("time",post.getTime_simplenews());
                    x.putString("date",post.getDate_simplenews());

                    Fragment descriptionFragment = new DescriptionFragment();
                    FragmentTransaction transaction = mContext.beginTransaction();
                    descriptionFragment.setArguments(x);
                    transaction.replace(R.id.framelayout, descriptionFragment).addToBackStack(null).commit();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    public int getItemCount() {
        return filtered_items2.size();
    }





    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ItemClickListener itemClickListener;
        TextView txt_news_title,txt_date;
        ImageView image_news;

        public ViewHolder(View itemView) {
            super(itemView);
            txt_news_title = (TextView) itemView.findViewById(R.id.txt_news_title);
            txt_date = (TextView) itemView.findViewById(R.id.txt_timedate);
            image_news = (ImageView) itemView.findViewById(R.id.image_news);
            itemView.setOnClickListener(this);

        }


        @Override
        public void onClick(View v) {
            try {
                this.itemClickListener.onItemClick();

            }catch (Exception e)
            {
                e.printStackTrace();
            }


        }
        public void setItemClickListener(ItemClickListener itemClickListener)
        {
            this.itemClickListener=itemClickListener;
        }
    }

}

I am using a PHP interface to add news.

The getTime_simplenews() contains the news sumbmitted time format as : 14:00

So i want to display the immediate news in Top of recyclerview not under the previous news.

Thanks a lot.

UPDATED :

public class Simplenews_data {

    int id_simplenews;
    String name_simplenews,image_simplenews,desc_simplenews,time_simplenews,date_simplenews;

    public String getDate_simplenews() {
        return date_simplenews;
    }

    public void setDate_simplenews(String date_simplenews) {
        this.date_simplenews = date_simplenews;
    }

    public String getTime_simplenews() {
        return time_simplenews;
    }

    public void setTime_simplenews(String time_simplenews) {
        this.time_simplenews = time_simplenews;
    }

    public int getId_simplenews() {
        return id_simplenews;
    }

    public void setId_simplenews(int id_simplenews) {
        this.id_simplenews = id_simplenews;
    }

    public String getName_simplenews() {
        return name_simplenews;
    }

    public void setName_simplenews(String name_simplenews) {
        this.name_simplenews = name_simplenews;
    }

    public String getImage_simplenews() {
        return image_simplenews;
    }

    public void setImage_simplenews(String image_simplenews) {
        this.image_simplenews = image_simplenews;
    }

    public String getDesc_simplenews() {
        return desc_simplenews;
    }

    public void setDesc_simplenews(String desc_simplenews) {
        this.desc_simplenews = desc_simplenews;
    }

Fragment Code:

 public void parseJson2(String response) {

        try {

            JSONArray array = new JSONArray(response);
            JSONObject jsonObject = null;
            post_array2.clear();
            Simplenews_data p;
            for (int i = 0; i < array.length(); i++) {
                jsonObject = array.getJSONObject(i);

                int id_simplenews = jsonObject.getInt("id_simplenews");
                String name_simplenews = jsonObject.getString("name_simplenews");
                String image_simplenews = jsonObject.getString("image_simplenews");
                String desc_simplenews = jsonObject.getString("desc_simplenews");
                String time_simplenews = jsonObject.getString("time_simplenews");
                String date_simplenews = jsonObject.getString("date_simplenews");



                p = new Simplenews_data();
                p.setId_simplenews(id_simplenews);
                p.setName_simplenews(name_simplenews);
                p.setImage_simplenews(image_simplenews);
                p.setDesc_simplenews(desc_simplenews);
                p.setTime_simplenews(time_simplenews);
                p.setDate_simplenews(date_simplenews);

                post_array2.add(p);

            }

        } catch (JSONException e) {
            swipeRefreshLayout.setRefreshing(false);
            e.printStackTrace();
        }
        adapter = new PostAdapter2(getFragmentManager(), post_array2);
        recycler_post.setAdapter(adapter);
        swipeRefreshLayout.setRefreshing(false);


    }
Mahdi H
  • 339
  • 8
  • 24
  • You can sort you list in which you are storing the data and recyclerview automatically will show data as per the sorting in list. For reference you can check below URL to make you Object Comparable which will make it easy for sorting your list https://stackoverflow.com/a/5927408/3136282 – Arshad May 27 '17 at 16:10
  • sort the list first before send it to adapter.. – ZeroOne May 27 '17 at 16:13
  • @ZeroOne please can u try your solution on my code – Mahdi H May 27 '17 at 16:16
  • @Arshad i saw the solution but i wish u can implement it for my code – Mahdi H May 27 '17 at 16:17
  • @MahdiHraybi share your Simplenews_data code – Arshad May 27 '17 at 16:20
  • @Arshad please see my updated question – Mahdi H May 27 '17 at 16:23
  • Possible duplicate of [Sort ArrayList of custom Objects by property](https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – OneCricketeer May 27 '17 at 16:44

1 Answers1

1

I have updated your model class and implemented Comparable to it

public class Simplenews_data implements Comparable<Simplenews_data> {

    int id_simplenews;
    String name_simplenews,image_simplenews,desc_simplenews,time_simplenews,date_simplenews;

    public String getDate_simplenews() {
        return date_simplenews;
    }

    public void setDate_simplenews(String date_simplenews) {
        this.date_simplenews = date_simplenews;
    }

    public String getTime_simplenews() {
        return time_simplenews;
    }

    public void setTime_simplenews(String time_simplenews) {
        this.time_simplenews = time_simplenews;
    }

    public int getId_simplenews() {
        return id_simplenews;
    }

    public void setId_simplenews(int id_simplenews) {
        this.id_simplenews = id_simplenews;
    }

    public String getName_simplenews() {
        return name_simplenews;
    }

    public void setName_simplenews(String name_simplenews) {
        this.name_simplenews = name_simplenews;
    }

    public String getImage_simplenews() {
        return image_simplenews;
    }

    public void setImage_simplenews(String image_simplenews) {
        this.image_simplenews = image_simplenews;
    }

    public String getDesc_simplenews() {
        return desc_simplenews;
    }

    public void setDesc_simplenews(String desc_simplenews) {
        this.desc_simplenews = desc_simplenews;
    }

    @Override
    public int compareTo(MyObject o) {
        Date newDate = formatDateTime(o.getTime_simplenews(), "HH:mm", "YYYY-MM-DD HH:mm");
        Date inputDate = formatDateTime(getTime_simplenews(), "HH:mm", "YYYY-MM-DD HH:mm");
        return inputDate.compareTo(newDate);
    }

    public Date formatDateTime(String date, String fromFormat, String toFormat) {
        Date d = null;
        try {
            d = new SimpleDateFormat(fromFormat, Locale.US).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat(toFormat, Locale.US).parse(d);
    }
}

Sort the list using below code

Collections.sort(yourList);
Arshad
  • 1,262
  • 16
  • 25
  • Thanks a lot @Arshad but where should i use Collections.sort ? I am updating the question i am posting you how do i get the data.. Please re check the updated question – Mahdi H May 27 '17 at 16:41
  • use Collections.sort just before initializing your recyclerView adapter with the list above this line "adapter = new PostAdapter2(getFragmentManager(), post_array2);" – Arshad May 27 '17 at 16:42
  • @Mahdi obviously sort before you display in the adapter. Like immediately before you create the adapter – OneCricketeer May 27 '17 at 16:43
  • Okay thanks it works @Arshad Thanks for your help but what is the difference between class to import for SimpleDateFormat(java.text) and SimpleDateFormat(andorid.icu.text) ? – Mahdi H May 27 '17 at 16:48