在Android中排序后没有任何反应

时间:2012-07-13 05:16:29

标签: android arrays listview sorting

public void addListenerOnSorting() {

    sorting=(Button) findViewById(R.id.sort);

    sorting.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            final  File f = new File(dp);
            File[] files = f.listFiles();
            Arrays.sort(files, filecomparator);
        }
        Comparator<File> filecomparator = new Comparator<File>(){
            public int compare(File file1, File file2) {
                return String.valueOf(file1.getName()).compareTo(file2.getName());
            }
        };
    });
}

我的问题是,在排序后,我的列表视图是不是会更新或刷新?

package but.view.apply;

public class homes  extends ListActivity {

    protected static final ArrayAdapter<String> List = null;
    private List<String> item = null;
    private List<String> path = null;
    private String root="/";
    private String back = "../";
    File fifo;
    File f;
    File currentFolder;
    int len;
    Stack<File> history;
    String dp;

    private List<String> fileList = new ArrayList<String>();
    ArrayAdapter<String> list;
    ArrayAdapter<String>directoryList;
    Button homing,editing,uping,viewing,sorting;
    Button mb, bm,so, wp, lt, st;
    private TextView myPath;
    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.homemain);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

        ImageView entro=(ImageView) this.findViewById(R.id.header);
        entro.setSelected(true);
        RunAnimations();
        myPath = (TextView)findViewById(R.id.path);
        addListenerOnediting();
        addListenerOnButton();
        addListenerOnviewing();
        addListenerOnSorting();

        getDir(root);
    }
    public void addListenerOnButton() {

        final Context context = this;

        homing = (Button) findViewById(R.id.home);

        homing.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(context, homes.class);
                startActivity(intent);
            }
        });
    }


    private void getDir(String dirPath)
    {
        myPath.setText("Location: " + dirPath);

        item = new ArrayList<String>();

        path = new ArrayList<String>();

        dp=dirPath;
        final  File f = new File(dirPath);

        File[] files = f.listFiles();

        back = f.getParent();
        uping = (Button) findViewById(R.id.up);

        uping.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                getDir(back);
            }
        });

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

            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.homerow, item);

        setListAdapter(fileList);
        fileList = list;
    }


    @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
        {
            fifo=file;
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.parse("file://" + file.getPath());
            String fname=file.getName();

            if(fname.endsWith(".jpg")||fname.endsWith("png")||fname.endsWith(".gif")||
                    fname.endsWith(".      jpeg"))
            {          intent.setDataAndType(uri, "image/*");
                startActivity(intent);      }
            else if(fname.endsWith(".mp4")||fname.endsWith(".3gp"))
            {          intent.setDataAndType(uri, "video/*");
                startActivity(intent);      }      else if(fname.endsWith(".mp3"))
            {          intent.setDataAndType(uri, "audio/*");
                startActivity(intent);      }
        }
    }


    public  void addListenerOnSorting() {

        sorting=(Button) findViewById(R.id.sort);
        final Context context1=this;
        sorting.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

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

                    if (fileList != null) {
                        final  File f = new File(dp);

                        File[] fileList = f.listFiles();
                        List<File> directoryListing = new ArrayList<File>();
                        directoryListing.addAll(Arrays.asList(fileList));
                        Collections.sort(directoryListing, new SortFileName());
                        Collections.sort(directoryListing, new SortFolder());
                    }
                }
            }
        });
    }

    public void addListenerOnediting() {

        final Context context = this;

        editing = (Button) findViewById(R.id.edit);

        editing.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                final CharSequence[] items = {"copy","delete","move","paste","rename"};

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

                builder.setTitle("Pick an item");

                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {

                    }
                });
                AlertDialog alert = builder.create();

                alert.show();
            }
        });
    }


    public void addListenerOnviewing() {

        final Context context2 = this;

        viewing = (Button) findViewById(R.id.view);

        viewing.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                final CharSequence[] items = {"list", "grid"};

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

                builder.setTitle("View by");

                builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                    // Click listener
                    public void onClick(DialogInterface dialog, int item) {

                        if(items[item]=="grid")
                        {

                        }
                    }
                });
                AlertDialog alert = builder.create();
                //display dialog box
                alert.show();
            }
        });
    }

    private void RunAnimations() {
        ImageView entro = (ImageView) findViewById(R.id.header);
        Animation a = AnimationUtils.loadAnimation(this, R.animator.ani);
        entro.clearAnimation();
        entro.startAnimation(a);
    }
}

现在这是我尝试的另一种选择...... SortFileName类是:

package but.view.apply;

import java.io.File;
import java.util.Comparator;

public class SortFileName implements Comparator<File> {
    public int compare(File f1, File f2) {
        return f1.getName().compareTo(f2.getName());
    }
}

和Sortfoldername类是:

package but.view.apply;

import java.io.File;
import java.util.Comparator;

public class SortFolder implements Comparator<File> {

    public int compare(File f1, File f2) {
        if ((f1.isDirectory() && f2.isDirectory())
                || (!f1.isDirectory() && !f2.isDirectory()))
            return 0;
        else if (f1.isDirectory() && !f2.isDirectory())
            return -1;
        else
            return 1;
    }
}

3 个答案:

答案 0 :(得分:0)

如果您的数据有任何变化(添加或删除项目,排序或其他),您需要在ListAdapter上调用notifyDataChanged()。

<强> [编辑]
你必须做这样的事情

class MyList extends ListActivity {
    private ArrayAdapter<String> mAdapter;
    private List<String> mList;
    ....

    public void onCreate(){
        mList = createMyList();
        mAdapter = new ArrayAdapter<String>(this, R.layout.homerow, mList);
        // get sort button
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Collections.sort(mList);
                mAdapter.notifyDatasetChanged(); // you have to do that in the UI thread!
                ....

答案 1 :(得分:0)

如果您已在适配器使用的阵列中删除或添加数据,则更新ListView,然后使用以下命令刷新适配器:

 adapter.notifyDataSetChanged();

答案 2 :(得分:0)

我真的不知道你在做什么。这很好。

package my.test.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;

public class AndroidTestActivity extends ListActivity implements OnClickListener {
    private List<String> mList;
    private ArrayAdapter<String> mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mList = new ArrayList<String>();
        mList.add("bbb");
        mList.add("aaa");
        mList.add("ddd");
        mList.add("ccc");

        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
        setListAdapter(mAdapter);

        findViewById(R.id.button).setOnClickListener(this);
    }

    private void sort(){
        Collections.sort(mList);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.button:
            sort();
            break;
        }
    }
}