问题
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.
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?
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
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
or maybe i should do this using abstract class ? i am really stuck! Thankyou
回答1:
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.
回答2:
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
As far as using OnClickListeners, 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
<Button
android:id="@+id/btn1"
// button attributes
android:onClick=methodName/>
then in your java code
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
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:
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.
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.
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.
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?
e.g.
// somewhere in the methods of A
B b = new B();
b.giveData(theData);
来源:https://stackoverflow.com/questions/15059351/using-nested-interfaces-for-communication-between-two-classes-in-java-android