简体   繁体   English

Android Studios如何将上下文从mainactivity传递到另一个类

[英]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); 我有这个问题,如何在类Dialog_findname中使用MainActivity中的函数,我必须在行中传递main的上下文: 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: 这是我要在其他类中调用的MainActivity函数:

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. 我认为,您必须在您的MainActivity上调用此函数。

vardadienas = main.loadedfile(this);

or check MainActivity.class 或检查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 . 它应该以Context.startActivity()开始,您可以在Activity文档中阅读有关它的更多信息。

Second , you have the following method: 其次 ,您具有以下方法:

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

so, you can't call it with the following inside of Fragment: 因此,您不能在Fragment内部使用以下代码来调用它:

 vardadienas = main.loadedfile(MainActivity.this);

because MainActivity.this is referencing to instance of MainActivity but your fragment is not an instance of activity. 因为MainActivity.this指向MainActivity的实例,但是您的片段不是活动的实例。

You need to call the method with the following: 您需要使用以下方法调用该方法:

 vardadienas = main.loadedfile(getContext());

where getContext() referring to Activity where the Fragment attached. 其中getContext()指的是Fragment所在的Activity。


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. 最好将loadedfile(Context ctxt)作为util类移到其自己的类中,以便可以从任何其他类重用该方法。 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将上下文从MainActivity传递到Android中的另一个类? - How to pass context from MainActivity to another class in Android? 如何在Mainactivity和Android中的另一个类之间传递全局计数器? - How to pass global counter between Mainactivity & another class in android? 安卓 如何从另一个文件访问一个类到MainActivity - Android. How to access a class from another file to MainActivity 从android中的另一个类调用MainActivity方法 - Call MainActivity method from another class in android 将MainActivity中的逻辑移动到Android中的另一个类 - Moving logic from MainActivity to another class in Android 来自Android中另一个类的mainactivity中的调用方法 - calling method in mainactivity from another class in android 如何在MainActivity Android的另一个类中使用mQueue? - How to use mQueue in another class then MainActivity Android? 如何在另一个类中使用“ MainActivity类”中的意图传递数据,而不扩展任何类 - How to use intent pass data from 'MainActivity class' in another class, not extending any class 如何从另一个 class 调用 MainActivity 中的方法? - How to call a method in MainActivity from another class? 目的问​​题:如何使用另一个“适配器”类中的MainActivity将数据传递给SecondActivity - Intent problem: how to use MainActivity from another class “Adapter” to pass data to SecondActivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM