简体   繁体   中英

(Java) Declare ArrayList to use in other class

I have following code which is as a final result giving me list of images from Android device larger than 500 KB:

package com.click;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.widget.LinearLayout;
import android.widget.TextView;



public class SkanerActivity extends Activity {
    private File root;
    private ArrayList<File> fileList = new ArrayList<File>();
    private LinearLayout view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skaner);


        //TEST
        view = (LinearLayout) findViewById(R.id.view);

        //getting SDcard root path
        root = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        getfile(root);

        for (int i = 0; i < fileList.size(); i++) {
            TextView textView = new TextView(this);
            textView.setText(fileList.get(i).getName());
            textView.setPadding(5, 5, 5, 5);

            System.out.println(fileList.get(i).getName());

            if (fileList.get(i).isDirectory()) {
                textView.setTextColor(Color.parseColor("#FF0000"));
            }
            view.addView(textView);
        }

    }

    public ArrayList<File> getfile(File dir) {
        File listFile[] = dir.listFiles();
        if (listFile != null && listFile.length > 0) {
            for (int i = 0; i < listFile.length; i++) {

                if (listFile[i].isDirectory()) {
                    // Zakomentowane aby nie dodawac do listy folderów
                    // fileList.add(listFile[i]);
                    getfile(listFile[i]);

                } else if (listFile[i].length() >= 500 * 1024)  {
                    if (listFile[i].getName().endsWith(".png")
                            || listFile[i].getName().endsWith(".jpg")
                            || listFile[i].getName().endsWith(".jpeg")
                            || listFile[i].getName().endsWith(".gif"))

                    {
                        fileList.add(listFile[i]);

                    }



                    }

                }

            }

        return fileList;
    }
}

Now I would like to create new class to process further with new operations on this list of images - in new class.

My question is: how can I handle and declare fileList as a variable (Object) to be used in other class (new Java class)? I know, that in Java there isn't such method as "Global variable", but for sure is there any method to use result of script in other place (Class) in Application.

Can you please help me with my issue? Many thanks in advance for your help!

If by new class you mean new Activity then you can pass data using

Intent intent = new Intent(getBaseContext(), NewClass.class);
intent.putExtra("imageList", fileList);
startActivity(intent);

But prior to that you need to get reference to this List in the current class

fileList = getfile(root);

But if you simply mean need a new Class for processing. Simply create a new class. Define a constructor/method that takes ArrayList<File> argument. Create an object of this new class from your current class passing the list of files as argument and call the method in it for your further processing logic.

class ImagesProcessorClass {

  List<File> imageList;

  public ImagesProcessorClass (List<file> imageList) {
     this.imageList = imageList;
  }

  public void processsImages(){
    //do processing on imageList
   }

}

and in your present class do

public class SkanerActivity extends Activity {{

//your  present code

    protected void onCreate(Bundle savedInstanceState) {
     //your present code
     fileList = getfile(root);
      ImagesProcessorClass  imageProcessor = new ImagesProcessorClass (fileList);
       imageProcessor.processsImages();
     }


}

Make fileList a private member of the class. Then add accessor methods to access it from outside the class . You will of course need to create an object of SkanerActivity to do so.

class SkanerActivity{
...
 private ArrayList<File> fileList = new ArrayList<File>();
 public getFileList(){
  return fileList;
 }
... 
} 

Class Access{

void function1(){
 SkanerActivity obj = new SkanerActivity();
 List fileList = obj.getFileList();
 ..
} 

If you want to use it in another activity, you can use something like that

    Intent intent = new Intent(this, AnotherActivity.class);
    intent.putExtra("List", fileList);
    startActivity(intent); 

And in AnotherActivity have to do this

    Intent data = getIntent();
    List<File> files = (List<File>)data.getSerializableExtra("List");

Also you can make this list static and create method to get it;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM