I am trying to load some data in the main activity layout from the database, while this happens I want to show the user a dialog in the popup. I have tried using thread and also async task but still the screen freezes while the data is being loaded and finally when it completes then it displays my popup. I want the dialog to display simultaneously with data load. below is the function from main activity that loads the data and calls the async task.
public void call_itemdisplay(final String bookid)
{
new waitAsync().execute();
//load data from DB below
}
The async task that starts a new activity
public class waitAsync extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Intent i = new Intent(MainActivity.mCon, waitActivity.class);
MainActivity.mCon.startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
The Popup Activity that is started by the async task
public class waitActivity extends Activity {
static waitActivity wact;
public static Context wcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wact=this;
setContentView(R.layout.activity_wait);
}
}
Need help in showing the POPup activity in the UI while data loads in the main activity. Currently the screen freezes while loading data and then show all together after loading is complete
EDITED with new changes: The async task
public class waitAsync extends AsyncTask<String, Void, String> {
ResultSet rs=null;
private ProgressDialog processingDialog;
static Connection conn=null;
Statement stmt;
// waitThread wt= new waitThread();
@Override
protected void onPreExecute() {
// ProgressDialog processingDialog;
super.onPreExecute();
processingDialog = new ProgressDialog(MainActivity.mCon);
processingDialog.show();
processingDialog.setContentView(R.layout.activity_wait);
//processingDialog.setMessage("Loading...");
}
@Override
protected String doInBackground(String... params) {
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
String ip = "mfb.dfsdfsdf.ap-north-1.rds.amazonaws.com";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "MyDatabase";
String un = "xxxwww";
String password = "xxxxxx";
Class.forName(classs);
String ConnURL = null;
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(params[0]);
int size = 0;
//conn.close();
MainActivity.getdata=rs;
MainActivity.flag=1;
//Thread.sleep(5000);
} catch (Exception e)
{
Log.w("Error connection", "" + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
processingDialog.dismiss();
// wt.interrupted();
}
}
The call of async task from Main_Activity
Getdata(String bookid)
{
String SQL="Select eb.book_name,eb.AuthorID,ma.Name,eb.GenreID_M,eb.GenreID_D,gm.Genre_MN+'- '+gd.Genre_DN as Genre,isnull(eb.pubDate,'') pubDate,isnull(eb.book_Loc,'') book_Loc,\n" +
" eb.Price,eb.ico_Loc,eb.Descrip from Master_ebook eb inner join Master_Author ma on eb.AuthorID=ma.Author_ID\n" +
" inner join Master_Genre_M gm on eb.GenreID_M=gm.Genre_MID\n" +
"inner join Master_Genre_D gd on eb.GenreID_D= gd.Genre_DID where eb.bookID=" + bookid;
new waitAsync().execute(SQL);
int c=0;
while(MainActivity.flag==0)
{
c++;
}
if(MainActivity.flag==1)
{
MainActivity.flag=0;
//processingDialog.dismiss();
}
ResultSet rs=MainActivity.getdata;
}