app_maker
app_maker

Reputation: 29

i implemented a custom comparator to sort my ArrayList, but listview is still not sorted

OK. i made a listview that can see my custom object names I put in the arraylist. I then wrote code to sort the Object List based on their startdate (sdate).

Cannot find solution to my problem.

However, I do not know how to test if my implementation is working since the listview where I see the objects is NOT being sorted, and retains the order in which I added them to the list. here is the code for reference.

Comparator:

package app.zioueche_travelexpense;

import java.util.Comparator;

public class CustomComparator implements Comparator<Claim> {
    @Override
    public int compare(Claim c1, Claim c2) {
        return c1.getSDate().compareTo(c2.getSDate());
    }
}

where getSDate is a method in object Claim that simply returns the attribute sdate assigned to it on creation by the user.

now for the sorting problem:

here is where I sort. look for the comment

package app.zioueche_travelexpense;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import android.text.InputFilter.LengthFilter;
import android.widget.Toast;

public class ClaimsList implements Serializable{

    /**
     * Claim List Serialization ID
     */
    private static final long serialVersionUID = 372301924739907840L;

    protected static ArrayList<Claim> claimList;
    protected ArrayList<Listener> listeners;

    public ClaimsList(){
        claimList = new ArrayList<Claim>();
        listeners = new ArrayList<Listener>();
    }

    public Collection<Claim> getClaim(){
        return claimList;
    }

    public void addClaim(Claim string){
        claimList.add(string);
        if (claimList.size() < 1){//SORTING HAPPENS HERE
            Collections.sort(claimList, new CustomComparator());
            notifyListeners();
        }else{
        notifyListeners();
    }}

    public void deleteClaim(Claim removeclaim){
        claimList.remove(removeclaim);
        notifyListeners();
    }

    public static boolean isEmpty(){
        return claimList.size()== 0;
    }

    public void notifyListeners(){
        for (Listener listener: listeners){
            listener.update();
        }
    }
    public void addListener(Listener l){
        listeners.add(l);
    }

    public void removeListener(Listener l){
        listeners.remove(l);

    }


}

if you need any more information let me know, but I really need to solve this problem

Here is the CLaim object code:

package app.zioueche_travelexpense;

import java.io.Serializable;
import java.util.Date;
import java.util.ArrayList;

public class Claim implements Serializable{
    /**
     * Student serialized ID
     */
    private static final long serialVersionUID = 3325687864575767244L;
    private String Name;
    private ArrayList<Expense> expenseList;
    static Date sdate;
    private Date edate;
    private String status;

    //Claim object constructor NEED TO ADD STATUS
    public Claim(String Name, Date sdate2, Date edate2){
        this.Name = Name;
        this.expenseList = new ArrayList<Expense>();
        this.sdate = sdate2;
        this.edate = edate2;
        //this.status = status;
    }

    //get the claim name
    public String getName(){
        return this.Name;
    }

    //add an expense to the claim's expense list
    public void addExpense(Expense expense){
        expenseList.add(expense);
    }

    //change the name to a string.
    public String toString(){
        return getName();
    }

    //return the status of the string
    public String getStatus(){
        return status;
    }

    //get the start date of the claim
    public Date getSDate(){
        return sdate;
    }

    //get the end date of the claim
    public Date getEDate(){
        return edate;
    }

    //change the status of the Claim.
    public void editStatus(String status){
        this.status = status;
    }

    public boolean equal(Object compareClaim){
        if (compareClaim != null && compareClaim.getClass()==this.getClass()){
            return this.equals((Claim) compareClaim);
        }else{
            return false;
        }
    }

    public int hashCode(){
        return ("Claim"+getName()).hashCode();
    }

    //return the expenses list of the Claim
    public ArrayList<Expense> getExpenses() {
        // TODO Auto-generated method stub
        return expenseList;
    }
}

and then the Controller and ClaimsList:

package app.zioueche_travelexpense;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class ClaimListController {
    private static ClaimsList claimList = null;

    static public ClaimsList getClaimList(){
        if (claimList == null){
            claimList = new ClaimsList();
        }
        return claimList;
    }

    public void addClaim(Claim claim){
        getClaimList().addClaim(claim);
    }
}

    package app.zioueche_travelexpense;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import android.text.InputFilter.LengthFilter;
import android.widget.Toast;

    public class ClaimsList implements Serial

izable{

    /**
     * Claim List Serialization ID
     */
    private static final long serialVersionUID = 372301924739907840L;

    protected static ArrayList<Claim> claimList;
    protected ArrayList<Listener> listeners;

    public ClaimsList(){
        claimList = new ArrayList<Claim>();
        listeners = new ArrayList<Listener>();
    }

    public Collection<Claim> getClaim(){
        return claimList;
    }

    public void addClaim(Claim string){
        claimList.add(string);
        if (claimList.size() > 1){
            Collections.sort(claimList, new CustomComparator());
            notifyListeners();

        }else{
        notifyListeners();
    }}

    public void deleteClaim(Claim removeclaim){
        claimList.remove(removeclaim);
        notifyListeners();
    }

    public static boolean isEmpty(){
        return claimList.size()== 0;
    }

    public void notifyListeners(){
        for (Listener listener: listeners){
            listener.update();
        }
    }
    public void addListener(Listener l){
        listeners.add(l);
    }

    public void removeListener(Listener l){
        listeners.remove(l);

    }


    }

here is the claim List adapter:

ListView listView = (ListView) findViewById(R.id.claimListView);
    Collection<Claim> claims = ClaimListController.getClaimList().getClaim();
    final ArrayList<Claim> list = new ArrayList<Claim>(claims);
    final ArrayAdapter<Claim> claimAdapter = new ArrayAdapter<Claim>(this, android.R.layout.simple_list_item_1, list);
    listView.setAdapter(claimAdapter);

Upvotes: 0

Views: 666

Answers (1)

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

You sort your list only when it has 0 size

Change this condition

if (claimList.size() < 1)

to

if (claimList.size() > 1)

Additionally you have to call the notifyDataSetChanged() method on your adapter to refresh list.

Upvotes: 1

Related Questions