can anyone tell me if I am right or wrong? I am really getting confused in solving my problem.
任何人都可以告诉我,我是对还是错?我真的很困惑解决我的问题。
What I have is (or what I want to do Or am thinking is:)
我拥有的是(或者我想做什么或者我在想:)
I have:
Class B{
........
........
interface I{
......
........
}
.......
.......
}
and :
Class A implements B.I{
........
.......
B b= new B();
}
Is it the right way of communication between two classes class B and Class A? how should i make this work. I want some data from class A passed to class B for further operations.
它是B类和A类两个班级之间正确的沟通方式吗?我该怎么做才能做到这一点。我希望A类中的一些数据传递给B类以进行进一步的操作。
how should i make the methods that i will implement in A get called from B when i require the data? A simple example on an Interface having same scenario will really help me. Doea anyone have a good explanation on how interface work? or how should they be used?
当我需要数据时,我应该如何制作我将在A中实现的方法?在具有相同场景的界面上的一个简单示例将真正帮助我。 Doea有没有人对接口如何工作有一个很好的解释?或者它们应该如何使用?
I would also further like to ask logic behind working of interfaces in android..? what is the logic behind callback methods that we have in OnClick Listeners? because this also is carried out using interfaces? for ex: we implement them in our class
我还想进一步询问android背后的接口工作背后的逻辑吗?我们在OnClick Listeners中使用的回调方法背后的逻辑是什么?因为这也是使用接口进行的?例如:我们在课堂上实施它们
class A implements View.OnClickListener
and provide the logic in our class for handling onClick events? So when are they called .(I know they are called when we click on that particular view) i want the mechanism or implementation of how they are called
并提供我们班级处理onClick事件的逻辑?那么他们什么时候打电话。(我知道当我们点击那个特定的视图时会调用它们)我想要它们被调用的机制或实现
or maybe i should do this using abstract class ? i am really stuck! Thankyou
或者我应该使用抽象类来做到这一点?我真的被卡住了!谢谢
3 个解决方案
#1
0
It seems that what you are trying to figure out is communication between Fragments
. Here it is well explained. If you want to communicate between Activities
then you should read about Intents
.
看来你想弄清楚的是片段之间的沟通。这里有很好的解释。如果你想在活动之间进行交流,那么你应该阅读Intents。
#2
0
I'm not completely sure I understand but it sounds like you just need to implement a DB class. For this you can find many good examples such as Here. In ActivityA
you can do your DB stuff in an AsyncTask so that these operations are done in the background and don't hold up your UI
thread. Then use a separate AyncTask
in ActivityB
to access your DB class to retrieve the info
我不完全确定我理解,但听起来你只需要实现一个DB类。为此您可以找到很多很好的例子,例如Here。在ActivityA中,您可以在AsyncTask中执行数据库操作,以便这些操作在后台完成,并且不会阻止您的UI线程。然后在ActivityB中使用单独的AyncTask来访问您的数据库类以检索信息
As far as using OnClickListener
s, you can do this in different ways. You can define them in a separate class but it is usually easier and just as efficient to do them in the class that utilizes them.You can define them in your xml with
就使用OnClickListeners而言,您可以通过不同方式执行此操作。您可以在一个单独的类中定义它们,但它通常更容易,并且在使用它们的类中执行它们同样有效。您可以在xml中定义它们
<Button
android:id="@+id/btn1"
// button attributes
android:onClick=methodName/>
then in your java code
然后在你的java代码中
public void methodName(View v)
{
//do stuff here
}
or use something like
或使用类似的东西
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do more stuff
}
});
in your java code after defining button1
在定义button1之后的java代码中
Without seeing code and knowing your specific question, this is what it sounds like you might be looking for. If not, please be more specific as to what you want and maybe we can better assist you
无需查看代码并了解您的具体问题,这就是您可能正在寻找的内容。如果没有,请更具体地说明您的需求,也许我们可以更好地为您提供帮助
#3
0
Interfaces are meant to define a type of behavior in Java. If a class implements an interface, it is reassuring the compiler that it can do all the methods in that interface.
接口用于定义Java中的一种行为。如果一个类实现了一个接口,它会向编译器保证它可以执行该接口中的所有方法。
For example, you could have a Printable interface with methods required of objects that can be printed (e.g. a getStringRepresentation method). Any class that implements Printable must implement all its methods, and so you must be able to print objects of that class.
例如,您可以使用Printable接口,其中包含可以打印的对象所需的方法(例如,getStringRepresentation方法)。任何实现Printable的类都必须实现其所有方法,因此您必须能够打印该类的对象。
You can read more about interfaces here.
您可以在此处阅读有关接口的更多信
If you just want to pass data from class A to class B, you don't necessarily need an interface as you don't have multiple class which can do the same thing, which is when you may need to define their common behavior by using an interface.
如果你只想将数据从A类传递给B类,那么你不一定需要一个接口,因为你没有多个类可以做同样的事情,这时你可能需要通过使用它来定义它们的共同行为一个界面。
Why couldn't you just pass the data from class A to and object of class B using a parameter of one of B's methods?
你为什么不能使用B方法之一的参数将数据从A类传递给B类对象?
e.g.
// somewhere in the methods of A
B b = new B();
b.giveData(theData);
#1
0
It seems that what you are trying to figure out is communication between Fragments
. Here it is well explained. If you want to communicate between Activities
then you should read about Intents
.
看来你想弄清楚的是片段之间的沟通。这里有很好的解释。如果你想在活动之间进行交流,那么你应该阅读Intents。
#2
0
I'm not completely sure I understand but it sounds like you just need to implement a DB class. For this you can find many good examples such as Here. In ActivityA
you can do your DB stuff in an AsyncTask so that these operations are done in the background and don't hold up your UI
thread. Then use a separate AyncTask
in ActivityB
to access your DB class to retrieve the info
我不完全确定我理解,但听起来你只需要实现一个DB类。为此您可以找到很多很好的例子,例如Here。在ActivityA中,您可以在AsyncTask中执行数据库操作,以便这些操作在后台完成,并且不会阻止您的UI线程。然后在ActivityB中使用单独的AyncTask来访问您的数据库类以检索信息
As far as using OnClickListener
s, you can do this in different ways. You can define them in a separate class but it is usually easier and just as efficient to do them in the class that utilizes them.You can define them in your xml with
就使用OnClickListeners而言,您可以通过不同方式执行此操作。您可以在一个单独的类中定义它们,但它通常更容易,并且在使用它们的类中执行它们同样有效。您可以在xml中定义它们
<Button
android:id="@+id/btn1"
// button attributes
android:onClick=methodName/>
then in your java code
然后在你的java代码中
public void methodName(View v)
{
//do stuff here
}
or use something like
或使用类似的东西
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do more stuff
}
});
in your java code after defining button1
在定义button1之后的java代码中
Without seeing code and knowing your specific question, this is what it sounds like you might be looking for. If not, please be more specific as to what you want and maybe we can better assist you
无需查看代码并了解您的具体问题,这就是您可能正在寻找的内容。如果没有,请更具体地说明您的需求,也许我们可以更好地为您提供帮助
#3
0
Interfaces are meant to define a type of behavior in Java. If a class implements an interface, it is reassuring the compiler that it can do all the methods in that interface.
接口用于定义Java中的一种行为。如果一个类实现了一个接口,它会向编译器保证它可以执行该接口中的所有方法。
For example, you could have a Printable interface with methods required of objects that can be printed (e.g. a getStringRepresentation method). Any class that implements Printable must implement all its methods, and so you must be able to print objects of that class.
例如,您可以使用Printable接口,其中包含可以打印的对象所需的方法(例如,getStringRepresentation方法)。任何实现Printable的类都必须实现其所有方法,因此您必须能够打印该类的对象。
You can read more about interfaces here.
您可以在此处阅读有关接口的更多信
If you just want to pass data from class A to class B, you don't necessarily need an interface as you don't have multiple class which can do the same thing, which is when you may need to define their common behavior by using an interface.
如果你只想将数据从A类传递给B类,那么你不一定需要一个接口,因为你没有多个类可以做同样的事情,这时你可能需要通过使用它来定义它们的共同行为一个界面。
Why couldn't you just pass the data from class A to and object of class B using a parameter of one of B's methods?
你为什么不能使用B方法之一的参数将数据从A类传递给B类对象?
e.g.
// somewhere in the methods of A
B b = new B();
b.giveData(theData);