Problem with onClickListeners

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with onClickListeners

Postby kitty » Thu Dec 10, 2009 10:54 am

Hi all,

I'm writing an application in which i have two onclicklisteners, one for Dialog box and another one for Checkbox. I imported the packages android.view.view.onclicklistener , android.content.dialoginterface.onclicklistener to implement these listeners. I'm getting an error in my program COLLISION IN IMPORT STATEMENTS. Can i have two different onclicklisteners in my program or not???

Please help me in this scenario.

Thanks in advance
Kitty.
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Postby sandis84 » Thu Dec 10, 2009 4:40 pm

Hello good sir.

You dont necessarily need to import something to use it.

Instead of this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. *imports stuff*
  3.  
  4. myButton.setOnClickListener(new OnClickListener(){
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


you can write like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. myButton.setOnClickListener(new android.view.View.OnClickListener(){
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


this should solve your conundrum!

Cheers,
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby achie1266 » Thu Dec 10, 2009 5:07 pm

OnClickListener is the Interface which you implement while setting onClickListeners and Like Sandis84 said you donot have to import it twice.

You can either define your view class to implemnt the OnClickLictener and then implement the onClick method like this
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class YourClass extends Activity implements OnClickListener{
  2.  
  3.  
  4.  
  5. public void onCreate(Bundle savedInstanceState){
  6.  
  7. //your implmentation
  8.  
  9. }
  10.  
  11.  
  12.  
  13. public void onClick(View v){
  14.  
  15.  // Do your onclick implmentation here
  16.  
  17. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Or you can define it on your view like this
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. button.setOnClickListener(new OnClickListener(){
  2.  
  3.   public void onClick(View v){// your implementation}
  4.  
  5. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


In both the cases you import android.view.View.OnClickListener but eclipse can take care of that import

If you have trouble in understanding what to import and what not to leave it to Eclipse to do it[if you happen to use eclipse]
Use ctrl+space for hints after you start typing a class or method name.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby kitty » Fri Dec 11, 2009 7:56 am

Hi sandis,

Thanks for your reply. That is what exactly i want, i've used it in my code and its working fine.

Regards
Kitty
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests