karthi karthi - 26 days ago 9
Android Question

Could not get the value from Edit Text element

Fantastic morg. Below this code to get data from

mysql
database and displayed into the
EditText
element.There is no problem with getting data from
db
its working good using this
asyn tesk
new checkUserPermission().execute("");
.

Problem is
I want to make some calculation from
code
and dispaly in another
Edittext
. so i need values thats why i get data from
db
.while
OnCreate()
to get the data from
db
(its working). whenever i call this
calculatePL();
method i could not get value.

LOGCAT:
System.out: Empty Value



Why its empty or something. but above my
edittext
elements hold the
values.


...some declaration of variables and etc....
public void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.five_activity);

new checkUserPermission().execute(""); //call here
calculatePL();//call the method
}

class checkUserPermission extends AsyncTask<String, String, String> {

private ProgressDialog Dialog = new ProgressDialog(Five_Activity.this);

@Override
protected void onPreExecute() {
Dialog.setMessage("Please wait..");
Dialog.show();
super.onPreExecute();
userid = (TextView)findViewById(R.id.userID);
uid = userid.getText().toString();
System.out.println(uid);
}

@Override
protected String doInBackground(String... arg0) {
ArrayList<NameValuePair> values = new ArrayList<NameValuePair>();

values.add(new BasicNameValuePair("userid", uid));

try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.13:8090/stat_api/shiftClose.php");
httppost.setEntity(new UrlEncodedFormEntity(values));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is2 = entity.getContent();
Log.i("TAG", "Connection Successful");

} catch (Exception e) {
Log.i("TAG", e.toString());
//Invalid Address
}

try {
BufferedReader reader2 = new BufferedReader(new InputStreamReader(is2, "iso-8859-1"), 8);
StringBuilder sb2 = new StringBuilder();
while ((line2 = reader2.readLine()) != null) {
sb2.append(line2 + "\n");
}
is2.close();
result2 = sb2.toString();

JSONObject json_data2 = new JSONObject(result2);
code2=(json_data2.getString("code"));

Allvalues = code2;
String[] splited = Allvalues.split("\\s+");

Totalkm=splited[0];
discountamt=splited[1];
receviedamt=splited[2];
totalamt=splited[3];
expen=splited[4];

//Log.d("Splited String ", "Splited String" + totalamt+expen);

runOnUiThread(new Runnable() {
@Override
public void run() {
totkm.setText(Totalkm);
discount.setText(discountamt);
recamt.setText(receviedamt);
totamt.setText(totalamt);
expenses.setText(expen);
}
});

Log.i("TAG", "Result Retrieved");

} catch (Exception e) {
Log.i("TAG", e.toString());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result){
// Close progress dialog
Dialog.dismiss();
}
}

public void calculatePL(){
try {
String a_value =totamt.getText().toString().trim();
System.out.println(a_value);

}catch(NumberFormatException numberEx)
{
System.out.println(numberEx);
}
}

Answer

Override protected void onPostExecute in your asyncTask and call calculatePl() here. And you should set Edittext's text in onPostExecute too, because this method is main thread and you don't need to use runOnUIThread.

EDIT with example code:

class checkUserPermission extends AsyncTask<String, String, String> {

        private ProgressDialog Dialog = new ProgressDialog(Five_Activity.this);

        @Override
        protected void onPreExecute() {
            Dialog.setMessage("Please wait..");
            Dialog.show();
            super.onPreExecute();
            userid = (TextView)findViewById(R.id.userID);
            uid = userid.getText().toString();
            System.out.println(uid);
        }

        @Override
        protected String doInBackground(String... arg0) {
            ArrayList<NameValuePair> values = new ArrayList<NameValuePair>();

            values.add(new BasicNameValuePair("userid", uid));

            try {
                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.1.13:8090/stat_api/shiftClose.php");
                httppost.setEntity(new UrlEncodedFormEntity(values));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is2 = entity.getContent();
                Log.i("TAG", "Connection Successful");

            } catch (Exception e) {
                Log.i("TAG", e.toString());
                //Invalid Address
            }

            try {
                BufferedReader reader2 = new BufferedReader(new InputStreamReader(is2, "iso-8859-1"), 8);
                StringBuilder sb2 = new StringBuilder();
                while ((line2 = reader2.readLine()) != null) {
                    sb2.append(line2 + "\n");
                }
                is2.close();
                result2 = sb2.toString();

                JSONObject json_data2 = new JSONObject(result2);
                code2=(json_data2.getString("code"));

                Allvalues = code2;


            } catch (Exception e) {
                Log.i("TAG", e.toString());
                e.printStackTrace();
            }
            return Allvalues;
        }
        protected void onPostExecute(String result){

            String[] splited = result.split("\\s+");

            Totalkm=splited[0];
            discountamt=splited[1];
            receviedamt=splited[2];
            totalamt=splited[3];
            expen=splited[4];


            totkm.setText(Totalkm);
            discount.setText(discountamt);
            recamt.setText(receviedamt);
            totamt.setText(totalamt);
            expenses.setText(expen);
            // Close progress dialog
            Dialog.dismiss();
            calculatePL();
        }
    }
Comments