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