Question

I have a table layout of 14 rows and each row is having 2 EditText Fields. I have button which the user clicks and the program computes and prints the answer. The problem is as soon as the user clicks the button the character entered in last EditText field disappears but when the user clicks on it it reappers. Please help me to solve this problem. Here is the xml file.

 <TableRow 
 android:id="@+id/tableRow2" 
 android:layout_width="fill_parent" 
 android:layout_height="50dp">

 <TextView 
 android:textSize="20sp" 
 android:typeface="sans" 
 android:textStyle="bold" 
 android:gravity="center" 
 android:id="@+id/sub1" 
 android:padding="10dp" 
 android:layout_width="120dp" 
 android:layout_height="wrap_content" 
 android:text="@string/sub1" />

 <EditText 
 android:gravity="center" 
 android:id="@+id/subject1" 
 android:padding="10dp" 
 android:layout_width="120dp" 
 android:layout_height="wrap_content" 
 android:inputType="text" >
 <requestFocus />
 </EditText>

 <EditText 
 android:gravity="center" 
 android:id="@+id/credits1" 
 android:padding="10dp" 
 android:layout_width="120dp" 
 android:layout_height="wrap_content" 
 android:inputType="phone" >
 <requestFocus />
 </EditText>

 </TableRow>

Here is on click code

public void onClick(View arg0) {
            // TODO Auto-generated method stub
            tv.setText("");
            num=0;
            den=0;
            f=0;

            if(dept.equalsIgnoreCase("cse")||dept.equalsIgnoreCase("mechtronics")||dept.equalsIgnoreCase("genetics")) {
                for(int i=0;i<12;i++) {
                    s[i]=sub[i].getText().toString();
                    g[i]=SetGP.setGP(s[i]);
                    c[i]=Float.parseFloat(cre[i].getText().toString());
                    if((g[i]==-1)&&((c[i]>=1)&&(c[i]<=4))) {
                        j=String.valueOf(i+1);
                        Toast.makeText(Gpa.this, "Please Enter Valid Grade For Subject "+j, Toast.LENGTH_LONG).show();
                        f=1;
                        break;
                    }   
                    else if((g[i]!=-1)&&((c[i]<1)||(c[i]>4))) {
                        j=String.valueOf(i+1);
                        Toast.makeText(Gpa.this, "Please Enter Valid Credits For Subject "+j, Toast.LENGTH_LONG).show();
                        f=1;
                        break;
                    }
                    else if((g[i]==-1)&&((c[i]<1)||(c[i]>4))) {
                        j=String.valueOf(i+1);
                        Toast.makeText(Gpa.this, "Please Enter Valid Grade and Credits For Subject "+j, Toast.LENGTH_LONG).show();
                        f=1;
                        break;
                    }
                }

                for(i=0;i<12;i++) {
                    num = CalcGPA.calcnum(g[i], c[i], num);
                    den = CalcGPA.calcden(c[i], den);
                }
            }
Was it helpful?

Solution 2

I solved it I just changed from RelativeLayout To LinearLayout That there solved the problem for me

OTHER TIPS

Your onClick has a reference to a global TextView variable ("tv") which it sets to ("") an empty String. Is this perhaps the reason the text in one of your views is disappearing when you click the button?

I realize your problem is the last view changing which appears to be an editText, but an editText is castable to TextView so it's something to check.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top