Java 如何按降序排列ArrayList

Java 如何按降序排列ArrayList,java,android,Java,Android,好的,我有一个电影列表,在那里我有一些图片,标题,收视率,类型和年份,在列表视图中的每一项。现在我试着按名字、等级和年份对这些电影进行分类。我跟随了,但我在这里结巴了: @Override public void onClick(View view) { if(view.getTag().equals(TAG_SORT_NAME)){ adapter.getItem(); } if(view.getTag().equals(TAG_SORT_RATING

好的,我有一个电影列表,在那里我有一些图片,标题,收视率,类型和年份,在列表视图中的每一项。现在我试着按名字、等级和年份对这些电影进行分类。我跟随了,但我在这里结巴了:

@Override
public void onClick(View view) {
    if(view.getTag().equals(TAG_SORT_NAME)){
        adapter.getItem();
    }

    if(view.getTag().equals(TAG_SORT_RATING)){

    }

    if(view.getTag().equals(TAG_SORT_YEAR)){

    }

}
我不知道我应该为getItem传递什么,在他的教程中,他使用的是片段,而我没有。以下是我的活动:

    public class ListaPreporuka extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener
, SortListener{

    // Log tag
    private static final String TAG = ListaPreporuka.class.getSimpleName();

    // Movies json url
    private static final String url = "http://www.nadji-ekipu.org/wp-content/uploads/2015/07/movies.txt";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private CustomListAdapter adapter;
    private static final String TAG_SORT_NAME = "sortName";
    private static final String TAG_SORT_RATING = "sortRating";
    private static final String TAG_SORT_YEAR = "sortYear";
    private static String Year = "year";
    private static String Rating = "rating";
    private static String Title = "title";
    private static String bitmap = "thumbnailUrl";
    private static String opis = "opis";
    private static String urlMovie = "url";
    private MediaPlayer mp_off;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lista_preporuka);

        // Toolbabr settings
        getSupportActionBar().setDisplayShowHomeEnabled(true);   
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setLogo(R.drawable.ic_horor_filmovi_ikonica);

        Intent newActivity2=new Intent();
        setResult(RESULT_OK, newActivity2);

        mp_off = MediaPlayer.create(this, R.raw.button_click_off);
        final MediaPlayer mp_on = MediaPlayer.create(this, R.raw.button_click_on);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setIcon(R.drawable.ic_horor_filmovi_ikonica);
        pDialog.setMessage("Učitavanje...");
        pDialog.setCancelable(false);
        pDialog.show();

        listView = (ListView) findViewById(R.id.list);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(this);

        /**
         * Showing Swipe Refresh animation on activity create
         * As animation won't start on onCreate, post runnable is used
         */
        swipeRefreshLayout.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        swipeRefreshLayout.setRefreshing(true);
                                        fetchMovies();
                                    }
                                }
        );

        if (AppStatus.getInstance(this).isOnline()) {

            Log.v("Home", "############################You are online!!!!");  

        } else {
            setContentView(R.layout.no_connection);
            Toast t = Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT);
            t.show();
            Log.v("Home", "############################You are not online!!!!");    
        }


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String name = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();

                String opisFilma = ((TextView) view.findViewById(R.id.opis))
                        .getText().toString();

                String urlFilm = ((TextView) view.findViewById(R.id.url))
                        .getText().toString();

                String ocena = String.valueOf(movieList.get(position).getRating());

                String godina = String.valueOf(movieList.get(position).getYear());

                bitmap = ((Movie) movieList.get(position)).getThumbnailUrl();

                Intent intent = new Intent(ListaPreporuka.this, MoviesSingleActivity.class);
                intent.putExtra(Title, name);
                intent.putExtra(opis, opisFilma);
                intent.putExtra("images", bitmap);
                intent.putExtra(Rating, ocena);
                intent.putExtra(Year, godina);
                intent.putExtra(urlMovie, urlFilm);
                mp_on.start();
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
            }
        });

        buildFAB();

    }

    private void buildFAB(){
        // Declare icon for FAB
        ImageView icon = new ImageView(this);
        icon.setImageResource(R.drawable.ic_halloween);

        // Build FAB
        FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
                .setContentView(icon)
                .build();

        // Declare icons for SubAction Buttons
        ImageView iconSortName = new ImageView(this);
        iconSortName.setImageResource(R.drawable.ic_halloween);
        ImageView iconSortRating = new ImageView(this);
        iconSortRating.setImageResource(R.drawable.ic_halloween);
        ImageView iconSortYear = new ImageView(this);
        iconSortYear.setImageResource(R.drawable.ic_halloween);

        // Set the background for all Sub buttons
        SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this);

        // Build the Sub Buttons
        SubActionButton buttonSortName = itemBuilder.setContentView(iconSortName).build();
        SubActionButton buttonSortRating = itemBuilder.setContentView(iconSortRating).build();
        SubActionButton buttonSortYear = itemBuilder.setContentView(iconSortYear).build();
        buttonSortName.setTag(TAG_SORT_NAME);
        buttonSortRating.setTag(TAG_SORT_RATING);
        buttonSortYear.setTag(TAG_SORT_YEAR);
        buttonSortName.setOnClickListener(this);
        buttonSortRating.setOnClickListener(this);
        buttonSortYear.setOnClickListener(this);

        // add the sub buttons to the main floating action button
        FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(buttonSortName)
                .addSubActionView(buttonSortRating)
                .addSubActionView(buttonSortYear)
                .attachTo(actionButton)
                .build();
    }

    @Override
    public void onRefresh() {
        fetchMovies();

    }

    private void fetchMovies(){

        // showing refresh animation before making http call
        swipeRefreshLayout.setRefreshing(true);
        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();
                        movieList.clear();
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("title"));
                                movie.setOpis(obj.getString("opis"));
                                movie.setThumbnailUrl(obj.getString("image"));
                                movie.setRating(((Number) obj.get("rating"))
                                        .doubleValue());
                                movie.setYear(obj.getInt("releaseYear"));
                                movie.setUrl(obj.getString("url"));

                                // Genre is json array
                                final JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                movie.setGenre(genre);

                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }                        

                        }
                                adapter.notifyDataSetChanged();                                  
                                // stopping swipe refresh
                                swipeRefreshLayout.setRefreshing(false);
                    }


                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        hidePDialog();

                        // stopping swipe refresh
                        swipeRefreshLayout.setRefreshing(false);
                    }
                });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        if (item.getItemId() == android.R.id.home) {
            finish();
            mp_off.start();
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
        mp_off.start();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    @Override
    public void onClick(View view) {
        if(view.getTag().equals(TAG_SORT_NAME)){
            adapter.getItem();
        }

        if(view.getTag().equals(TAG_SORT_RATING)){

        }

        if(view.getTag().equals(TAG_SORT_YEAR)){

        }

    }

    @Override
    public void onSortByName() {

    }

    @Override
    public void onSortByRating() {

    }

    @Override
    public void onSortByYear() {

    }
}
公共类ListaPreporuka扩展AppCompatActivity实现SwipeRefreshLayout.OnRefreshListener、View.OnClickListener
,SortListener{
//日志标签
私有静态最终字符串标记=ListaPreporuka.class.getSimpleName();
//电影json url
私有静态最终字符串url=”http://www.nadji-ekipu.org/wp-content/uploads/2015/07/movies.txt";
私人对话;
private List movieList=new ArrayList();
私有列表视图列表视图;
私人SwipeRefreshLayout SwipeRefreshLayout;
专用自定义列表适配器;
私有静态最终字符串标记\u SORT\u NAME=“sortName”;
私有静态最终字符串标记\u SORT\u RATING=“sortRating”;
私有静态最终字符串标记\u SORT\u YEAR=“sortYear”;
私有静态字符串Year=“Year”;
私有静态字符串Rating=“Rating”;
私有静态字符串Title=“Title”;
私有静态字符串bitmap=“thumbnailUrl”;
专用静态字符串opis=“opis”;
私有静态字符串urlmoine=“url”;
私人媒体播放器mp_关闭;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_preproruka);
//Toolbabr设置
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_horor_filmovi_ikonica);
Intent newActivity2=新Intent();
setResult(RESULT_OK,newActivity2);
mp\u off=MediaPlayer.create(这个,R.raw.button\u单击\u off);
final MediaPlayer mp\u on=MediaPlayer.create(这个,R.raw.button\u单击);
pDialog=新建进度对话框(此对话框);
//在发出http请求之前显示进度对话框
pDialog.setIcon(R.drawable.ic_horor_filmovi_ikonica);
pDialog.setMessage(“Učitavanje…”);
pDialog.setCancelable(假);
pDialog.show();
listView=(listView)findViewById(R.id.list);
swipeRefreshLayout=(swipeRefreshLayout)findViewById(R.id.swipe\u refresh\u layout);
adapter=新的CustomListAdapter(此,movieList);
setAdapter(适配器);
swipeRefreshLayout.setOnRefreshListener(此);
/**
*在“创建活动”上显示滑动刷新动画
*由于动画不会在onCreate上启动,因此会使用post runnable
*/
swipeRefreshLayout.post(新的Runnable(){
@凌驾
公开募捐{
swipeRefreshLayout.setRefreshing(true);
获取电影();
}
}
);
if(AppStatus.getInstance(this.isOnline()){
Log.v(“家”、“家”、“你在线!!!”;
}否则{
setContentView(右布局,无连接);
Toast t=Toast.makeText(这是“没有互联网连接”,Toast.LENGTH\u SHORT);
t、 show();
Log.v(“家”、“家”、“你不在线!!!”;
}
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
字符串名称=((TextView)view.findViewById(R.id.title))
.getText().toString();
字符串opisFilma=((TextView)view.findViewById(R.id.opis))
.getText().toString();
字符串urlFilm=((TextView)view.findViewById(R.id.url))
.getText().toString();
String ocena=String.valueOf(movieList.get(position.getRating());
String godina=String.valueOf(movieList.get(position.getYear());
位图=((Movie)movieList.get(position)).getThumbnailUrl();
Intent Intent=newintent(ListaPreporuka.this,MoviesSingleActivity.class);
意向书(标题、名称);
意向。额外支出(opis、opisFilma);
intent.putExtra(“图像”,位图);
意向。额外(评级,ocena);
意向。额外费用(年,戈迪纳);
intent.putExtra(urlmoine,urlmoil);
mp_on.start();
星触觉(意向);
覆盖转换(R.anim.slide\u in,R.anim.slide\u out);
}
});
buildFAB();
}
私有void buildFAB(){
//声明晶圆厂的图标
ImageView图标=新的ImageView(此);
icon.setImageResource(R.drawable.ic_halloween);
//建造工厂
FloatingActionButton actionButton=新建FloatingActionButton.Builder(此)
.setContentView(图标)
.build();
//声明子动作按钮的图标
ImageView图标传感器名称=新的ImageView(此);
iconSortName.setImageResource(R.drawable.ic_halloween);
ImageView iconSortRating=新的ImageView(此);
iconSortRating.setImageResource(R.drawable.ic_halloween);
ImageView图标传感器EAR=新的ImageView(此);
iSensorTyear.setImageResource(R.drawable.ic_halloween);
//设置所有子按钮的背景
SubActionButton.Builder itemBuilder=新建SubActionButton.Builder(此);
//建造潜艇但是
Comparator<Movie> comparator = new Comparator<Movie>() {
  @Override
  public int compare(Movie movie, Movie t1) {
    return movie.genre.compareTo(t1.genre);
  }
};

// ordered by genre
Collections.sort(movieList, comparator);    

// Reverse order by genre
Collections.sort(movieList, Collections.reverseOrder(comparator));