Filter and display the data in a listView or recyclerview

1

In my main activity I have a button that when pressed calls an API-REST to consume a Json with the weather forecast of a city for the next 5 days, this prediction is saved in an ArrayList of Prediction objects.

The prediction is shown in ListView , each day of the prediction is an item in the list and contains: a date, an icon, a maximum and a minimum temperature. I extract these data, in my ListAdapter, from the ArrayList that I loaded with the call to the API-RES

I have to create a filter to show the days the user wants, for example only next 2 days or 3 days. Some idea of how to filter, I've already tried using the JodaTime library and comparing the date field of the ArrayList with the current date but it does not work.

public class Prediccion{
int tmpMax;
int tmpMin;
int diaMilliSecond;
}

This is my adapter

    public class PrediccionListAdapter extends BaseAdapter {

        private Context mContext;
        private List<Prediccion> mForescastList;

        @BindView(R.id.txt_forescast_day)
        TextView mForecastDate;
        @BindView(R.id.txt_temp_max)
        TextView mForecastTempMax;
        @BindView(R.id.txt_temp_min)
        TextView mForecastTempMin;
        @BindView(R.id.txt_sky_is)
        TextView mSkyConditions;
        @BindView(R.id.image_forecast_city_icon)
        ImageView mWeatherIcon;

        public PrediccionListAdapter(Context context, List<Prediccion> mList) {
            mContext = context;
            mForescastList = mList;
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            JodaTimeAndroid.init(mContext);
            DateTime dateTime = new DateTime();
           //quiero filtrar y mostrar solo 3 días de los 5 que tengo
            Long endForescastDay = dateTime.plusDays(3).getMillis();

       //esto no funciona, sigue mostrando la lista entera
       if (mForescastList.get(position).getdiaMilliSecond < endForescastDay){
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.item_forescast_city, parent, false);
            } else {
                view = convertView;
            }

            //Seteo todos los datos

         }
            return view;
        }

}

Would it be more efficient (and easy) in this case to implement a recyclerview to filter ?. Thanks in advance

    
asked by JoCuTo 21.09.2016 в 20:15
source

2 answers

0

A very basic example to filter a list that meets a condition:

 int count = 0;
    ArrayList<String> _list;//ur arraylist with names
    for (String names : _list) {
        if (names.startsWith("R")) {
            count++;
        }

    }
    System.out.println(count);
    
answered by 08.12.2016 / 14:09
source
0

I would do the filter before calling PrediccionListAdapter (Context context, List mList). Having loaded everything in mList you can program a filter that is in charge of selecting the data that fulfill the condition you want with the variable you want (tmpMax, tmpMin, diaMilliSecond). Then as I said, you pass it to the adapter.

    
answered by 21.09.2016 в 20:54