1

I have a custom list and iam trying to sorting the list view by alphabetically but get wrong every time.

i tried by compare but its didn't work with me.

    Context mContext;

    private Activity activity;
    private LayoutInflater inflater;
    private List<Movie> movieItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<Movie> movieItems) {
        mContext = activity;
        this.activity = activity;
        this.movieItems = movieItems;
    }

    @Override
    public int getCount() {
        return movieItems.size();
    }

    @Override
    public Object getItem(int location) {
        return movieItems.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)convertView = inflater.inflate(R.layout.list_row, null);
        if (imageLoader == null)imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView rating = (TextView) convertView.findViewById(R.id.rating);
        TextView genre = (TextView) convertView.findViewById(R.id.genre);
        TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
        // getting movie data for the row
        final Movie m = movieItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);


        // title
        title.setText(m.getTitle());
        title.setTextColor(Color.parseColor("#0b6887"));
        // rating
        rating.setText("Rating: " + String.valueOf(m.getRating()));
        rating.setTextColor(Color.parseColor("#66890e"));

        // genre
        String genreStr = "";
        for (String str : m.getGenre()) {
            genreStr += str + ", ";
        }
        genreStr = genreStr.length() > 0 ? genreStr.substring(0,
                genreStr.length() - 2) : genreStr;
        genre.setText(genreStr);



        // release year
        year.setText(String.valueOf(m.getYear()));
        year.setTextColor(Color.parseColor("#2c85cb"));

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(mContext, Show.class);
                intent.putExtra("rank",m.getTitle());
                intent.putExtra("img",m.getThumbnailUrl());
                intent.putStringArrayListExtra("eps", m.getep());
                intent.putStringArrayListExtra("typ", m.getGenre());            
                mContext.startActivity(new Intent(intent));
            }
        });

        return convertView;
    }

}

Thanks All...........................................

1
  • 1
    Where is the compare function ? Commented Aug 7, 2014 at 22:31

2 Answers 2

1

With this you can sort you listView after you call new CustomListAdapter in your activity class

Collections.sort(movieItems, new Comparator<User>() {
            @Override
            public int compare(Movie m1, Movie m2) {
                 return (int) m1.getTitle().charAt(0) - (int) m2.getTitle().charAt(0);
            }
        });
Sign up to request clarification or add additional context in comments.

5 Comments

movieList.clear(); listView = (ListView) findViewById(R.id.list); adapter = new CustomListAdapter(this, movieList); Collections.sort(movieList, new Comparator<Movie>() { @Override public int compare(Movie m1, Movie m2) { return (int) m1.getTitle().charAt(0) - (int) m2.getTitle().charAt(0); } }); listView.setAdapter(adapter);
you should call listView.setAdapter(adapter); before Collections.sort and then call Collections.sort with adapter.notifyDataSetChanged();
You are calling movieList.clear();, that will clear all movies from movieList ... remove it :)
worked but there is one problem. when start loading wont sort but when i refresh it sorting and if i remove movieList.clear() it will add more items to listview and the the saved items will be sort only.
worked thanks problem was i should add it after movieList.add(movie);
0

Your Movie class must implement Comparable. You should get smth like this

In Movie Class:

class Movie implements Comparable<Movie> {
    private String title; //that's yours title

    // Some other code 

    @Override
    public int compareTo(Movie another) {
        return this.title.compareTo(another.title);
    }
}

And in CustomListAdapter:

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
    Collections.sort(movieItems);
    this.movieItems = movieItems;
}

3 Comments

please post your Movie class listing including comparable implementation
omg, man. My field name was just for primer. Use your corresponding field title. I changed an example so it would be obvious for you. Don't follow exactly what people say to you. Try to understand how it works and change it in appropriate way :)
i added this but still not working look after change : textuploader.com/k3hx textuploader.com/k3hg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.