Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Android ListView

I'm only starting with Android dev, and while the Milestone is a nice device Java is not my natural language and I'm struggling with both the Google docs on Android SDK, Eclipse and Java itself. Anyway...

I'm writing a microblog client for Android to go along with my Windows client (MahTweets). At the moment I've got Tweets coming and going without fail, the problem is with the UI.

The initial call will order items correctly (as in highest to lowest)

  • 3
  • 2
  • 1

When a refresh is made

  • 3
  • 2
  • 1
  • 6
  • 5
  • 4

After the tweets are processed, I'm calling adapter.notifyDataSetChanged(); Initially I thought that getItem() on the Adapter needed to be sorted (and the code below is what I ended up with), but I'm still not having any luck.

public class TweetAdapter extends BaseAdapter
{
private List<IStatusUpdate> elements;
private Context c;
public TweetAdapter(Context c, List<IStatusUpdate> Tweets)
{
    this.elements = Tweets;
    this.c = c;
}
public int getCount() {

    return elements.size();
}
public Object getItem(int position) {
    Collections.sort(elements, new IStatusUpdateComparator());
    return elements.get(position);
}
public long getItemId(int id) {
    return id;
}
public void Remove(int id)
{   
    notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) 
{
    RelativeLayout rowLayout;
    IStatusUpdate t = elements.get(position);
            rowLayout = t.GetParent().GetUI(t, parent, c);
    return rowLayout;
}

class IStatusUpdateComparator implements Comparator
{
    public int compare(Object obj1, Object obj2)
    {
        IStatusUpdate update1 = (IStatusUpdate)obj1;
        IStatusUpdate update2 = (IStatusUpdate)obj2;
        int result = update1.getID().compareTo(update2.getID());

        if (result == -1)
        return 1;
        else if (result == 1)
        return 0;
        return result;
    }
}
}

Is there a better way to go about sorting ListViews in Android, while still being able to use the LayoutInflater? (rowLayout = t.GetParent().GetUI(t, parent, c) expands the UI to the specific view which the microblog implementation can provide)

like image 862
Paul Jenkins Avatar asked Apr 14 '10 07:04

Paul Jenkins


People also ask

How to sort string array in Android listview?

This example demonstrate about How to sort string array in android listview. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml Step 3 − Add the following code to src/MainActivity.java

How to sort the newly added listview items at runtime?

You need to add DataSource.SortDescriptors again after changing ItemsSource if you want to retain sorting in listview. To sort the newly added listview items at runtime, set the SfListView.DataSource.LiveDataUpdateMode to LiveDataUpdateMode.AllowDataShaping. To learn more details about the LiveDataUpdateMode, refer to here.

How to sort data in a listview in Kotlin Android?

How to sort a simple listview in ascending and descending manner in Kotlin Android. Let’s see how to sort data in a ListView in both ascending and descendig manner. You click a button to sort in ascending, then click it again to toggle the sort into descending manner and vice versa.

How to sort the data in sflistview?

Not finding the assistance you need? The SfListView supports sorting the data either in ascending or descending order by using DataSource.SortDescriptors property and by using the custom logic. When ItemsSource changed for ListView, DataSource.SortDescriptors will be cleared by default.


1 Answers

I wouldn't recommend sorting the underlying list in getItem(), in my opinion a get..() method should be 'idempotent' and not have any externally-visible effect on its parent's state.

In the case of your problem I would tackle it at the point at which the tweets are added to the container. You haven't included this part of your app source but it seems likely you are using an ArrayList. Perhaps you are querying the Twitter API for a list of tweets ordered most recently down, and calling add() on your ArrayList for each result, which is adding the tweets to the bottom of the list.

Did you know you can insert at any point into an ArrayList? I would recommend reverse iterating through the results from the Twitter API and inserting each result at the top of your ArrayList with add(0, ....) rather than adding to the back of the list.

like image 61
Jim Blackler Avatar answered Sep 20 '22 11:09

Jim Blackler