android - 如何通过 ListActivity 上的对象属性对 JSON 数组进行排序?

标签 android android-listview

我实现了一个 JSONArray 来填充一个 ListView。现在我想按名称(即对象属性)的字母顺序对 ListActivity 上的项目进行排序。 我该如何解决?

这是我用来实现 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...");
                }
            });
        }
    }

}

最佳答案

使用Collections.sort .解析您的 json 并使用 VideoLocation 实例填充矢量或 ArrayList(我认为)。然后调用 Collections.sort。

编辑:

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

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

关于android - 如何通过 ListActivity 上的对象属性对 JSON 数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10348907/

相关文章:

安卓错误 :error: linker command failed with exit code 1 (use -v to see invocation) build error

java - 更改 TextView 样式 Android 应用程序

java - 在我的自定义 ListView 中添加一个复选框(一次只能选中一个)

android - 如何将 ArrayAdapter.getView() 中的 'position' 参数转换为基础数据的绝对索引?

android - 菜单中的 onOptionsItemSelected 对于使用 actionLayout 的项目是不可点击的

android - 如何从 APK 文件中提取 API 调用?

android - Android ListView OnItemClickListener中获取点击坐标

android - 如何在 Android 上的特定时间段的一日日历 View 中显示日历事件?

java - 在 Android 上的 ListFragment 中使用 ArrayAdapter 获取构造函数错误

android - Flutter Android SDK 版本 28 错误,但我使用的是 30