简体   繁体   中英

Android studios how to pass a context from mainactivity to another class

I have this problem, how do i use a function from MainActivity in my class Dialog_findname, i have to pass the context of main in line: vardadienas = main.loadedfile(MainActivity.this);

public class Dialog_findname extends AppCompatDialogFragment {

  private EditText findName;
  private findnameDialogListener listener;
  private List<VDienas> vardadienas = new ArrayList<>();
  private Finder finder = new Finder();
  private MainActivity main = new MainActivity();


  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {

    vardadienas = main.loadedfile(MainActivity.this);
  }

And this is my MainActivity function I want to call in the other class:

public List<VDienas> loadedfile(Context ctxt){
    FileInputStream fis = null;
    try {
        fis = openFileInput(FILE_NAME);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader reader = new BufferedReader(isr);
        String line;
        reader.readLine();

        while ((line = reader.readLine()) != null){
            VDienas VissGads = new VDienas();
            String[] tokens = line.split(";");


            VissGads.setDatums(tokens[0]);
            VissGads.setMenesis(Integer.parseInt(tokens[1]));
            VissGads.setDiena(Integer.parseInt(tokens[2]));

            for (int i = 0; i < Integer.parseInt(tokens[3]); i++) {
                VissGads.setVards(tokens[i + 4]);

            }
            vardadienas.add(VissGads);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (fis != null){
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return vardadienas;

}

I tried to look up for similar questions, but i still couldn't understand how to :(

i think, u have to call this Function on ur MainActivity.

vardadienas = main.loadedfile(this);

or check MainActivity.class

First , you can't create an Activity with the following:

private MainActivity main = new MainActivity();

it should be started with Context.startActivity() , you can read more about it at Activity documentation .

Second , you have the following method:

public List<VDienas> loadedfile(Context ctxt) {
  ...
}

so, you can't call it with the following inside of Fragment:

 vardadienas = main.loadedfile(MainActivity.this);

because MainActivity.this is referencing to instance of MainActivity but your fragment is not an instance of activity.

You need to call the method with the following:

 vardadienas = main.loadedfile(getContext());

where getContext() referring to Activity where the Fragment attached.


You better move the loadedfile(Context ctxt) to its own class as an util class so you can reuse the method from any other class. You can make something like this:

public class FileUtils {
  private FileUtils() {} // this prevent class being instantiate.

  // we need to make it static so it can be accessed without
  // creating an instance of the class.
  // of course, you can use singleton. But it's another topic
  public static List<VDienas> loadedfile(Context ctxt) {
    ...
  }
}

then you can use the method with something like this:

vardadienas = FileUtils.loadedfile(getContext());

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