0

我有一个由 JSON 文件中的对象填充的 ListView。我想根据这个对象按字母顺序对 ListView 进行排序:vidLocation.name

我怎么能做到这一点?

这是我的 ListView 类:

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

        listIntent.putExtra("spendino.de.ProjectDetail.position",position);
        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

3 回答 3

0

我看到您正在从数据库中获取数据。在 sqllite 查询中,您可以给出排序顺序。我在这里找不到您查询 db 的行。尝试这样的事情

public Cursor fetchRow(String tableName, String[] columnslist,
            String condition) throws SQLException {
        Log.i("DB Fetch arg ", condition);
        Cursor mCursor = mDb.query(true, tableName, columnslist, condition,
                null, null, null,  "_id" + " DESC",null);

        if (mCursor != null) {
            Log.i("DB", "NOT NULL "+mCursor.getCount());
            mCursor.moveToFirst();

        }
        return mCursor;

    }
于 2012-04-25T12:58:36.027 回答
0

您是否在查询中设置了 DEFAULT_SORT_ORDER?如果没有,通过这样做,您可以轻松地按字母顺序列出它。

于 2012-04-25T10:57:24.433 回答
0

使用预定义的排序技术

于 2012-04-25T11:01:17.787 回答