如何根据某些特定输入更改列表视图的项目的顺序

时间:2017-01-31 07:32:05

标签: java android listview android-adapter android-json

我想根据与任何列表视图项内容匹配的特定输入字符串更改列表视图项的顺序,该项位于列表视图的顶部。我努力但直到现在都没有成功。如果有任何其他疑问,请询问。

以下是从服务器获取数据的代码:

这里date2是我想要比较的字符串输入

<xsl:if test="position() != last()">+</xsl:if>

这是我的适配器类:

    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();
                        }

                        Calenndar_Model movie = new Calenndar_Model();
                        for (int i = 0; i < jsonarray.length(); i++) {
                            try {
                                JSONObject obj = jsonarray.getJSONObject(i);

                                movie.setUserid(obj.getString("userid"));
                                movie.setHost(obj.getString("eventname"));

                                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));
//                                System.out.println(time);
                                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();
                                int index=calList.indexOf(date2);
                                calList.add(movie);
                                calList.remove(date2);
                                calList.add(0, movie);


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

                        }



                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
//                        listView.smoothScrollToPositionFromTop(selectedPos,0,300);

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //                VolleyLog.d(TAG, "Error: " + error.getMessage());
                //                hidePDialog();

            }
        }) {
            @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);
    }

模特课程:

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

    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;
    }

}

2 个答案:

答案 0 :(得分:0)

更新:我发现date2的使用(变量的真实姓名,而不是data2,正如您在描述中所描述的那样)。 您只需要对List<Calenndar_Model>

进行排序
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();
                    }

                    calList = new ArrayList<>();

                    for (int i = 0; i < jsonarray.length(); i++) {
                        try {
                            JSONObject obj = jsonarray.getJSONObject(i);

                            Calenndar_Model movie = new Calenndar_Model();
                            movie.setUserid(obj.getString("userid"));
                            movie.setHost(obj.getString("eventname"));

                            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));
                            //System.out.println(time);
                            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();
                            calList.add(movie);

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

                    }

                    //sort calList list 
                    Comparator<Calenndar_Model> calendarModelComparator = new Comparator<Calenndar_Model>() {
                        @Override
                        public int compare(Calenndar_Model cm1, Calenndar_Model cm2) {
                            boolean firstContainsData2 = calendarModelContainsData2(cm1);
                            boolean secondContainsData2 = calendarModelContainsData2(cm2);
                            if (firstContainsData2 && secondContainsData2) {
                                return 0;
                            } else if (firstContainsData2) {
                                return -1;
                            } else if (secondContainsData2) {
                                return 1;
                            } else return cm2.getData().compareTo(cm1.getData());
                        }

                        private boolean calendarModelContainsData2(Calenndar_Model cm) {
                             return cm.getData().contains(data2);
                        }
                    };
                    Collections.sort(calList, calendarModelComparator);


                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.swapList(calList);
                    //listView.smoothScrollToPositionFromTop(selectedPos,0,300);

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //                VolleyLog.d(TAG, "Error: " + error.getMessage());
            //                hidePDialog();

        }
    }) {
        @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);
}

答案 1 :(得分:0)

Suppose date2 is eventname you want to compare.
if(date2.equals(movie.getHost())
{
    // store Movie object on index 0, if it equals date2
    calList.add(0, movie);
}
else
{
    calList.add(movie);
}

Remove the Following Lines from caladata().
int index=calList.indexOf(date2);
calList.add(movie);
calList.remove(date2);
calList.add(0, movie);