2012-04-27 194 views
1

我实现了一个JSONArray来填充一个ListView。现在我想按照名称的字母顺序排列ListActivity上的项目,这是它的对象属性。 我怎样才能整理出来?如何使用ListActivity上的对象属性对JSON数组进行排序?

继承人的Object类我用它来实现ListView控件:

public class VideoLocation { 

    public String deleted_at = null; 
    public int documentary_video_length = -1; 
    public int id = -1; 
    public double latitude = 0d; 
    public double longitude = 0d; 
    public int position = -1; 
    public String updated_at = null; 
    public String name = null; 
    public String text = null; 
    public String documentary_video_url = null; 
    public String documentary_thumbnail_url = null; 
    public String audio_text_url = null; 
    public Footage[] footages = null; 

    public VideoLocation(){ 

    } 

和这里的ListActivity类:

public class ProjectsList extends ListActivity implements OnItemClickListener, VideoLocationReceiver{ 
     /** Called when the activity is first created. */ 

     private VideoLocation[] videoLocations = null; 
     private VideoLocationAdapter videoLocationAdapter = null; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.projects_list); 

      doSync(); 
      //storeSharedPrefs(); 
      ListView lv = getListView(); 
      lv.setOnItemClickListener(this); 

      videoLocationAdapter = new VideoLocationAdapter(ProjectsList.this, 
        R.layout.listitems, 
        new VideoLocation[0]); 
      lv.setAdapter(videoLocationAdapter); 

      //CREATE VideoLocation[] from Database! 

      JsonDB dbhelper = new JsonDB(ProjectsList.this); 
      SQLiteDatabase db = dbhelper.getWritableDatabase(); 

      db.beginTransaction(); 
      videoLocations = dbhelper.getVideoLocations(db); 
      db.setTransactionSuccessful();//end transaction 
      db.endTransaction(); 
      db.close(); 
     } 

     @Override 
     public void onResume(){ 
      super.onResume(); 
      DBSync.setVideoLocationReceiver(ProjectsList.this); 
     } 

     @Override 
     public void onPause(){ 
      super.onResume(); 
      DBSync.setVideoLocationReceiver(null); 
     } 

     @Override 
     public void onItemClick(AdapterView<?> l, View v, int position, long id) { 
      Intent listIntent = new Intent(this, ProjectDetailsActivity.class); 

      VideoLocation vidLocation = videoLocations[position]; 
     listIntent.putExtra("documentary_video_url",vidLocation.documentary_video_url); 
      startActivity(listIntent); 

     } 
protected void storeSharedPrefs() { 
     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     if (prefs.getBoolean("first-time", true)) { 
      doSync(); 
      prefs.edit().putBoolean("first-time", false).commit(); // record the fact that the app has been started at least once 
     } 
    } 

    void doSync() { 
     Intent serviceIntent = new Intent(this, DBSync.class); 
     startService(serviceIntent); 
    } 

    public class VideoLocationAdapter extends ArrayAdapter<VideoLocation> { 
     public ImageLoader imageLoader; 
     ImageLoader loader = null; 

     public VideoLocationAdapter(Context context, int resource, VideoLocation[] vidLocs) { 
      super(context, resource, vidLocs); 
      ProjectsList.this.videoLocations = vidLocs;  
      loader = new ImageLoader(context); 
     } 

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

      if(convertView == null){ 
       convertView = ProjectsList.this.getLayoutInflater().inflate(R.layout.listitems, null, true); 
      } 

      VideoLocation vidLocation = videoLocations[position]; 
      ImageView v = (ImageView)convertView.findViewById(R.id.image); 
      String url = vidLocation.documentary_thumbnail_url; 
      v.setTag(url); 
      loader.DisplayImage(url, ProjectsList.this, v); 
      TextView titleView = (TextView)convertView.findViewById(R.id.txt_title); 
      titleView.setText(vidLocation.name); 
      return convertView; 
     } 

     @Override 
     public int getCount(){ 
      return videoLocations==null?0:videoLocations.length; 
     } 

     @Override 
     public VideoLocation getItem(int position){ 
      return videoLocations[position]; 
     } 

     @Override 
     public boolean hasStableIds(){ 
      return true; 
     } 

     @Override 
     public boolean isEnabled(int position){ 
      return true; 
     } 

     @Override 
     public long getItemId(int position){ 
      return videoLocations[position].id; 
     } 

     public void setVideoLocationData(VideoLocation[] newData){ 
      ProjectsList.this.videoLocations = newData; 
      VideoLocationAdapter.this.notifyDataSetChanged(); 
     } 
    } 

    @Override 
    public void receivedVideoLocationData(VideoLocation[] vidLocs) { 
     final VideoLocation[] locs = vidLocs; 

     if (vidLocs==null) { 
      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 

        //show popup and inform about missing network 
       } 
      }); 
     }else{ 
      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 

        videoLocationAdapter.setVideoLocationData(locs); 
        Log.d("ProjectsList", "updating video locations..."); 
       } 
      }); 
     } 
    } 

} 

回答

4

使用Collections.sort。解析你的json并用VideoLocation的实例填充一个vector或一个ArrayList(我认为)。然后调用Collections.sort。

编辑:

Collections.sort(youarrayList, new Comparator<VideoLocation>() { 

      @Override 
      public int compare(VideoLocation lhs, VideoLocation rhs) { 
       return lhs.name.compareTo(rhs.name); 
      } 
     }); 
+0

我有它已经被解析为排序。在我的ListActivity中,我使用vidLocation.name来将字符串调用到活动中。如果你能告诉我如何在我的代码上实现它,那将是非常好的。谢谢 – hectichavana 2012-04-27 10:28:21

+0

看到我的编辑.... – Blackbelt 2012-04-27 10:33:06

+0

因为我使用一个正常的数组我决定使用数组而不是基于您的片段的集合!谢谢! – hectichavana 2012-04-27 10:43:40

0

getVideoLocations(db),你可能会告诉你的查询返回结果通过ORDER BY [专栏]

+0

不幸的是我不保存基于对象及其属性的表及其列。它只保存每个位置的整个属性作为每个列的字符串 – hectichavana 2012-04-27 10:34:56

+0

嗯然后我想@黑带的答案将帮助你在这种情况下 – waqaslam 2012-04-27 10:37:45

相关问题