ListView Data Sorting Example

ListView Data Sorting Example

ListView is often used to sort the data of an item, such as the price of a commodity, sales volume, number of tickets, price, etc. And some also record entries (delete or reserve them for another use). Here is a simple implementation of the effect. The code is also shared to you.

Effect:

Description: When clicking on the first name here, it is arranged in ascending order of the selected name, when clicking on the second name, it is arranged in descending order of the selected name, and when clicking on the back, it is switched in ascending order.
Click on the "Disable" button on the right, and the text of the button becomes "Start" and the data is saved.
Click the Edit button and you can choose to clear all Disabled buttons.
Click on the "Details" button to analyze the remaining data and display the chart (the specific code of the chart can be designed by itself)

There are several more difficult knowledge involved here:

(1) Design of up and down arrows (adding pictures dynamically)

Usage: setCompound Drawables (); you can pass in four Drawable parameters to indicate that you put the image object around the top and bottom of the text. If the parameter is empty, you do not need to insert.
The code is as follows:

 //Sorting icons
    Drawable picture_up;
    Drawable picture_down;
  picture_up = getResources().getDrawable(R.drawable.up);
        picture_down = getResources().getDrawable(R.drawable.down1);
        picture_down.setBounds(0, 0, picture_down.getMinimumWidth(), picture_down.getMinimumHeight());
        picture_up.setBounds(0, 0, picture_up.getMinimumWidth(), picture_up.getMinimumHeight());

//Setting the button object (here is the picture on the right) has four parameters representing the top and bottom.
 button.setCompoundDrawables(null, null, picture_down, null);

(2) Design Ideas of Selecting "Disabled" Items

The transition of "disabled" and "enabled" text here cannot be judged by the Check state of this Button, because the reuse of View will save the state, and the state of many buttons will be confused after the list is pulled down and then up.
Here you need to use a collection to save the unique identifier of the selected item (here is the date string), add one to the data of the collection every click, delete it if it already exists, and refresh the adapter.

(3) Realization of Sorting

The method used is Collections.sort (list, class), which can be passed on to two objects, one is the collection object, and the other is the class object that implements Comparator.
The interface Comparator is used here. Comparator is a sort implemented outside the collection, located under java.lang, and is a dedicated comparator.
Here's an extension: Comparable is a sort implemented by methods defined within a collection, located under java.util. Comparable is generally used for static sorting, which will help you sort when adding, but if dynamic sorting is needed, the Comparator interface is generally used for sorting.
Comparator sorting requires two elements, one is the Bean class, the other is the class that implements the Comparator interface, and then the List collection can be sorted using the method Collections.sort.

For example, here is a simple example:

1. The Bean class of a Student

package com.xykj.comparatorTest;
public class Student {
    //Create two basic properties
    String name="";
    int age=0;

    //Used to transfer data from a write constructor
    public Student(String name, int age) {
       super();
       this.name = name;
       this.age = age;
    }
    //From the write toString method, easy to display
    @Override
    public String toString() {
       return name + "  " + age ;
    }

    //get and set methods for basic attributes
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }

}

2. Implementation class of inheritance interface

(1) Create implementation classes in ascending order of names

package com.xykj.comparatorTest; 
import java.util.Comparator;
       //Implementing interface generics in ascending order of names is a custom class with contents to be sorted.   
public class NameSort implements Comparator<Student>{
    @Override         //Two parameters are generic objects
    public int compare(Student o1, Student o2) {
                  //In ascending order of names, a minus sign is added before them and in descending order.
       return o1.getName().compareTo(o2.getName());
    }
}

(2) Create implementation classes in ascending order of age

package com.xykj.comparatorTest;
import java.util.Comparator;
//Implementing interface generics in ascending order of age is a custom class with contents to be sorted.  
public class AgeSort implements Comparator<Student>{

    @Override         //Two parameters are generic objects
    public int compare(Student o1, Student o2) {
       //In ascending order of names, a minus sign is added before them and in descending order.
       return o1.getAge()-o2.getAge();
    }
}

3. It can be used directly. Method: Collections.sort

package com.xykj.comparatorTest;
import java.util.ArrayList;
import java.util.Collections;
public class MainClass {
    /**
     * comparator Use  
     * */
    public static void main(String[] args) {
       ArrayList<Student> list = new ArrayList<>();
       list.add(new Student("1wenzhi", 18));
       list.add(new Student("3wenzhi", 19));
       list.add(new Student("2wenzhi", 33));
       list.add(new Student("66wenzhi", 10));
       list.add(new Student("4wenzhi", 18));
       System.out.println("=========Before sorting=======");
       for (Student student : list) {
           System.out.println(student);
       }

        // In ascending order of age
       Collections.sort(list, new AgeSort());
       System.out.println("=========After sorting=======");

       for (Student student : list) {
           System.out.println(student);
       }
    }
}

Of course, there are some complex logic implementations in the program.
Below is a source code for your reference (some of the class names are scribbled! But the function is achieved:
http://download.csdn.net/detail/wenzhi20102321/9793484

Below is a summary of my sorting of set data (with all set details and comparison of interface comparable s and interface comparator s):
http://blog.csdn.net/wenzhi20102321/article/details/52494402

Keywords: Java

Added by wefollow on Tue, 16 Jul 2019 04:00:22 +0300