using method inside asyntask in android -
i have created 1 simple login application takes user name , password sqlserver..it works fine...
i want during login process 1 progeress bar should displayed using asyntask... unaware use parameters in asyntask...if 1 plzz tell me how put method in doinbackground of asyntask , param should use....
my code is;.....
public void save(){ initilize(); resultset rs = null; string mylog=id.gettext().tostring(); string mypass=pass.gettext().tostring(); try{ statement statement=connect.createstatement(); rs=statement.executequery("login '"+mylog+"', '"+mypass+"'"); }catch(exception e){ e.printstacktrace(); } if(mylog.equals("")||mypass.equals("")){ toast.maketext(getapplicationcontext(), "empty fields", toast.length_short).show(); } else try { if(rs.next()){ intent i=new intent(getapplicationcontext(),act2.class); startactivity(i); } else if(rs.next()==false){ toast.maketext(getapplicationcontext(), "incorrect login", toast.length_short).show(); id.settext(""); pass.settext(""); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } }
if possible same method save() kept inside doinbackground() of asyntask...
making fast refactorization (note stand it's bad practice , coding, must refactor code more maintanable , avoid duplication):
public class myasynctask extends asynctask<> {
private activity activity; boolean result; private string mylog; private string mypass; private connection connect;
public myasynctask(activity activity, connection connect) { this.activity = activity; this.connect = connect; } @override protected void onpreexecute() { //show progress dialog } @override protected object doinbackground(object[] objects) { resultset rs = null; activity.runonuithread(new runnable() { @override public void run() { initilize(); mylog=id.gettext().tostring(); mypass=pass.gettext().tostring(); } }); try{ statement statement=connect.createstatement(); rs=statement.executequery("login '"+mylog+"', '"+mypass+"'"); }catch(exception e){ e.printstacktrace(); } if(mylog.equals("")||mypass.equals("")){ activity.runonuithread(new runnable() { @override public void run() { toast.maketext(activity.getapplicationcontext(), "empty fields", toast.length_short).show(); } }); } else try { if(rs.next()){ result = true; } else if(rs.next()==false){ activity.runonuithread(new runnable() { @override public void run() { toast.maketext(activity.getapplicationcontext(), "incorrect login", toast.length_short).show(); id.settext(""); pass.settext(""); } }); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } @override protected void onpostexecute(object o) { //hide progress dialog if(result == boolean.true){ activity.runonuithread(new runnable() { @override public void run() { intent i=new intent(activity.getapplicationcontext(),act2.class); activity.startactivity(i); } }); } } }
then in activity this:
myasynctask = new myasynctask(this, connect); //im guessing "connect" connection object a.execute();
as said made fast refactoring code work best practice , implementation not in consideration here.
Comments
Post a Comment