0

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

user2056245
  • 595
  • 1
  • 9
  • 24

3 Answers3

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.

Juan Andrés Diana
  • 2,215
  • 3
  • 25
  • 36
  • what i have is : an activity showing a form with various fields.... i want to take the information from those fields and store them in database (before that i want to covert them into json string) so my activity is Class A from my question and class B will be a simple class with that nested interface which will handle my data from the form to json string and eventually to the database....? any idea on how should i do this? – user2056245 Feb 25 '13 at 03:20
  • Well I'm not sure why you put it with an Android tag then. There's many ways in which you can communicate between two instances of two classes. The most obvious one is to have a reference to the other one and just call a method. – Juan Andrés Diana Feb 25 '13 at 03:22
  • It would be easier to see the problem if you posted some code. I don't know why you need that inner interface. – Juan Andrés Diana Feb 25 '13 at 03:27
  • i just wanted solution for class B to access the data from my activity..... and the same class B will be used by many other activities..... so i just wondered whether this could be done using interfaces....making all those activities implement the interface.... so that data can be communicated by implementing those methods differently in particular activities – user2056245 Feb 25 '13 at 03:36
  • You can declare an interface (not attached to a class) and then implement that interface in different classes B1, B2, B3 to process the data differently. Then your activities A1, A2, A3 will use that common interface but the instantiated object of class B should be the corresponding one. – Juan Andrés Diana Feb 25 '13 at 03:54
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

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

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
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.

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);
chm
  • 1,519
  • 13
  • 21