-2
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;
    }
}
4

3 に答える 3

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!
                ....
于 2012-07-13T05:27:25.390 に答える
0

アダプタで使用されるアレイのデータを削除または追加した場合にListViewを更新するには、次のコマンドでアダプタを更新します。

 adapter.notifyDataSetChanged();
于 2012-07-13T05:29:40.583 に答える
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;
        }
    }
}
于 2012-07-13T08:10:38.710 に答える