Value always return 0 where it should return number of row inserted Value always return 0 where it should return number of row inserted android android

Value always return 0 where it should return number of row inserted


As per your code to add information:

a=addInformation(name,weather,date2,status,first1[1],last1[1]);

Here, you are calling addInformation on main thread and hold the return value to variable a. But the problem is, your actual implementation of adding information is done in doInBackground of AsyncTask which actually does the job on a worker thread (async call) and so you will always get the default value for a=0 (as a is long).

What you need to do is to move your code to onPostExecute function of AsyncTask like below:

public void addInformation( final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut)    {        class AddInfo extends AsyncTask<String, Void, String> {            ProgressDialog loading;            @Override            protected void onPreExecute() {                super.onPreExecute();                loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);            }            @Override            protected void onPostExecute(String s) {                super.onPostExecute(s);                loading.dismiss();                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();                addWorkForce(Sub, NoP, NoH,a);            }            @Override            protected String doInBackground(String... params) {                HashMap<String, String> data = new HashMap<String,String>();                data.put(Config.KEY_USER_NAME,name);                data.put(Config.KEY_WEATHER,weather);                data.put(Config.KEY_DATE,date2);                data.put(Config.KEY_STATUS,status);                data.put(Config.KEY_TIMEIN,timeIn);                data.put(Config.KEY_TIMEOUT,timeOut);                RequestHandler rh=new RequestHandler();                String result = rh.sendPostRequest(Config.ADD_INFORMATION,data);                return  result;            }        }         AddInfo ru = new AddInfo();         ru.execute(name,weather,date2,status,timeIn,timeOut);    }

There is another approach to use callbacks. Implement an interface and after completion of your AsyncTask, you can send callback to do another job.


I believe the issue is coming from this line

return 0;

Your addInformation method returns a long with a value of 0 every time. If you want it to return some other number you will need to change that line.

Edit: To keep track of your row, add another parameter to your method header

public long addInformation(int rowId, final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut)


You can use the lastInsertId() mysql function after the row inserted in first table.that is (Information)store the value of lastInsertId() in some variable and use that variable at that time when you insert row in next table. that is (WorkForce)