0

my goal is to insert to a certain db 2 values, id and pass. I have a registeration page which asks for that data and a button to complete the action. So on the button listener what should I do?many told me to use AsyncTask (which I don't know to use) instead of Thread. Remember that this class needs to get 2 parameters id and pass .. and as far as I know threads starts after using the start() method which invoke the run method, and the run method has no parameters.. so how can I pass those 2 parameters? Anyway I'm very confused. Another thing is that if I get any kind of error on the catch block I will put the error on a certain string something like : String error = exceptionInstance.toString(); and then I can take see that string from the registeration page and print the error.

myThreadInstance.start(); textViewInstance.setText(myThreadInstance.getError());

It's some kind of a marathon.. I'M CONFUSED!!!!!!!

Imri Persiado
  • 1,857
  • 8
  • 29
  • 45
  • You are asking too many question in one question. However it would be help if you show us your code snippent and stacktrace. – Smit Dec 20 '12 at 18:40
  • I don't want a code based answer. I wan't a direction. which class to use to execute that proccess and how?You have 2 values and you want to send them to another class that will insert them to a mysql db. how would you do that?it's cant be done on the main thread since you'll get an exception – Imri Persiado Dec 20 '12 at 18:46
  • Are you talking about passing values to constructor in class or methods in class. – Smit Dec 20 '12 at 18:50
  • I don't know how to pass those values, that's the thing! I mean you can't pass values to a Thread... so how it can be done? – Imri Persiado Dec 20 '12 at 18:51

1 Answers1

1

According to me use AsyncTask instead of an Thread because it's easy to use and you have better control on Background thread without doing extra code for creating separate logic for updating Ui when Thread execution complete, calculate progress units to so user how much time take by an operation to done etc

Your First question how you send username and password to AsyncTask on button click .for this use AsyncTask Constructor as:

LoginOperation loginopertion=new LoginOperation(strusername, strpassword);
loginopertion.execute("");

Your Second answer how we receive username and password in AsyncTask and update Ui when Task complete for this use onPostExecute of AsyncTask to update Ui when doInBackground execution complete for example :

public class LoginOperation extends AsyncTask<String, Void, String> {
    String strusername,strpassword;

    public LoginOperation(String strusername, String strpassword){
        this.strusername=strusername;
        this.strpassword=strpassword;
    }
    @Override
    protected void onPreExecute() {
        //show progressbar here 
    }
    @Override
    protected String doInBackground(String... params) {
        string result="";
        try
        {
            result=="success or fail";
            //do your network opertion here
        }
        catch(SQLException e)
        {
            result="ERROR";
        }
        return result;
    }      

    @Override
    protected void onPostExecute(String resultmsg) {    
        // show error here and update UI
        //or other opertion if login success
        textViewInstance.setText(resultmsg);
    }

}

For more information about AsyncTask method's see

http://developer.android.com/reference/android/os/AsyncTask.html

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • btw in order to do that: textViewInstance.setText(resultmsg); I need to send the textViewInstance as a parameter to this class right? – Imri Persiado Dec 20 '12 at 20:20
  • @ImriPersiado : if you have separate class for AsyncTask then you will need to send Activity Context for accessing TextView in onPostExecute see [this](http://stackoverflow.com/questions/6030982/android-how-to-access-activity-ui-from-my-class) post for more help on this point – ρяσѕρєя K Dec 20 '12 at 20:27
  • 1
    @ImriPersiado : and also declare all UI elements as class level fields instead of inside methods for accessing in non UI class – ρяσѕρєя K Dec 20 '12 at 20:28