【问题标题】:how to add the sort option in my app?如何在我的应用程序中添加排序选项?
【发布时间】:2012-06-07 07:00:04
【问题描述】:

我一直在尝试了解 android 中的列表和其他内容。我有一个应用程序可以显示 sd 卡中的项目列表。我想要代码,我可以为其添加排序选项。将按名称、日期、类型和大小对列表进行排序。请帮帮我.. 我在这里显示我的代码..它在列表视图中显示内容。

package com.android.sam;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener ;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SamActivity  extends ListActivity {

private List<String> item = null;

private List<String> path = null;

private String root="/";

private TextView myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
    myPath = (TextView)findViewById(R.id.path);
getDir(root);
}
private void getDir(String dirPath)

{

 myPath.setText("Location: " + dirPath);



 item = new ArrayList<String>();

 path = new ArrayList<String>();



 File f = new File(dirPath);

 File[] files = f.listFiles();



 if(!dirPath.equals(root))

 {



  item.add(root);

  path.add(root);



  item.add("../");

  path.add(f.getParent());



  }



  for(int i=0; i < files.length; i++)

  {

   File file = files[i];

   path.add(file.getPath());

   if(file.isDirectory())

    item.add(file.getName() + "/");

   else

    item.add(file.getName());

  }



  ArrayAdapter<String> fileList =

   new ArrayAdapter<String>(this, R.layout.row, item);

   setListAdapter(fileList);

  }
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())

 {

 if(file.canRead())

 getDir(path.get(position));

 else

{

 new AlertDialog.Builder(this)

.setIcon(R.drawable.ic_launcher)

.setTitle("[" + file.getName() + "] folder can't be read!")

.setPositiveButton("OK", 

  new DialogInterface.OnClickListener() {

 public void onClick(DialogInterface dialog, int which) {

    // TODO Auto-generated method stub

   }

  }).show();

  }

  }

 else

{

  new AlertDialog.Builder(this)

.setIcon(R.drawable.alert)

.setTitle("[" + file.getName() + "] ")

.setPositiveButton("OK", 

  new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {

    // TODO Auto-generated method stub

   }

  }).show();

  }

   }

  }

在我的布局中,我添加了一个图像按钮,它应该显示排序选项并对列表进行排序 提前谢谢 这是我添加的 `排序=(按钮)findViewById(R.id.button2); 最终上下文 context1=this; 排序.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

          if (v == findViewById(R.id.button2)) {

                 final CharSequence[] items = {"name", "date", "size", "type", "none"};

                 AlertDialog.Builder builder = new AlertDialog.Builder(context1);

                 builder.setTitle("Sort by");

                 builder.setSingleChoiceItems(items, -1, new                                    DialogInterface.OnClickListener       ()       {
                     // Click listener
                     public void onClick(DialogInterface dialog, int item) {
                         Toast.makeText(getApplicationContext(), items[item],            Toast.LENGTH_SHORT).show();
                         //If the Cheese item is chosen close the dialog box
                         if(items[item]=="none")
                             dialog.dismiss();
                     }
                 });
                 AlertDialog alert = builder.create();
                 //display dialog box
                 alert.show();
             }
      }
      });  

【问题讨论】:

  • Collections.sort(item);但是如果用户单击任何按钮,是否要再次对它们进行排序?
  • 谢谢你 dheeresh singh :) 但是我在哪里添加呢?
  • 如果我在对话框中单击名称,列表会根据名称进行排序吗?以及日期、大小和类型/
  • 创建上面链接中提到的 Comparator a

标签: android listview


【解决方案1】:

1-您是否只想对 String 对象的 ArrayList 进行排序,因为 ArrayList 可以使用 Collections.sort(item);

2- 如果您有对象类型的 ArrayList,则需要为此创建比较器。

http://www.developer.com/java/other/article.php/858411/Data-Structures-in-Java-Part-9-The-Comparator-Interface-Part-1.htm

java-sorting-comparator-vs-comparable

【讨论】:

  • 因为看起来你需要对不同属性 name"、"date"、"size"、"type"、"none" 的 bassi 进行排序,所以你需要为每个属性并在对话框列表中选择项目时对列表进行排序。
  • 我已经为 diff 属性添加了比较器,但代码不起作用 :( 可能我错过了什么……你能告诉我我必须输入这段代码吗?提前 tthnakx
  • 是的,我需要查看代码............要么在此处更新,要么在新查询中发布......
猜你喜欢
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多