user3728517 user3728517 - 27 days ago 7
Java Question

How to access preexcute data in doinbackground?

I fetch a database and display value in a spinner in preexcute value.in doinbackground thread i want to insert that spinner value in a sqlite table,I know that i can not use ui component in doinbackground. Expecting your help.
Here are my preexcute value

try {
statement = connect.prepareStatement("exec dbo.getSalesIdtest;");
} catch (SQLException e) {
Log.e("Error 1 here: ", e.getMessage());
}

try {
rs = statement.executeQuery();
} catch (SQLException ex) {
Log.e("error 2 here: ", ex.getMessage());
}
try {
while(rs.next()) {
TextView generateData = (TextView) findViewById(R.id.getOrderid);
pab = rs.getString("CurrentNo");
generateData.setText(pab);
Log.e(pab,"Message here is");
boolean isInserted = orderDB.insertorderData(rs.getString("CurrentNo"));

if (isInserted == true)
Toast.makeText(orderChalan.this, "Data save successfully", Toast.LENGTH_LONG).show();
else
Toast.makeText(orderChalan.this, "Data not Inserted", Toast.LENGTH_LONG).show();


and here is code in my doinbackground

if (con == null) {
z = "Check Your Internet Access!";
} else {
String query = "insert into dbo.slsOrderD(osdSalesID,osdProductID,osdQty)values('joy','" + getRata.getString(1).toString() + "','" + getRata.getString(2).toString() + "');";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.executeUpdate();
z = "New client's data added successfully";
isSuccess = true;
}
} catch (Exception ex) {
isSuccess = false;
z = "Exceptions";
Log.e("ERRO", ex.getMessage());
}


In insert statement i have to access data from pre execute spinner element how to solve this issue?

Answer

If you declare variables that you are updating in your onPreExecute() as class members of your inherited version of Asynctask, then you should be able to access them from the doInBackground().

From what I see you are declaring them as local variables in OnPreExecute()