0

I'm learning how to build Android apps and i'm building a login application.

I created two classes:

MainActivity

  usernameField = (EditText)findViewById(R.id.editText1);
  passwordField = (EditText)findViewById(R.id.editText2);

   public void loginGet(View view){
      String username = usernameField.getText().toString();
      String password = passwordField.getText().toString();
      new SigninActivity(this,status,role,1).execute(username,password);
   }

SigninActivity

   @Override
   protected void onPostExecute(String result){
       if(result == ""){
           this.statusField.setText("Wrong password.");
       }else{
           this.statusField.setText("Login Successful");
           this.roleField.setText(result);  
           // I would like to change the Layout here
       }
   }

How can i change the Layout when the user logs in?

I tried to put setContentView, but i received this warning:

The method setContentView(int) is undefined for the type SigninActivity

Thank you guys.

Update:

SigninActivity

public class SigninActivity  extends AsyncTask<String,Void,String>{

       private TextView statusField,roleField;
       private Context context;
       private int byGetOrPost = 0; 
       //flag 0 means get and 1 means post.(By default it is get.)
       public SigninActivity(Context context,TextView statusField,
       TextView roleField,int flag) {
          this.context = context;
          this.statusField = statusField;
          this.roleField = roleField;
       }

       protected void onPreExecute(){

       }
       @Override
       protected String doInBackground(String... arg0) {

             try{
                String username = (String)arg0[0];
                String password = (String)arg0[1];
                String link = "http://10.0.2.2/example/teste.php?username="
                        +username+"&password="+password;
                URL url = new URL(link);
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(link));
                HttpResponse response = client.execute(request);
                BufferedReader in = new BufferedReader
               (new InputStreamReader(response.getEntity().getContent()));

               StringBuffer sb = new StringBuffer("");
               String line="";
               while ((line = in.readLine()) != null) {
                  sb.append(line);
                  break;
                }
                in.close();
                return sb.toString();
          }catch(Exception e){
             return new String("Exception: " + e.getMessage());
          }
          }


       @Override
       protected void onPostExecute(String result){
           if(result == ""){
               this.statusField.setText("Wrong password.");
           }else{
               this.statusField.setText("Login Successful");
               this.roleField.setText(result);  

               MainActivity.setAuth(context);
               setContentView(R.layout.activity_main);
           }
       }
    }

MainActivity

public class MainActivity extends Activity {

   private EditText usernameField,passwordField;
   private TextView status,role,method;

   @Override 
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      usernameField = (EditText)findViewById(R.id.editText1);
      passwordField = (EditText)findViewById(R.id.editText2);
      status = (TextView)findViewById(R.id.textView6);
      role = (TextView)findViewById(R.id.textView7);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
   public void loginGet(View view){
      String username = usernameField.getText().toString();
      String password = passwordField.getText().toString();
      new SigninActivity(this,status,role,0).execute(username,password);

   }  
}
  • What class does your SigninActivity derive from? (i assume not activity? is it AsyncTask or something like that?) – yekretsaM Apr 03 '15 at 19:27
  • Try saving the context of MainActivity in a variable in SigninActivity and calling: context.setContentView(int)? – aProperFox Apr 03 '15 at 19:29
  • i assume what you want to do is start a new activity (a new screen) after your login stuff is done successfully. If thats the case have a look at this link: http://developer.android.com/training/basics/firstapp/starting-activity.html – yekretsaM Apr 03 '15 at 19:33
  • Thanks, i will check the link. – Antonio Jeremias Apr 03 '15 at 19:35
  • I did it, i inserted this piece of code: context.startActivity(new Intent(context,showlist.class)); – Antonio Jeremias Apr 03 '15 at 19:58

1 Answers1

0

You should look how to pass data from Async task back to Activity

AsyncTask : passing value to an Activity (onCreate method )

how do i send data back from onPostExecute in an AsyncTask?

Community
  • 1
  • 1
Nauman Afzaal
  • 1,046
  • 12
  • 20