我想用Android用户对listview进行排序

时间:2014-08-26 22:52:50

标签: android listview

我有两个类来在列表视图中显示一些数据

但我希望make选项让用户对此列表视图进行排序

这是适配器类

public class CustomListViewAdapter extends BaseAdapter {
     private Activity activity;
        private Book[] data;
        private static LayoutInflater inflater=null;
        public CustomListViewAdapter(Activity a, Book list[]) {
            activity = a;
            data=list;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getBookId(int position){
            return data[position].getId();
         }

        public int getCount() {
            return data.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            if(convertView==null)
                vi = inflater.inflate(R.layout.list_row, null);
            TextView row_id =(TextView)vi.findViewById(R.id.row_id);
            TextView name=(TextView)vi.findViewById(R.id.title);
            TextView descp = (TextView) vi.findViewById(R.id.artist);
            TextView note_type = (TextView) vi.findViewById(R.id.row_note_type);
            ImageView image=(ImageView)vi.findViewById(R.id.image);
            row_id.setText(String.valueOf(data[position].getId()));
            name.setText(data[position].getTitle());
            descp.setText(data[position].getContent());
            //row_id.setText(data[position].getId());
            note_type.setText(data[position].getType());
            //image.setImageResource(R.drawable.subway);
            if(data[position].getImage().toString().equals("Facebook")){
                image.setImageResource(R.drawable.facebook);
            }else if(data[position].getImage().toString().equals("skype")){
                image.setImageResource(R.drawable.skype);
            }else if(data[position].getImage().toString().equals("Subway")){
                image.setImageResource(R.drawable.subway);
            }else if(data[position].getImage().toString().equals("Book")){
                image.setImageResource(R.drawable.book);
            }
            return vi;
        }
}

这是书类

public class Book {
    private int id;
    private String title;
    private String content;     // this is content of table
    private String image;       // choosing images
    private String type;        // type of table ( note or task)
    private int archived;       // Archive table (true or false) 
    private int check;          // make overline when finish (true or false)
    private int protect;        // protect the table with password (true or false)
    private String password;    // password of table if it was protected
    private String date_added;

    public Book() {}

    public Book(String title, String content, String image, String type, int archived, 
                int check, int protect, String password) {
        this.title = title;
        this.content = content;
        this.image = image;
        this.type = type;
        this.archived = archived;
        this.check = check;
        this.protect = protect;
        this.password = password;
    }

    // ---- setter
    public void setId(int id){
           this.id = id;
    }

    public void setTitle(String title){
           this.title = title;
    }

    public void setContent(String content){
        this.content = content;
    }

    public void setImage(String image){
        this.image = image;
    }

    public void setType(String type){
        this.type = type;
    }

    public void setArchived(int archived){
        this.archived = archived;
    }

    public void setCheck(int check){
        this.check = check;
    }

    public void setProtect(int protect){
        this.protect = protect;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public void setDate_added(String date_added){
        this.date_added = date_added;
    }

    // --- getter ---

    public int getId(){
           return id;
    }

    public String getTitle(){
           return title;
    }

    public String getContent(){
        return content;
    }

    public String getImage(){
        return image;
    }

    public String getType(){
        return type;
    }

    public int getArchived(){
        return archived;
    }

    public int getCheck(){
        return check;
    }

    public int getProtect(){
        return protect;
    }

    public String getPassword(){
        return password;
    }

    public String getDate_added() {
        return date_added;
    }

    public String toString(){
           return "Book  >> id:"+id+" | title:"+title+" | author:";
    }
}

这是在列表视图中显示数据的方法,但BookTable是连接类并从sqlite获取数据

public void list_Books() {

        BookTable bt = new BookTable(getActivity());
        adapter = new CustomListViewAdapter(getActivity(), bt.getAllBooks());

        list.setAdapter(adapter);

        list.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent, View arg1,int pos, long id) {
                  selected = adapter.getBookId(pos);
                  row_type = (TextView) arg1.findViewById(R.id.row_note_type); 
                    return false;
                }
            });
           registerForContextMenu(list);
    }

我想知道如何通过Book类的对象

对数据进行排序

请我快速回答

1 个答案:

答案 0 :(得分:0)

一种排序方法是让对象Book实现Comparable接口:

public class Book implements Comparable<Book>{

    private int id;

    @Override
    public int compareTo(Book another) {
        if(this.id >= another.id)
            return 1;
        return -1;
    }
}

如果要在从数据库查询数据后进行排序,只需调用Collections sort()方法:

Collections.sort(list);

如果您不想实施Comparable界面,只需创建Comparator并调用Collections的以下方法:

Collections.sort(list, new Comparator<Book>() {

    @Override
    public int compare(Book lhs, Book rhs) {
        if(lhs.id >= rhs.id)
            return 1;
        return -1;
    }
});

要颠倒顺序(通常需要排序):

Collections.sort(list, Collections.reverseOrder());

对列表进行排序后,不要忘记通知适配器刷新视图:

adapter.notifyDataSetChanged();