views:

1054

answers:

1

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)

+3  A: 

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.

Jim Blackler
Yeah, you're right, I'm using an ArrayList. As for having it in the getItem(), again, you're right, wasn't planning on putting it there but I'm somewhat confused with Android ;) The problem with inserting at any point (or at zero) is when you encounter multiple accounts (or multiple plugins). A call from AccountA may occur after AccountB while AccountA's updates are all /after/ AccountB's (hopefully that made sense).A quick fix would never allow two accounts visible at once, which I'd rather not do.Of course, could do the sorting at insertion I guess?
aeoth
The main principal I recommend is to sort the underlying data model, not try to change the sort order at the ListAdapter level. It *may* be possible to sort at that level but since you're having difficulty getting that to work I'd go with the simpler approach. Regarding the multiple plugins issue, that sounds quite complex for a mobile app. I'm not sure if you're describing a Java access concurrency issue or a logical paradox with the data you're dealing with. If it's the former there are ways around that (concurrency safe containers). The latter, I would add my own sort logic after ...
Jim Blackler
.. the list was modified.
Jim Blackler
Thanks Jim, I've got it working by moving it into the insert logic to the array.Coming from WPF/XAML, I'm using to being able to sort on the UI markup itself. It's a poor excuse, I know.
aeoth