0

I have a Materialise calendar which contains some event on several days. and all these events also listed in a listview. I'm trying to do that like if I have clicked on a specific date on calendar then if this date available in the listview's item, then that item comes first in the listview. Both calendar and listview are in different activity.So i'm save the clicked date and transfer it in other activity like that:

materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {

                Date dt=date.getDate();
                String str1=String.valueOf(timeZoneFormat.format(dt));
                Intent i = new Intent(MainActivity.this, CalendarList.class);
                i.putExtra("datec",str1);
                startActivity(i);
                // close this activity
                finish();
            }
        });

and get it in other activity like that:

    Intent i = getIntent();
    i.getData();
    date2 = i.getStringExtra("datec");

And my listview is display using this code:

     caladata();
        calList = new ArrayList < > ();
        adapter = new CalendarListAdapter(this, calList);
        listView.setAdapter(adapter);


 private void caladata() {

        // showing refresh animation before making http call
        swipeRefreshLayout.setRefreshing(false);

        // Volley's json array request object
        StringRequest stringRequest = new StringRequest(Request.Method.POST, CALENDAR_DATA,
                new Response.Listener < String > () {
                    @Override
                    public void onResponse(String response) {
                        //                        Log.d(TAG, response.toString());
                        //                        hidePDialog();
                        JSONObject object = null;
                        try {
                            object = new JSONObject(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        JSONArray jsonarray = null;

                        try {
                            jsonarray = object.getJSONArray("Table");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                        for (int i = 0; i < jsonarray.length(); i++) {
                            try {
                                JSONObject obj = jsonarray.getJSONObject(i);
                                Calenndar_Model movie = new Calenndar_Model();
                                String str = obj.getString("eventdate").replaceAll("\\D+","");
                                String upToNCharacters = str.substring(0, Math.min(str.length(), 13));
                                DateFormat timeZoneFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                                timeZoneFormat.setTimeZone(TimeZone.getTimeZone("GMT-8"));

                                Date time = new Date(Long.parseLong(upToNCharacters));
                                movie.setDate(String.valueOf(timeZoneFormat.format(time)));

                                movie.setColor(obj.getString("eventcolor"));

                                movie.setAutoid(obj.getString("autoid"));

//                                Toast.makeText(getApplicationContext(), "server data respone", Toast.LENGTH_LONG).show();

                                if (date2.equals(time)){

                                // adding movie to movies array
                                calList.add(0,movie);
                                   } calList.add(movie);
                                // Toast.makeText(getApplicationContext(), "server data respone", Toast.LENGTH_LONG).show();

                            } catch (JSONException e) {
                                // Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                            }

                        }



                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map < String, String > getParams() {
                Map < String, String > params = new HashMap < String, String > ();
                params.put("clientid", get1);
                return params;
            }
        };
        // Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(stringRequest);
    }

My adapter code:

   public class CalendarListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Calenndar_Model> movieList;
    private String[] bgColors;

    public CalendarListAdapter(Activity activity, List<Calenndar_Model> movieList) {
        this.activity = activity;
        this.movieList = movieList;
    }

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

    @Override
    public Object getItem(int location) {
        return movieList.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.calendar_listrow, null);

        ImageView serial = (ImageView) convertView.findViewById(R.id.serial);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView date1 = (TextView) convertView.findViewById(R.id.date1);

        title.setText(movieList.get(position).getHost());
        date1.setText(movieList.get(position).getDate());



        return convertView;
    }

}

So how to update this code to display the clicked date(item) on top of list view.

Please guide.

Adi
  • 400
  • 8
  • 25

3 Answers3

0

You can add headerView to you ListView..

View header = getLayoutInflater().inflate(R.layout.header, null);
listView.addHeaderView(header);

Finally set your adapter to your listview.

listView.setAdapter(youradapter);
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • I don't want to change in my layout, because it harm many ways.can U suggest another thing. @Wizard – Adi Jan 24 '17 at 07:04
  • Another way is to use `getitemviewtype` method of Listview. Here is the reference [http://stackoverflow.com/questions/8649705/how-to-add-a-dynamic-view-to-a-listview-item-at-runtime] – Paresh P. Jan 24 '17 at 07:09
0

I think you want to show selected date on the top of the list and the List view still need to be scroll-able.

Above code you wrote will put the selected date on top but the sorted behaviour will be compromised.

Better solution is to add the items in list as it is, then scroll to particular selected date.

In your for loop for traversing table, add below code.

selectedItemPos = 0;
if (date2.equals(time)) {
    selectedItemPos = i;
}

After refreshing the List view. Use below method to scroll to selectedPos.

getListView().smoothScrollToPositionFromTop(position,offset,duration);

Parameters

  • position : Position to scroll to (our case selectedPos)
  • offset :Desired distance in pixels of position from the top of the view when scrolling is finished
  • duration : Number of milliseconds to use for the scroll

EDIT

Part of code you need to update. Please see the updated lines in function caladata(). listview need to be final in this case.

for (int i = 0; i < jsonarray.length(); i++) {
   try {
        JSONObject obj = jsonarray.getJSONObject(i);
        Calenndar_Model movie = new Calenndar_Model();
        String str = obj.getString("eventdate").replaceAll("\\D+","");
        String upToNCharacters = str.substring(0, Math.min(str.length(), 13));
        DateFormat timeZoneFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");                                        timeZoneFormat.setTimeZone(TimeZone.getTimeZone("GMT-8"));

        Date time = new Date(Long.parseLong(upToNCharacters));
        movie.setDate(String.valueOf(timeZoneFormat.format(time)));
        movie.setColor(obj.getString("eventcolor"));

        movie.setAutoid(obj.getString("autoid"));
        // **Code updated**
        selectedPos = 0;
        if (date2.equals(time)){
             selectedItemPos = i;
        }
        // Toast.makeText(getApplicationContext(), "server data respone", Toast.LENGTH_LONG).show();

    } catch (JSONException e) {
    // Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}

adapter.notifyDataSetChanged();
 // **Code updated**
listview..smoothScrollToPositionFromTop(selectedPos,0,300);

I think it will help. Happy Coding

Sayooj O
  • 15
  • 1
  • 6
  • Listview items are not selected.,only dates are selected from calendar in first activity and according to this needs to be change in item's of listview in second activity.@Sayooj O – Adi Jan 24 '17 at 07:10
  • Understood. You just need to find the listview item position corresponding to passed date from first activity. You can use this code to do this `selectedItemPos = 0; if (date2.equals(time)){ selectedItemPos = i; }` – Sayooj O Jan 24 '17 at 07:19
  • Then what is selectedItemPos and i ? – Adi Jan 24 '17 at 08:11
  • What is selectedPos ? I have initialize it as int but now listview is empty. – Adi Jan 24 '17 at 09:43
  • add line of code `calList.add(movie);` above `selectedPos = 0`. selectPos is int itself – Sayooj O Jan 24 '17 at 10:55
  • Have you got it ? – Sayooj O Jan 24 '17 at 17:34
0

Put the below code in Adapter and call this method from adapter instance. If you not clear please paste your adapter code and let me help for you.

public void swapList(List<Object> objectList) {
        this.objectList = objectList;
        notifyDataSetChanged();
    }

Updated Adapter class:

public class CalendarListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Calenndar_Model> movieList;
    private String[] bgColors;

    public CalendarListAdapter(Activity activity, List<Calenndar_Model> movieList) {
        this.activity = activity;
        this.movieList = movieList;
    }

     public void swapList(List<Calenndar_Model> movieList) {
        this.movieList = movieList;
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int location) {
        return movieList.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.calendar_listrow, null);

        ImageView serial = (ImageView) convertView.findViewById(R.id.serial);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView date1 = (TextView) convertView.findViewById(R.id.date1);

        title.setText(movieList.get(position).getHost());
        date1.setText(movieList.get(position).getDate());



        return convertView;
    }

}

After you changed/sort in calList use this code to refresh the list.

adapter.swapList(calList);
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
  • your code only sort the list.Please read my question again,I want change only first index of list according to previous input which comes from calendar@Naveen – Adi Jan 24 '17 at 09:52
  • Then you have to use Header in ListView or ReyclerView for only changing firstrow. Refer these thinks used for recyclerview http://stackoverflow.com/questions/26530685/is-there-an-addheaderview-equivalent-for-recyclerview http://stackoverflow.com/questions/32789529/how-to-categorize-list-items-in-a-recyclerview https://www.youtube.com/watch?v=10IE3oNbck8 – Naveen Kumar M Jan 24 '17 at 10:14