Setting the text

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

Setting the text

Postby gtang » Tue Mar 08, 2011 1:20 am

I have been having this problem for a while. I am making a GCF calculator and I can't get the Text View to set Text to the text from my Edit TExt. Its apart of euclid's algorithm.
Code: Select all
package com.gcf;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class gcf extends Activity {
   
   /** Called when the activity is first created. */


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
     
        final EditText a = (EditText) findViewById(R.id.number1);
        final EditText b = (EditText) findViewById(R.id.number2);
        Button solve = (Button) findViewById(R.id.widget32);
        TextView answer = (TextView) findViewById(R.id.answer);
       
        solve.setOnClickListener(new View.OnClickListener() {
           public void onClick(View answer)  {
                 if (a.getText().length() == 0 );
                   answer.setText(b.getText());
                       
              
              
              
           
              
           }
           
        });
   }
}

If you could help that would be great
gtang
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Mar 08, 2011 1:14 am

Top

Re: Setting the text

Postby raj_J2ME » Tue Mar 08, 2011 6:58 am

public void onClick(View answer) {
if (a.getText().length() == 0 );
answer.setText(b.getText());


I think you are setting the text inside the if condition, and I am clearly able to see that the if condition is terminated as you have mistakenly set the semi colon (;). Check after removing the same,
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Setting the text

Postby JoxTraex » Wed Mar 09, 2011 3:42 am

You know that you have to take the text from a editText and convert it to a string?:

[Edittext].getText().toString()
JoxTraex
Developer
Developer
 
Posts: 25
Joined: Wed Jan 26, 2011 12:29 am

Re: Setting the text

Postby gtang » Wed Mar 09, 2011 5:52 am

The eclipse engine said therror was this The method setText(Editable) is undefined for the type View Im not sure how I would go about defining the method
gtang
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Mar 08, 2011 1:14 am

Re: Setting the text

Postby raj_J2ME » Wed Mar 09, 2011 6:31 am

gtang wrote:The eclipse engine said therror was this The method setText(Editable) is undefined for the type View Im not sure how I would go about defining the method

Have you tried after removing the semicolon from the if condition?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Setting the text

Postby gtang » Thu Mar 10, 2011 5:03 am

yes i have removed the semicolon it hasn't worked
gtang
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Mar 08, 2011 1:14 am

Top

Re: Setting the text

Postby raj_J2ME » Thu Mar 10, 2011 6:43 am

gtang wrote:yes i have removed the semicolon it hasn't worked

Hi, please check that whether the control is coming inside the if condition or not by putting the System.out.println().
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Setting the text

Postby gtang » Sat Mar 12, 2011 5:11 am

I don't quite understand what you are talking about
gtang
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Mar 08, 2011 1:14 am

Re: Setting the text

Postby raj_J2ME » Mon Mar 14, 2011 6:59 am

gtang wrote:I don't quite understand what you are talking about

What I am saying is that put some system.out.println() inside the code which is supposed to execute to set the text, and make the sure that you are setting the string but it is not getting reflected,
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Setting the text

Postby ruelblapid » Mon Mar 14, 2011 7:54 am

Hi gtang,

The problem is the signature/parameter that has been set to your onClick event
It has the same variable name with the TextView that you've declared.

Here's the problem:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TextView answer = (TextView) findViewById(R.id.answer);
  2. public void onClick(View answer)  
  3.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


both use the same variable name (answer) you should change the other one like View v instead of View answer
so it wont conflict to other code. Also remove the semicolon at the right side of the if condition and use toString() function when getting values from EditText.

I corrected the code that you've pasted above and here's my version.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         setContentView(R.layout.main);
  2.         final EditText a = (EditText)findViewById(R.id.number1);
  3.         final EditText b = (EditText)findViewById(R.id.number2);
  4.         Button solve = (Button) findViewById(R.id.solve);
  5.         final TextView answer = (TextView) findViewById(R.id.answer);
  6.        
  7.         solve.setOnClickListener(new View.OnClickListener() {
  8.             public void onClick(View v)  {
  9.                   if (a.getText().toString().length() == 0 )
  10.                   {
  11.                           answer.setText(b.getText().toString());
  12.                   }
  13.             }
  14.            
  15.          });
  16.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 10 guests