Problem with onClickListener()

Put your problem here if it does not fit any of the other categories.

Problem with onClickListener()

Postby gvkreddyvamsi » Fri Feb 22, 2008 12:16 pm

Hi,

I am working on button click manipulations.
Is it changed all Button.OnClickListener in new SDK...

Button.onclickListener(this); giving error..

Here i s my code..
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.androidi.webservices;
  2.  
  3.  
  4.  
  5. import org.apache.commons.httpclient.HttpClient;
  6.  
  7. import org.apache.commons.httpclient.methods.GetMethod;
  8.  
  9.  
  10.  
  11. import android.app.Activity;
  12.  
  13. import android.ipc.IpcSocket.Listener;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.util.Log;
  18.  
  19. import android.view.View;
  20.  
  21. import android.view.ViewManager;
  22.  
  23. import android.widget.Button;
  24.  
  25. import android.widget.EditText;
  26.  
  27. import android.widget.TextView;
  28.  
  29. import android.widget.SimpleCursorAdapter.ViewBinder;
  30.  
  31.  
  32.  
  33. public class CallingWebServices extends Activity  {
  34.  
  35.     /** Called when the activity is first created. */
  36.  
  37.         // UI elements
  38.  
  39.     private Button Invoke;
  40.  
  41.     private EditText Fromc;
  42.  
  43.     private EditText Toc;    
  44.  
  45.     private EditText Resultc;
  46.  
  47.  
  48.  
  49.        
  50.  
  51.        
  52.  
  53.     @Override
  54.  
  55.     public void onCreate(Bundle icicle) {
  56.  
  57.         super.onCreate(icicle);
  58.  
  59.         setContentView(R.layout.main);
  60.  
  61.              
  62.  
  63.        
  64.  
  65.      // Gather the troops
  66.  
  67.         Fromc = (EditText) findViewById(R.id.fromc);
  68.  
  69.         Toc = (EditText) findViewById(R.id.toc);        
  70.  
  71.         Resultc = (EditText) findViewById(R.id.resultc);
  72.  
  73.         Invoke = (Button) findViewById(R.id.invoke);        
  74.  
  75.         Invoke.setOnClickListener(this); // <--------
  76.  
  77.          
  78.  
  79.        
  80.  
  81.          
  82.  
  83.        
  84.  
  85.         HttpClient client = new HttpClient();
  86.  
  87.  
  88.  
  89.         GetMethod get = new GetMethod("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency="+Fromc+"&ToCurrency="+Toc);
  90.  
  91.  
  92.  
  93.         try {
  94.  
  95.                         int status = client.executeMethod(get);
  96.  
  97.                         TextView resultado=(TextView)findViewById(R.id.resultc);
  98.  
  99.                         String res="";
  100.  
  101.  
  102.  
  103.                         if(status!=404)
  104.  
  105.                                 res=get.getResponseBodyAsString();
  106.  
  107.                         else
  108.  
  109.                                 res=getString(R.string.ws_not_found);
  110.  
  111.                         resultado.setText(res);
  112.  
  113.                 }
  114.  
  115.                 catch (Exception e) {
  116.  
  117.                         Log.e("Error:",e.getMessage());
  118.  
  119.                 }
  120.  
  121.                 finally {
  122.  
  123.                         get.releaseConnection();
  124.  
  125.                         get=null;
  126.  
  127.                 }
  128.  
  129.              
  130.  
  131.     }
  132.  
  133.  
  134.  
  135. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

And which interface is needed to imlement to set button listener/...

Thanks in advance...

by vamsi
gvkreddyvamsi
Developer
Developer
 
Posts: 43
Joined: Mon Jan 21, 2008 5:10 am
Location: INDIA

Top

Postby baskar » Fri Feb 22, 2008 1:41 pm

Hi gvkreddyvamsi,

what u r problem..
same method is used to new sdk....
u implement this function?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private OnClickListener mButtonListener = new OnClickListener()
  2.  
  3. {
  4.  
  5.     public void onClick(View v)
  6.  
  7.     {
  8.  
  9. }
  10.  
  11. };
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Thanks,
Baskar
baskar
Developer
Developer
 
Posts: 46
Joined: Wed Dec 26, 2007 6:03 am
Location: India

Postby plusminus » Fri Feb 22, 2008 2:08 pm

Hello vamsi,

you need to implements the following interface:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CallingWebServices extends Activity implements View.OnClickListener{
  2.  
  3.  
  4.  
  5. //....
  6.  
  7.  
  8.  
  9.    public void onClick(View v){
  10.  
  11.       // TODO
  12.  
  13.    }
  14.  
  15. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


or set an anonymous OnClickListener to the Button, like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Invoke.setOnClickListener(new View.OnClickListener(){
  2.  
  3.    public void onClick(View v){
  4.  
  5.       // TODO
  6.  
  7.    {
  8.  
  9. });
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

problem using annonymous class

Postby gvkreddyvamsi » Mon Feb 25, 2008 6:13 am

Hi plusminus,

I have used anonymous onClickListener . But giving error that

" The type new View.OnClickListener(){} must implement the inherited abstract method
View.OnClickListener.onClick(View) "


Is it need to import any new classes? Already i was used statement import android.view.View.OnClickListener;

pls ..

by

vamsi
gvkreddyvamsi
Developer
Developer
 
Posts: 43
Joined: Mon Jan 21, 2008 5:10 am
Location: INDIA

Postby baskar » Mon Feb 25, 2008 7:55 am

Hi,

please send your error message..

Regards,
Baskar
baskar
Developer
Developer
 
Posts: 46
Joined: Wed Dec 26, 2007 6:03 am
Location: India

Postby gvkreddyvamsi » Mon Feb 25, 2008 8:04 am

Hi,

I have used button anonymous class as like below..

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Invoke.setOnClickListener(new <span style="font-weight: bold">View.OnClickListener()</span>{
  2.    public void onClick(View v){
  3.       // TODO
  4.    {
  5. });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



But showing error at bolded statement.

Error Message: The type new View.OnClickListener(){} must implement the inherited abstract method
View.OnClickListener.onClick(View)

by
vamsi
gvkreddyvamsi
Developer
Developer
 
Posts: 43
Joined: Mon Jan 21, 2008 5:10 am
Location: INDIA

Top

Postby baskar » Mon Feb 25, 2008 8:12 am

Hi,

Hai U used to this simple code
i hope it is perfectly done..

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.bas;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import android.app.Activity;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.view.View;
  12.  
  13. import android.view.View.OnClickListener;
  14.  
  15. import android.widget.Button;
  16.  
  17.  
  18.  
  19.     public class Sample extends Activity{
  20.  
  21.         public void onCreate(Bundle icicle) {
  22.  
  23.         super.onCreate(icicle);
  24.  
  25.         setContentView(R.layout.main);
  26.  
  27.         Button button = (Button)findViewById(R.id.button1);
  28.  
  29.         button.setOnClickListener(mButtonListener);
  30.  
  31.     }
  32.  
  33.    
  34.  
  35.     private OnClickListener mButtonListener = new OnClickListener(){
  36.  
  37.         public void onClick(View v)
  38.  
  39.         {
  40.  
  41.                
  42.  
  43.                 Log.e("Message","U Click Button");
  44.  
  45.                
  46.  
  47.         }
  48.  
  49.     };
  50.  
  51. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Regards,
Baskar
baskar
Developer
Developer
 
Posts: 46
Joined: Wed Dec 26, 2007 6:03 am
Location: India

Postby gvkreddyvamsi » Mon Feb 25, 2008 8:27 am

HI,

Thank u very much ... now it is working well..



by
vamsi
gvkreddyvamsi
Developer
Developer
 
Posts: 43
Joined: Mon Jan 21, 2008 5:10 am
Location: INDIA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 19 guests