[英]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.