android - how to set dialog or layouts in runnable of threads? -


i checking weather username , password present in table of db.if username , password present setting layout setcontentview(r.layout.activity_main1) , if not showing dialog message dialog.setmessage("your username , password not valid").it showing error cant set view in threads.is alternate way show layouts or have written code wrong.

    package com.example.loginandroid;  import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset;  import java.sql.sqlexception; import java.sql.statement; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.util.log; import android.view.view; import android.widget.edittext; import android.widget.toast; import android.os.looper;  public class mainactivity extends activity{     string username,password;     resultset rs =null;     boolean temcfag=false,temqfag=true;     public static string tag="lifecycle activity";     edittext user,pass;     alertdialog.builder dialog;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         dialog=new alertdialog.builder(this);         dialog.setneutralbutton("ok", null);           user=(edittext)findviewbyid(r.id.edittext1);         pass=(edittext)findviewbyid(r.id.edittext2);     }      thread  thrd1,thrd2,thrd3;     connection con;       string result ="",queryexct;      public void dbconctfun(view v )  throws sqlexception {          try {               class.forname("com.mysql.jdbc.driver");          } catch (classnotfoundexception e) {             e.printstacktrace();          }          thrd1 = new thread(new runnable() {             public void run() {                 while (!thread.interrupted()) {                     try {                         thread.sleep(100);                     } catch (interruptedexception e1) {                      }                     if (con == null) {                         try {                               con = drivermanager.getconnection("jdbc:mysql://111.111.11.11:6666/dyne", "root1", "mysql");                          } catch (sqlexception e) {                             e.printstacktrace();                             con = null;                         }                          if ((thrd2 != null) && (!thrd2.isalive()))                             thrd2.start();                      }                 }              }         });         if ((thrd1 != null) && (!thrd1.isalive())) thrd1.start();          thrd2 = new thread(new runnable() {             public void run() {                 while (!thread.interrupted()) {                      if (con != null) {                         if (temqfag) {                         try {statement st = con.createstatement();                         username=user.gettext().tostring().trim();                             password=pass.gettext().tostring().trim();                             queryexct="select *  `user_registration`  `email_id` =  '"+username+"' ,  `password` =  '"+password+"'";                              rs = st.executequery(queryexct);                              log.v("queryexct",queryexct);                             temqfag=false;                             if(rs!=null){               if (rs.next()) {         looper.prepare();          thread.interrupted();                        setcontentview(r.layout.activity_main1);                      }              else{               thread.interrupted();             looper.prepare();                     dialog.setmessage("your username , password not valid");                 dialog.show();                }}                                        } catch (sqlexception e) {                             e.printstacktrace();                             con = null;                         }                          try {                             log.v("test#######","errorrrrrrrrrrr3");                             if (temqfag) {thread.sleep(10);}                         } catch (interruptedexception e) {                             e.printstacktrace();                         }}                     } else {                         try {                         log.v("test#######","errorrrrrrrrrrr4");                             thread.sleep(300);                         } catch (interruptedexception e) {                             e.printstacktrace();                         }                     }                 }             }          });        }   } 

you can use handler class runonuithread method of activity

here small example handler class

 private final handler handler = new handler() { // put in somewhere in activity              public void handlemessage(message msg) {                      if (msg.arg1 == 1) {                          setcontentview(r.layout.activity_main1);                      } else {                          dialog.setmessage("your username , password not valid");                          dialog.show();                      }             }  } 

for code

if (rs.next()) {     looper.prepare();     thread.interrupted();      message msgobj = handler.obtainmessage();     msgobj.arg1 = 1;     handler.sendmessage(msgobj);  } else {     thread.interrupted();     looper.prepare();      message msgobj = handler.obtainmessage();     msgobj.arg1 = 0;     handler.sendmessage(msgobj); } 

you can check tutorial here


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -