2017-01-31 3 views
0

Ich möchte die Reihenfolge der Elemente meiner Listenansicht entsprechend einer bestimmten Eingabezeichenfolge ändern, die mit den Inhalten der Listenansichtselemente übereinstimmt und das Element oben angezeigt wird der Listenansicht. Ich habe versucht, aber bis jetzt keinen Erfolg zu bekommen. Wenn irgendeine andere Frage bitte fragen Sie.So ändern Sie die Reihenfolge der Elemente der Listenansicht gemäß bestimmter Eingaben

Hier ist der Code für die Daten vom Server holen:

Hier ist date2 String-Eingang, die ich

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

Hier ist meine Adapterklasse zu vergleichen bin versucht:

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

} 

Modell-Klasse:

public class Calenndar_Model { 
    private String host, date,userid,color,autoid; 

    //private double rating; 

public Calenndar_Model() { 
} 

public Calenndar_Model(String host,String date,String userid,String color,String autoid) { 
    this.host = host; 
    this.date = date; 
    this.userid = userid; 
    this.color = color; 
    this.autoid = autoid; 

} 

public String getHost() { 
    return host; 
} 

public void setHost(String host) { 
    this.host = host; 
} 

public String getDate() { 

    return date; 
} 
public void setDate(String date) { 

     this.date = date; 
} 

public String getUserid() { 
    return userid; 
} 

public void setUserid(String userid) { 
    this.userid = userid; 
} 

public String getColor() { 
    return color; 
} 

public void setColor(String color) { 
    this.color = color; 
} 

public String getAutoid() { 
    return autoid; 
} 

public void setAutoid(String autoid) { 
    this.autoid = autoid; 
} 

} 

Antwort

0

UPDATE: Ich fand Verwendung von date2 (der echte Name der Variablen, nicht data2 wie Sie in der Beschreibung erwähnt). Alles, was Sie brauchen, ist zu sortieren Sie Ihre 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); 
} 
+0

es zuletzt json Wert in jeder Listenansicht des TEMS @ Max.i.e zurückzukehren. Es zeigt denselben und falschen Wert in der ganzen Liste an. –

+0

Sie haben einen Fehler in Ihrem ursprünglichen Code. Diese Zeile 'Calendar_Model movie = new Calendar_Model();' muss sich innerhalb der Schleife befinden, da Sie für jede Zeile eine neue Instanz erstellen müssen, ansonsten ändern Sie einfach mehrmals die Felder desselben Objekts mit unterschiedlichen Werten. Ich habe diesen Fehler in meiner Antwort behoben. – Max

+1

Jetzt bin ich diese Zeile einfügen Calendar_Model movie = new Calendar_Model(); unter JSONObject obj = jsonarray.getJSONObject (i); (unter for loop) –

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); 
+0

Erforderlicher Wert kommt auf den Boden, aber ich brauche es an der Spitze –

Verwandte Themen