Advertisement
Guest User

Untitled

a guest
Jan 25th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. package com.tutecentral.restfulapiclient;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.support.v4.app.NavUtils;
  13. import android.annotation.TargetApi;
  14. import android.content.Intent;
  15. import android.os.Build;
  16.  
  17. public class CreateUserActivity extends Activity {
  18.  
  19.       EditText etfirstName, etLastName, etUsername, etPassword;
  20.       Button btnCreateUser;
  21.  
  22.       @Override
  23.       protected void onCreate(Bundle savedInstanceState) {
  24.             super.onCreate(savedInstanceState);
  25.             setContentView(R.layout.activity_create_user);
  26.             // Show the Up button in the action bar.
  27.             setupActionBar();
  28.  
  29.             etfirstName =(EditText) findViewById(R.id.et_fisrtname);
  30.             etLastName = (EditText) findViewById(R.id.et_lastname);
  31.             etUsername = (EditText) findViewById(R.id.et_cu_username);
  32.             etPassword = (EditText) findViewById(R.id.et_cu_password);
  33.             btnCreateUser=(Button) findViewById(R.id.btn_createuser);
  34.  
  35.             btnCreateUser.setOnClickListener(new View.OnClickListener() {
  36.  
  37.                   @Override
  38.                   public void onClick(View v) {
  39.                         // TODO Auto-generated method stub
  40.  
  41.                         String firstname, lastname, username, password;
  42.  
  43.                         firstname = etfirstName.getText().toString();
  44.                         lastname = etLastName.getText().toString();
  45.                         username = etUsername.getText().toString();
  46.                         password = etPassword.getText().toString();
  47.  
  48.                         UserDetailsTable userDetail = new UserDetailsTable(firstname,
  49.                                     lastname, username, password);
  50.  
  51.                         new AsyncCreateUser().execute(userDetail);
  52.  
  53.                   }
  54.             });
  55.  
  56.       }
  57.  
  58.       protected class AsyncCreateUser extends
  59.                   AsyncTask<UserDetailsTable, Void, Void> {
  60.  
  61.             @Override
  62.             protected Void doInBackground(UserDetailsTable... params) {
  63.  
  64.                   RestAPI api = new RestAPI();
  65.                   try {
  66.  
  67.                         api.CreateNewAccount(params[0].getFirstName(),
  68.                                     params[0].getLastName(), params[0].getUserName(),
  69.                                     params[0].getPassword());
  70.  
  71.                   } catch (Exception e) {
  72.                         // TODO Auto-generated catch block
  73.                         Log.d("AsyncCreateUser", e.getMessage());
  74.  
  75.                   }
  76.                   return null;
  77.             }
  78.  
  79.             @Override
  80.             protected void onPostExecute(Void result) {
  81.  
  82.                   Intent i = new Intent(CreateUserActivity.this, LoginActivity.class);
  83.                   startActivity(i);
  84.             }
  85.  
  86.       }
  87.  
  88.       /**
  89.        * Set up the {@link android.app.ActionBar}, if the API is available.
  90.        */
  91.       @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  92.       private void setupActionBar() {
  93.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  94.                   getActionBar().setDisplayHomeAsUpEnabled(true);
  95.             }
  96.       }
  97.  
  98.       @Override
  99.       public boolean onCreateOptionsMenu(Menu menu) {
  100.             // Inflate the menu; this adds items to the action bar if it is present.
  101.             getMenuInflater().inflate(R.menu.create_user, menu);
  102.             return true;
  103.       }
  104.  
  105.       @Override
  106.       public boolean onOptionsItemSelected(MenuItem item) {
  107.             switch (item.getItemId()) {
  108.             case android.R.id.home:
  109.                   // This ID represents the Home or Up button. In the case of this
  110.                   // activity, the Up button is shown. Use NavUtils to allow users
  111.                   // to navigate up one level in the application structure. For
  112.                   // more details, see the Navigation pattern on Android Design:
  113.                   //
  114.                   // http://developer.android.com/design/patterns/navigation.html#up-vs-back
  115.                   //
  116.                   NavUtils.navigateUpFromSameTask(this);
  117.                   return true;
  118.             }
  119.             return super.onOptionsItemSelected(item);
  120.       }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement