Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

java - How to sort files using datetimestamp

I am capturing images, then storing into SD Card and showing in a List, but here i need a small change, still i am getting old on top and latest at bottom, so now i want to show latest picture on the top on the basis of datetimestamp using as a part of file name.

UploadActivity.java code:-

 String fileName;

 static List <String> ImageList;

  /*** Get Images from SDCard ***/
    ImageList = getSD();

    // ListView and imageAdapter
    lstView = (ListView) findViewById(R.id.listView1);
    lstView.setAdapter(new ImageAdapter(this)); 
    }

    public static List <String> getSD()
    {
        List <String> it = new ArrayList <String>();
        String string = "/mnt/sdcard/Pictures/SamCam/";
        f = new File (string+ CameraLauncherActivity.folder+ "/");
        files = f.listFiles ();

        for (int i = 0; i < files.length; i++)
            {
                file = files[i];
                Log.d("Count",file.getPath());
                it.add (file.getPath());
            }
    return it;  
    }


    public class ImageAdapter extends BaseAdapter
    {
        private Context context;

            public ImageAdapter(Context c)
        {
            // TODO Auto-generated method stub
            context = c;

        }

Note: I am using date/timestamp while storing my images into SD Card.

so finally it looks like this:

  AU_20140328163947_1_4_X-1-4-006.jpg

and still files listing in below format, like below:

AU_20140328163947_1_4_X-1-4-006.jpg

 AU_20140328163948_1_4_X-1-4-007.jpg

 AU_20140328163949_1_4_X-1-4-008.jpg

but i want to list files in below format:-

AU_20140328163949_1_4_X-1-4-008.jpg

 AU_20140328163948_1_4_X-1-4-007.jpg

 AU_20140328163947_1_4_X-1-4-006.jpg

Code to Delete Image in a List:--

// btnDelete
        final ImageButton btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);
        btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

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

                    // set title
                    alertDialogBuilder.setTitle("Delete Image");

                    // Setting Icon to Dialog
                    alertDialogBuilder.setIcon(R.drawable.ic_launcher);

                    // set dialog message
                    alertDialogBuilder
                        .setMessage("Are you sure you want to delete this image?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, close
                                // current activity
                                // to get fileName
                                fileName = ImageList.get(position).toString().substring(strPath.lastIndexOf('/')+1, strPath.length());
                                // to get SD card path (Folders+fileName)
                                 String fileToDelete = Environment.getExternalStorageDirectory().getPath() +"/Pictures/SamCam/" + CameraLauncherActivity.folder+ "/" + fileName;
                                 Log.d("FileToDelete", fileToDelete);
                                  File myFile = new File(fileToDelete);
                                  // if image exists
                                      if(myFile.exists())
                                          // delete image
                                        myFile.delete();
                                      // get position and delete
                                      ImageList.remove(position);
                                      // to refresh the view
                                 ((BaseAdapter) lstView.getAdapter()).notifyDataSetChanged(); 

                                 dialog.cancel();
                            }
                          })
                        .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                // TODO Auto-generated method stub

            }
        });

        return convertView;

            }   
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you getting data in reverse order than you can use reverse loop.

Try below loop

for (int i = files.length-1; i >= 0; i--)
{
file = files[i];
Log.d("Count",file.getPath());
it.add (file.getPath());
}

instead of

 for (int i = 0; i < files.length; i++)
 {
      file = files[i];
      Log.d("Count",file.getPath());
      it.add (file.getPath());
  }

or sort data with particular field

Sort array data before using in for loop and use same loop..

Arrays.sort(files, new Comparator<Object>()
{
    public int compare(Object o1, Object o2) {

        if (((File)o1).lastModified() > ((File)o2).lastModified()) {
            return -1;
        } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
            return +1;
        } else {
            return 0;
        }
    }

});

for (int i = 0; i < files.length; i++)
 {
      file = files[i];
      Log.d("Count",file.getPath());
      it.add (file.getPath());
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...