Your Ad Here

Posted By

variable3 on 05/04/11


Tagged

form database using android UserRegistration


Versions (?)

Android UserRegistration Form using SQLite Database


 / Published in: Java
 

  1. DBAdapter
  2. ---------
  3. package com.v3.userform;
  4.  
  5.  
  6.  
  7. import android.content.ContentValues;
  8.  
  9. import android.content.Context;
  10.  
  11. import android.database.Cursor;
  12.  
  13. import android.database.SQLException;
  14.  
  15. import android.database.sqlite.SQLiteDatabase;
  16.  
  17. import android.database.sqlite.SQLiteOpenHelper;
  18.  
  19. import android.util.Log;
  20.  
  21.  
  22.  
  23. public class DBAdapter {
  24.  
  25.  
  26.  
  27. // Initial Configuration
  28.  
  29. public static final String DB_NAME = "userform";
  30.  
  31. private static final int DATABASE_VER = 1;
  32.  
  33. private static final String TAG = "DBAdapter";
  34.  
  35.  
  36.  
  37. private final Context context;
  38.  
  39. private DatabaseHelper DBHelper;
  40.  
  41. private SQLiteDatabase db;
  42.  
  43.  
  44.  
  45. // Set the Tables Key Words
  46.  
  47. public static final String TABLE_USERS = "users";
  48.  
  49. public static final String TABLE_CITIES = "cities";
  50.  
  51. public static final String TABLE_HOBBIES = "hobbies";
  52.  
  53. public static final String TABLE_USERS_HOBBIES = "users_hobbies";
  54.  
  55.  
  56.  
  57. // Table Queries
  58.  
  59. private static final String CREATE_USERS = "CREATE TABLE users (_id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT, sex TEXT, dob TEXT, city_id INTEGER);";
  60.  
  61. private static final String CREATE_CITIES = "CREATE TABLE cities (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE);";
  62.  
  63. private static final String CREATE_HOBBIES = "CREATE TABLE hobbies (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE);";
  64.  
  65. private static final String CREATE_USERS_HOBBIES = "CREATE TABLE users_hobbies (_id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, hobby_id INTEGER);";
  66.  
  67.  
  68.  
  69. // Keys
  70.  
  71. public static final String ID = "_id";
  72.  
  73. public static final String KEY_FNAME = "first_name";
  74.  
  75. public static final String KEY_LNAME = "last_name";
  76.  
  77. public static final String KEY_SEX = "sex";
  78.  
  79. public static final String KEY_DOB = "dob";
  80.  
  81. public static final String CID = "city_id";
  82.  
  83. public static final String KEY_NAME = "name";
  84.  
  85. public static final String KEY_UID = "user_id";
  86.  
  87. public static final String KEY_HID = "hobby_id";
  88.  
  89.  
  90.  
  91. static String[] hobbies = new String[] { "Dancing", "Singing", "Cycling",
  92.  
  93. "Swimming" };
  94.  
  95.  
  96.  
  97. public DBAdapter(Context ctx) {
  98.  
  99. this.context = ctx;
  100.  
  101. DBHelper = new DatabaseHelper(context);
  102.  
  103. }
  104.  
  105.  
  106.  
  107. private static class DatabaseHelper extends SQLiteOpenHelper {
  108.  
  109.  
  110.  
  111. public DatabaseHelper(Context context) {
  112.  
  113. super(context, DB_NAME, null, DATABASE_VER);
  114.  
  115. }
  116.  
  117.  
  118.  
  119. @Override
  120.  
  121. public void onCreate(SQLiteDatabase db) {
  122.  
  123. db.execSQL(CREATE_USERS);
  124.  
  125. db.execSQL(CREATE_CITIES);
  126.  
  127. db.execSQL(CREATE_HOBBIES);
  128.  
  129. db.execSQL(CREATE_USERS_HOBBIES);
  130.  
  131. System.out.println("Before ...");
  132.  
  133. ContentValues initialValues = new ContentValues();
  134.  
  135. for (int i = 0; i < hobbies.length; i++) {
  136.  
  137. initialValues.put(KEY_NAME, hobbies[i]);
  138.  
  139. db.insert(TABLE_HOBBIES, null, initialValues);
  140.  
  141. }
  142.  
  143. System.out.println("After ...");
  144.  
  145. }
  146.  
  147.  
  148.  
  149. @Override
  150.  
  151. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  152.  
  153. Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  154.  
  155. + newVersion + ", which will destroy all old data");
  156.  
  157. db.execSQL("DROP TABLE IF EXISTS users");
  158.  
  159. db.execSQL("DROP TABLE IF EXISTS cities");
  160.  
  161. db.execSQL("DROP TABLE IF EXISTS hobbies");
  162.  
  163. db.execSQL("DROP TABLE IF EXISTS users_hobbies");
  164.  
  165. onCreate(db);
  166.  
  167. }
  168.  
  169.  
  170.  
  171. }
  172.  
  173.  
  174.  
  175. public DBAdapter open() throws SQLException {
  176.  
  177. db = DBHelper.getWritableDatabase();
  178.  
  179.  
  180.  
  181. return this;
  182.  
  183. }
  184.  
  185.  
  186.  
  187. public void close() {
  188.  
  189. DBHelper.close();
  190.  
  191. }
  192.  
  193.  
  194.  
  195. // Custom Methods
  196.  
  197.  
  198.  
  199. // Check if City Exists
  200.  
  201. public Boolean checkCityName(String name) throws SQLException {
  202.  
  203. Cursor mCursor = db
  204.  
  205. .query(TABLE_CITIES, new String[] { ID, KEY_NAME }, KEY_NAME
  206.  
  207. + "=" + "'" + name + "'", null, null, null, null, null);
  208.  
  209.  
  210.  
  211. if (mCursor.moveToFirst()) {
  212.  
  213. return true;
  214.  
  215. }
  216.  
  217. return false;
  218.  
  219. }
  220.  
  221.  
  222.  
  223. // Insert data to user table
  224.  
  225. public long insertINuser(String fnm, String lnm, String sex, String dob,
  226.  
  227. int city_id) {
  228.  
  229. ContentValues initialValues = new ContentValues();
  230.  
  231. initialValues.put(KEY_FNAME, fnm);
  232.  
  233. initialValues.put(KEY_LNAME, lnm);
  234.  
  235. initialValues.put(KEY_SEX, sex);
  236.  
  237. initialValues.put(KEY_DOB, dob);
  238.  
  239. initialValues.put(CID, city_id);
  240.  
  241. return db.insert(TABLE_USERS, null, initialValues);
  242.  
  243. }
  244.  
  245.  
  246.  
  247. // Insert City
  248.  
  249. public long insertCity(String name) {
  250.  
  251. ContentValues initialValues = new ContentValues();
  252.  
  253. initialValues.put(KEY_NAME, name);
  254.  
  255. return db.insert(TABLE_CITIES, null, initialValues);
  256.  
  257. }
  258.  
  259.  
  260.  
  261. // Insert into user_hobby
  262.  
  263. public long insertUers_Hobbies(int uid, int hid) {
  264.  
  265. ContentValues initialValues = new ContentValues();
  266.  
  267.  
  268.  
  269. initialValues.put(KEY_UID, uid);
  270.  
  271. initialValues.put(KEY_HID, hid);
  272.  
  273. db.insert(TABLE_USERS_HOBBIES, null, initialValues);
  274.  
  275.  
  276.  
  277. return 5;
  278.  
  279. }
  280.  
  281.  
  282.  
  283. // City List
  284.  
  285. public Cursor cityList() throws SQLException {
  286.  
  287. return db.query(TABLE_CITIES, new String[] { ID, KEY_NAME }, null,
  288.  
  289. null, null, null, KEY_NAME + " ASC", null);
  290.  
  291. }
  292.  
  293.  
  294.  
  295. // List only user Fname
  296.  
  297. public Cursor listUser() {
  298.  
  299. System.out.println("listUser");
  300.  
  301. return db.query(TABLE_USERS, new String[] { ID, KEY_FNAME }, null,
  302.  
  303. null, null, null, null);
  304.  
  305.  
  306.  
  307. }
  308.  
  309.  
  310.  
  311. public int find_city_id(String city) {
  312.  
  313. Cursor c = db
  314.  
  315. .query(TABLE_CITIES, new String[] { ID, KEY_NAME }, KEY_NAME
  316.  
  317. + "=" + "'" + city + "'", null, null, null, null, null);
  318.  
  319. Cursor mCursor = db
  320.  
  321. .query(TABLE_CITIES, new String[] { ID, KEY_NAME }, KEY_NAME
  322.  
  323. + "=" + "'" + city + "'", null, null, null, null, null);
  324.  
  325.  
  326.  
  327. mCursor.moveToFirst();
  328.  
  329.  
  330.  
  331. System.out.println(mCursor.getString(0));
  332.  
  333. if (c.moveToFirst()) {
  334.  
  335. do {
  336.  
  337. return Integer.parseInt(c.getString(0));
  338.  
  339. } while (c.moveToNext());
  340.  
  341. } else
  342.  
  343. return -1;
  344.  
  345.  
  346.  
  347. }
  348.  
  349.  
  350.  
  351. // Get city Name
  352.  
  353. public Cursor find_city_name(int cityID) {
  354.  
  355. return db.query(TABLE_CITIES, new String[] { ID, KEY_NAME }, ID + "="
  356.  
  357. + "'" + cityID + "'", null, null, null, null, null);
  358.  
  359. }
  360.  
  361.  
  362.  
  363. // Get the user data
  364.  
  365. public Cursor getUserData(String fNm) {
  366.  
  367. return db.query(TABLE_USERS, new String[] { ID, KEY_FNAME, KEY_LNAME,
  368.  
  369. KEY_SEX, KEY_DOB, CID }, KEY_FNAME + "='" + fNm + "'", null,
  370.  
  371. null, null, null, null);
  372.  
  373. }
  374.  
  375.  
  376.  
  377. // Get user hobbies
  378.  
  379. public Cursor getUserHobbies(int uid) {
  380.  
  381. return db.query(TABLE_USERS_HOBBIES, new String[] { ID, KEY_UID,
  382.  
  383. KEY_HID }, KEY_UID + "='" + uid + "'", null, null, null, null,
  384.  
  385. null);
  386.  
  387. }
  388.  
  389.  
  390.  
  391. // Get single User Data
  392.  
  393. public Cursor getUsersData(int uid) {
  394.  
  395. return db.query(TABLE_USERS, new String[] { ID, KEY_FNAME, KEY_LNAME,
  396.  
  397. KEY_SEX, KEY_DOB, CID }, ID + "='" + uid + "'", null, null,
  398.  
  399. null, null, null);
  400.  
  401. }
  402.  
  403.  
  404.  
  405. // Get All user data
  406.  
  407. public Cursor getAllDATA() {
  408.  
  409. return db.query(TABLE_USERS, new String[] { ID, KEY_FNAME, KEY_LNAME,
  410.  
  411. KEY_SEX, KEY_DOB, CID }, null, null, null, null, null);
  412.  
  413. }
  414.  
  415.  
  416.  
  417. }
  418.  
  419.  
  420.  
  421.  
  422. HomeActivity
  423. -------------
  424. package com.v3.userform;
  425.  
  426. import java.util.Calendar;
  427. import android.app.Activity;
  428. import android.app.AlertDialog;
  429. import android.app.DatePickerDialog;
  430. import android.app.Dialog;
  431. import android.content.DialogInterface;
  432. import android.content.Intent;
  433. import android.database.Cursor;
  434. import android.os.Bundle;
  435. import android.view.Menu;
  436. import android.view.MenuInflater;
  437. import android.view.MenuItem;
  438. import android.view.View;
  439. import android.widget.Button;
  440. import android.widget.CheckBox;
  441. import android.widget.DatePicker;
  442. import android.widget.EditText;
  443. import android.widget.LinearLayout;
  444. import android.widget.RadioButton;
  445. import android.widget.SimpleCursorAdapter;
  446. import android.widget.Spinner;
  447. import android.widget.TextView;
  448. import android.widget.Toast;
  449.  
  450. public class HomeActivity extends Activity {
  451.  
  452. private TextView mDateDisplay;
  453. private Button mPickDate;
  454. private int mYear;
  455. private int mMonth;
  456. private int mDay;
  457. private String sex;
  458. DBAdapter db;
  459. static final int DATE_DIALOG_ID = 0;
  460. CheckBox chk1, chk2, chk3, chk4;
  461.  
  462. public void onCreate(Bundle savedInstanceState) {
  463. super.onCreate(savedInstanceState);
  464. setContentView(R.layout.main);
  465.  
  466. // Date Picker
  467. // capture our View elements
  468. mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
  469. mPickDate = (Button) findViewById(R.id.pickDate);
  470.  
  471. // add a click listener to the button
  472. mPickDate.setOnClickListener(new View.OnClickListener() {
  473. public void onClick(View v) {
  474. showDialog(DATE_DIALOG_ID);
  475. }
  476. });
  477.  
  478. // get the current date
  479. final Calendar c = Calendar.getInstance();
  480. mYear = c.get(Calendar.YEAR);
  481. mMonth = c.get(Calendar.MONTH);
  482. mDay = c.get(Calendar.DAY_OF_MONTH);
  483.  
  484. // display the current date (this method is below)
  485. updateDisplay();
  486.  
  487. // Database Conenction
  488. db = new DBAdapter(HomeActivity.this);
  489. db.open();
  490.  
  491. // Populating the City Spinner
  492. Cursor cities = db.cityList();
  493. startManagingCursor(cities);
  494.  
  495. // create an array to specify which fields we want to display
  496. String[] from = new String[] { DBAdapter.KEY_NAME };
  497. // create an array of the display item we want to bind our data to
  498. int[] to = new int[] { android.R.id.text1 };
  499.  
  500. Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);
  501. SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
  502. android.R.layout.simple_spinner_item, cities, from, to);
  503. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  504. cityList.setAdapter(adapter);
  505.  
  506. // On Click Save
  507. Button save = (Button) findViewById(R.id.btnSave);
  508. save.setOnClickListener(new View.OnClickListener() {
  509. public void onClick(View arg0) {
  510.  
  511. // Get the name
  512. EditText Fname = (EditText) findViewById(R.id.FirstName);
  513. EditText Lname = (EditText) findViewById(R.id.LastName);
  514.  
  515. String first_name = Fname.getText().toString();
  516. String last_name = Lname.getText().toString();
  517.  
  518. // Get Date of birth
  519. String DoB = (String) ((TextView) findViewById(R.id.dateDisplay))
  520. .getText();
  521.  
  522. // Get the Gender
  523. RadioButton rbM = (RadioButton) findViewById(R.id.rbMale);
  524.  
  525. if (rbM.isChecked()) {
  526. sex = "Male";
  527. } else {
  528. sex = "Female";
  529. }
  530.  
  531. // Get the City
  532. Spinner getCity = (Spinner) findViewById(R.id.citySpiner);
  533. String cityName = null;
  534. Cursor cc = (Cursor) (getCity.getSelectedItem());
  535. if (cc != null) {
  536. cityName = cc.getString(cc.getColumnIndex(db.KEY_NAME));
  537. }
  538. System.out.println("help :" + cityName);
  539.  
  540. Long idUser, idHobby, idUsersHobbies;
  541. db.open();
  542. // Inseret data into User Table
  543. int city_id = db.find_city_id(cityName);
  544. idUser = db.insertINuser(first_name, last_name, sex, DoB,
  545. city_id);
  546. int uid = idUser.intValue();
  547.  
  548. // Insert data into user_hobby table
  549. chk1 = (CheckBox) findViewById(R.id.cbDance);
  550. chk2 = (CheckBox) findViewById(R.id.cbSing);
  551. chk3 = (CheckBox) findViewById(R.id.cbCycle);
  552. chk4 = (CheckBox) findViewById(R.id.cbSwim);
  553.  
  554. if (chk1.isChecked()) {
  555. idUsersHobbies = db.insertUers_Hobbies(uid, 1);
  556. }
  557. if (chk2.isChecked()) {
  558. idUsersHobbies = db.insertUers_Hobbies(uid, 2);
  559. }
  560. if (chk3.isChecked()) {
  561. idUsersHobbies = db.insertUers_Hobbies(uid, 3);
  562. }
  563. if (chk4.isChecked()) {
  564. idUsersHobbies = db.insertUers_Hobbies(uid, 4);
  565. }
  566.  
  567. alertboxNeutral("Success", "Saved.", "Okay");
  568. }
  569. });
  570.  
  571. db.close();
  572. }
  573.  
  574. public void alertboxNeutral(String title, String message, String positive) {
  575. AlertDialog.Builder alertbox = new AlertDialog.Builder(
  576. HomeActivity.this);
  577. alertbox.setTitle(title);
  578. alertbox.setMessage(message);
  579. alertbox.setPositiveButton(positive,
  580. new DialogInterface.OnClickListener() {
  581. public void onClick(DialogInterface dialog, int id) {
  582. Intent i = new Intent(HomeActivity.this,
  583. HomeActivity.class);
  584. startActivity(i);
  585. }
  586. });
  587. alertbox.show();
  588. }
  589.  
  590. public void addCheckBox(String task) {
  591. LinearLayout layout = (LinearLayout) findViewById(R.id.Llayout);
  592. final CheckBox chk = new CheckBox(HomeActivity.this); // Creating
  593. // checkbox
  594. // objects.....
  595. chk.setText(task);
  596. layout.addView(chk);
  597. chk.setOnClickListener(new View.OnClickListener() {
  598.  
  599. public void onClick(View v) {
  600. chk.setVisibility(5);
  601. }
  602. });
  603. }
  604.  
  605. @Override
  606. public boolean onCreateOptionsMenu(Menu menu) {
  607. MenuInflater inflater = getMenuInflater();
  608. inflater.inflate(R.menu.main, menu);
  609. return true;
  610. }
  611.  
  612. @Override
  613. public boolean onOptionsItemSelected(MenuItem item) {
  614. switch (item.getItemId()) {
  615.  
  616. case R.id.list_users:
  617. Intent i = new Intent(this, ListUsersActivity.class);
  618. startActivity(i);
  619. break;
  620. case R.id.add_city:
  621. Intent j = new Intent(this, AddCityActivity.class);
  622. startActivity(j);
  623. break;
  624. case R.id.exit:
  625. Intent k = new Intent(this, HomeActivity.class);
  626. startActivity(k);
  627. break;
  628. }
  629.  
  630. return true;
  631. }
  632.  
  633. // Date Picker Methods
  634. // the call back received when the user "sets" the date in the dialog
  635. private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
  636.  
  637. public void onDateSet(DatePicker view, int year, int monthOfYear,
  638. int dayOfMonth) {
  639. mYear = year;
  640. mMonth = monthOfYear;
  641. mDay = dayOfMonth;
  642. updateDisplay();
  643. }
  644. };
  645.  
  646. private void updateDisplay() {
  647. mDateDisplay.setText(new StringBuilder()
  648. // Month is 0 based so add 1
  649. .append(mMonth + 1).append("-").append(mDay).append("-")
  650. .append(mYear).append(" "));
  651.  
  652. }
  653.  
  654. protected Dialog onCreateDialog(int id) {
  655. switch (id) {
  656. case DATE_DIALOG_ID:
  657. return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
  658. mDay);
  659. }
  660. return null;
  661. }
  662. }
  663.  
  664.  
  665. AddCityActivity
  666. ----------------
  667. package com.v3.userform;
  668.  
  669.  
  670.  
  671. import android.app.Activity;
  672.  
  673. import android.app.AlertDialog;
  674.  
  675. import android.content.DialogInterface;
  676.  
  677. import android.content.Intent;
  678.  
  679. import android.os.Bundle;
  680.  
  681. import android.view.View;
  682.  
  683. import android.widget.Button;
  684.  
  685. import android.widget.EditText;
  686.  
  687.  
  688.  
  689. public class AddCityActivity extends Activity {
  690.  
  691.  
  692.  
  693. protected void onCreate(Bundle savedInstanceState) {
  694.  
  695. super.onCreate(savedInstanceState);
  696.  
  697. setContentView(R.layout.add_city);
  698.  
  699.  
  700.  
  701. Button save = (Button) findViewById(R.id.btnSave);
  702.  
  703. save.setOnClickListener(new View.OnClickListener() {
  704.  
  705. public void onClick(View arg0) {
  706.  
  707. EditText city_name = (EditText) findViewById(R.id.cityname);
  708.  
  709. String cityName = city_name.getText().toString();
  710.  
  711.  
  712.  
  713. if (cityName.equals("")) {
  714.  
  715. alertbox("Error", "Please Enter the City Name", "Back",
  716.  
  717. null);
  718.  
  719.  
  720.  
  721. } else {
  722.  
  723. // Database Connection
  724.  
  725. DBAdapter db = new DBAdapter(AddCityActivity.this);
  726.  
  727. db.open();
  728.  
  729.  
  730.  
  731. if (db.checkCityName(cityName).equals(true)) {
  732.  
  733. alertboxNeutral("Error", "City Already exits.", "Okay",
  734.  
  735. "Add Another");
  736.  
  737. } else {
  738.  
  739. long cityID = db.insertCity(cityName);
  740.  
  741.  
  742.  
  743. if (cityID != -1) {
  744.  
  745. alertboxNeutral("Success", "City was added.",
  746.  
  747. "Okay", "Add Another");
  748.  
  749. } else {
  750.  
  751. alertbox("Error", "Couldnt Insert. please try",
  752.  
  753. "Back", null);
  754.  
  755. }
  756.  
  757. }
  758.  
  759.  
  760.  
  761. db.close();
  762.  
  763. }
  764.  
  765. }
  766.  
  767. });
  768.  
  769.  
  770.  
  771. }
  772.  
  773.  
  774.  
  775. public void alertbox(String title, String message, String positive,
  776.  
  777. String negative) {
  778.  
  779. AlertDialog.Builder alertbox = new AlertDialog.Builder(
  780.  
  781. AddCityActivity.this);
  782.  
  783. alertbox.setTitle(title);
  784.  
  785. alertbox.setMessage(message);
  786.  
  787. alertbox.setPositiveButton(positive, null);
  788.  
  789. alertbox.show();
  790.  
  791. }
  792.  
  793.  
  794.  
  795. public void alertboxNeutral(String title, String message, String positive,
  796.  
  797. String neutral) {
  798.  
  799. AlertDialog.Builder alertbox = new AlertDialog.Builder(
  800.  
  801. AddCityActivity.this);
  802.  
  803. alertbox.setTitle(title);
  804.  
  805. alertbox.setMessage(message);
  806.  
  807. alertbox.setPositiveButton(positive,
  808.  
  809. new DialogInterface.OnClickListener() {
  810.  
  811. public void onClick(DialogInterface dialog, int id) {
  812.  
  813. Intent i = new Intent(AddCityActivity.this,
  814.  
  815. HomeActivity.class);
  816.  
  817. startActivity(i);
  818.  
  819. }
  820.  
  821. });
  822.  
  823. alertbox.setNeutralButton(neutral,
  824.  
  825. new DialogInterface.OnClickListener() {
  826.  
  827. public void onClick(DialogInterface dialog, int id) {
  828.  
  829. EditText ed = (EditText) findViewById(R.id.cityname);
  830.  
  831. ed.setText("");
  832.  
  833. }
  834.  
  835. });
  836.  
  837.  
  838.  
  839. alertbox.show();
  840.  
  841. }
  842.  
  843. }
  844.  
  845.  
  846. ListUsersActivity
  847. -----------------
  848. package com.v3.userform;
  849.  
  850.  
  851.  
  852. import android.app.ListActivity;
  853.  
  854. import android.content.Intent;
  855.  
  856. import android.database.Cursor;
  857.  
  858. import android.os.Bundle;
  859.  
  860. import android.widget.ListAdapter;
  861.  
  862. import android.widget.ListView;
  863.  
  864. import android.widget.SimpleCursorAdapter;
  865.  
  866. import android.view.View;
  867.  
  868.  
  869.  
  870. public class ListUsersActivity extends ListActivity {
  871.  
  872. private static final String TAG = null;
  873.  
  874. Cursor c;
  875.  
  876. DBAdapter db;
  877.  
  878. static int uid;
  879.  
  880.  
  881.  
  882. protected void onCreate(Bundle savedInstanceState) {
  883.  
  884.  
  885.  
  886. super.onCreate(savedInstanceState);
  887.  
  888. setContentView(R.layout.userlist);
  889.  
  890.  
  891.  
  892. db = new DBAdapter(ListUsersActivity.this);
  893.  
  894. db.open();
  895.  
  896. c = db.listUser();
  897.  
  898. startManagingCursor(c);
  899.  
  900. String[] users = new String[] { DBAdapter.KEY_FNAME };
  901.  
  902. // String[] uid = new String[] { DBAdapter.ID };
  903.  
  904. for (String i : users) {
  905.  
  906. System.out.println("uid=" + i);
  907.  
  908. }
  909.  
  910. int[] t = new int[] { android.R.id.text1 };
  911.  
  912. ListAdapter adapter = new SimpleCursorAdapter(ListUsersActivity.this,
  913.  
  914. android.R.layout.simple_list_item_1, c, users, t);
  915.  
  916. System.out.println("listUser End");
  917.  
  918.  
  919.  
  920. // Bind to our new adapter.
  921.  
  922. setListAdapter(adapter);
  923.  
  924. }
  925.  
  926.  
  927.  
  928. @Override
  929.  
  930. protected void onListItemClick(ListView l, View v, int position, long id) {
  931.  
  932. super.onListItemClick(l, v, position, id);
  933.  
  934. // Get the item that was clicked
  935.  
  936. String fName = null;
  937.  
  938. Cursor cc = (Cursor) (this.getListAdapter().getItem(position));
  939.  
  940. if (cc != null) {
  941.  
  942. uid = Integer.parseInt(cc.getString(cc.getColumnIndex(db.ID)));
  943.  
  944. fName = cc.getString(cc.getColumnIndex(db.KEY_FNAME));
  945.  
  946. }
  947.  
  948. System.out.println("uid=:" + uid);
  949.  
  950.  
  951.  
  952. Intent intent = new Intent(this, DetailUserActivity.class);
  953.  
  954. startActivity(intent);
  955.  
  956. }
  957.  
  958.  
  959.  
  960. }
  961.  
  962.  
  963. DetailUserActivity
  964. -------------------
  965. package com.v3.userform;
  966.  
  967. import android.os.Bundle;
  968. import android.widget.TextView;
  969. import android.app.Activity;
  970. import android.database.Cursor;
  971.  
  972. public class DetailUserActivity extends Activity {
  973. TextView tv1, tv2, tv3;
  974. Cursor cr1, cr2, cr3;
  975. int cityID;
  976. String hobby = "";
  977.  
  978. public void onCreate(Bundle savedInstanceState) {
  979. super.onCreate(savedInstanceState);
  980. setContentView(R.layout.detailuser);
  981.  
  982. tv1 = (TextView) findViewById(R.id.txtVwUser);
  983. tv2 = (TextView) findViewById(R.id.txtVwCity);
  984. tv3 = (TextView) findViewById(R.id.txtVwHobby);
  985. tv1.setTextSize(20);
  986. tv2.setTextSize(20);
  987. tv3.setTextSize(20);
  988. int uid = ListUsersActivity.uid;
  989. DBAdapter db = new DBAdapter(DetailUserActivity.this);
  990. db.open();
  991. cr1 = db.getUsersData(uid);
  992.  
  993. if (cr1.moveToFirst()) {
  994. do {
  995. tv1.setText("User ID :" + cr1.getString(0) + "\n"
  996. + "First Name :" + cr1.getString(1) + "\n"
  997. + "Last Name :" + cr1.getString(2) + "\n" + "Sex :"
  998. + cr1.getString(3) + "\n" + "DOB :" + cr1.getString(4));
  999. cityID = Integer.parseInt(cr1.getString(5));
  1000.  
  1001. } while (cr1.moveToNext());
  1002. }
  1003.  
  1004. cr2 = db.find_city_name(cityID);
  1005. if (cr2.moveToFirst()) {
  1006. do {
  1007. tv2.setText("City :" + cr2.getString(1));
  1008. } while (cr2.moveToNext());
  1009. }
  1010. cr3 = db.getUserHobbies(uid);
  1011. if (cr3.moveToFirst()) {
  1012. do {
  1013.  
  1014. if (Integer.parseInt(cr3.getString(2)) == 1) {
  1015. hobby = "Dancing ";
  1016. }
  1017. if (Integer.parseInt(cr3.getString(2)) == 2) {
  1018. hobby = hobby + "Singing ";
  1019. }
  1020. if (Integer.parseInt(cr3.getString(2)) == 3) {
  1021. hobby = hobby + "Cycling ";
  1022. }
  1023. if (Integer.parseInt(cr3.getString(2)) == 4) {
  1024. hobby = hobby + "Swiming ";
  1025. }
  1026.  
  1027. } while (cr3.moveToNext());
  1028. }
  1029. tv3.setText("Hobbies :" + hobby);
  1030. }
  1031. }
  1032.  
  1033. Under layout--->
  1034. -----------------
  1035. main.xml
  1036. ----------
  1037. <?xml version="1.0" encoding="utf-8"?>
  1038. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  1039. android:layout_width="fill_parent" android:layout_height="fill_parent">
  1040.  
  1041. <LinearLayout android:orientation="vertical"
  1042. android:layout_height="wrap_content" android:layout_width="wrap_content">
  1043.  
  1044. <TextView android:text="@string/UserFormWelcome"
  1045. android:layout_width="fill_parent" android:layout_height="wrap_content"
  1046. android:textSize="20px" android:gravity="center" />
  1047.  
  1048. <TextView android:text="@string/name" android:layout_width="fill_parent"
  1049. android:layout_height="wrap_content" android:textStyle="bold"
  1050. android:paddingTop="20px" android:paddingLeft="10px" />
  1051.  
  1052.  
  1053. <TableLayout android:layout_height="wrap_content"
  1054. android:layout_width="wrap_content">
  1055.  
  1056. <TableRow android:layout_height="wrap_content"
  1057. android:layout_width="match_parent" android:paddingTop="20px">
  1058.  
  1059. <TextView android:text="@string/firstname"
  1060. android:layout_width="fill_parent" android:layout_height="wrap_content"
  1061. android:width="100px" android:paddingLeft="10px" />
  1062.  
  1063. <EditText android:id="@+id/FirstName" android:width="200px"
  1064. android:layout_width="fill_parent" android:layout_height="wrap_content" />
  1065.  
  1066. </TableRow>
  1067.  
  1068. <TableRow android:layout_height="wrap_content"
  1069. android:layout_width="match_parent">
  1070.  
  1071. <TextView android:text="@string/lastname"
  1072. android:layout_width="fill_parent" android:layout_height="wrap_content"
  1073. android:paddingLeft="10px" />
  1074.  
  1075. <EditText android:id="@+id/LastName" android:width="200px"
  1076. android:layout_width="fill_parent" android:layout_height="wrap_content" />
  1077.  
  1078. </TableRow>
  1079.  
  1080. </TableLayout>
  1081.  
  1082.  
  1083. <TextView android:text="@string/dob" android:layout_width="wrap_content"
  1084. android:layout_height="wrap_content" android:textStyle="bold"
  1085. android:paddingTop="20px" android:paddingLeft="10px" />
  1086.  
  1087. <LinearLayout android:layout_width="wrap_content"
  1088. android:layout_height="wrap_content" android:orientation="vertical"
  1089. android:paddingTop="20px" android:paddingLeft="10px">
  1090. <TextView android:id="@+id/dateDisplay"
  1091. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1092. android:text="ex: 8-4-1986" />
  1093.  
  1094. <Button android:id="@+id/pickDate" android:layout_width="wrap_content"
  1095. android:layout_height="wrap_content" android:text="Change the date"
  1096. android:layout_marginTop="10px" />
  1097. </LinearLayout>
  1098.  
  1099.  
  1100.  
  1101.  
  1102. <LinearLayout android:orientation="vertical"
  1103. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1104. android:paddingLeft="10px">
  1105.  
  1106. <TextView android:text="@string/sex" android:layout_width="wrap_content"
  1107. android:layout_height="wrap_content" android:textStyle="bold"
  1108. android:paddingTop="20px" />
  1109.  
  1110. <RadioGroup android:id="@+id/radioGroup1"
  1111. android:orientation="horizontal" android:layout_width="wrap_content"
  1112. android:layout_height="wrap_content">
  1113. <RadioButton android:text="Male" android:id="@+id/rbMale"
  1114. android:layout_height="wrap_content" android:layout_width="wrap_content"
  1115. android:paddingRight="20px" android:checked="true" />
  1116. <RadioButton android:text="Female" android:id="@+id/rbFemale"
  1117. android:layout_height="wrap_content" android:layout_width="wrap_content" />
  1118. </RadioGroup>
  1119.  
  1120. </LinearLayout>
  1121.  
  1122.  
  1123. <LinearLayout android:orientation="vertical"
  1124. android:layout_width="fill_parent" android:layout_height="wrap_content"
  1125. android:paddingLeft="10px">
  1126.  
  1127. <TextView android:text="@string/city" android:id="@+id/textView3"
  1128. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1129. android:textStyle="bold" android:paddingTop="20px"
  1130. android:paddingBottom="10px" />
  1131.  
  1132. <Spinner android:id="@+id/citySpiner" android:layout_width="wrap_content"
  1133. android:layout_height="wrap_content" android:prompt="@string/city">
  1134. </Spinner>
  1135. </LinearLayout>
  1136.  
  1137. <LinearLayout android:orientation="vertical" android:id="@+id/Llayout"
  1138. android:layout_width="fill_parent" android:layout_height="wrap_content"
  1139. android:paddingLeft="10px">
  1140.  
  1141. <TextView android:text="@string/hobby" android:id="@+id/textView3"
  1142. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1143. android:textStyle="bold" android:paddingTop="20px"
  1144. android:paddingBottom="10px" />
  1145.  
  1146. <TableLayout android:layout_height="wrap_content"
  1147. android:layout_width="wrap_content">
  1148.  
  1149. <TableRow android:layout_height="wrap_content"
  1150. android:layout_width="match_parent">
  1151.  
  1152. <CheckBox android:text="@string/dance" android:id="@+id/cbDance"
  1153. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1154. android:width="125px" />
  1155. <CheckBox android:text="@string/sing" android:id="@+id/cbSing"
  1156. android:layout_width="wrap_content" android:layout_height="wrap_content" />
  1157.  
  1158. </TableRow>
  1159.  
  1160. <TableRow android:layout_height="wrap_content"
  1161. android:layout_width="match_parent">
  1162.  
  1163. <CheckBox android:text="@string/cycle" android:id="@+id/cbCycle"
  1164. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1165. android:width="125px" />
  1166. <CheckBox android:text="@string/swim" android:id="@+id/cbSwim"
  1167. android:layout_width="wrap_content" android:layout_height="wrap_content" />
  1168.  
  1169. </TableRow>
  1170.  
  1171. </TableLayout>
  1172. </LinearLayout>
  1173.  
  1174. <Button android:text="@string/save" android:id="@+id/btnSave"
  1175. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1176. android:width="80px" android:layout_marginTop="20px"
  1177. android:layout_marginBottom="20px" />
  1178.  
  1179.  
  1180. </LinearLayout>
  1181. </ScrollView>
  1182.  
  1183.  
  1184. add_city.xml
  1185. -------------
  1186. <?xml version="1.0" encoding="utf-8"?>
  1187. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1188. android:orientation="vertical" android:layout_height="wrap_content"
  1189. android:layout_width="fill_parent">
  1190.  
  1191. <TextView android:text="@string/addcity" android:layout_width="fill_parent"
  1192. android:layout_height="wrap_content" android:width="100px"
  1193. android:paddingTop="10px" android:textSize="20px"/>
  1194.  
  1195. <EditText android:id="@+id/cityname" android:layout_width="fill_parent"
  1196. android:layout_height="wrap_content" android:paddingLeft="10px"
  1197. android:paddingRight="10px" />
  1198.  
  1199. <Button android:text="@string/save" android:id="@+id/btnSave"
  1200. android:layout_width="wrap_content" android:layout_height="wrap_content"
  1201. android:width="80px" android:layout_marginTop="10px"/>
  1202.  
  1203. </LinearLayout>
  1204.  
  1205. userlist.xml
  1206. -------------
  1207. <?xml version="1.0" encoding="utf-8"?>
  1208. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1209. android:orientation="vertical" android:layout_height="wrap_content"
  1210. android:layout_width="fill_parent">
  1211.  
  1212. <ListView android:id="@+id/android:list" android:layout_width="match_parent"
  1213. android:layout_height="match_parent" android:background="#00FF00"
  1214. android:layout_weight="1" android:drawSelectorOnTop="false" />
  1215.  
  1216.  
  1217. </LinearLayout>
  1218.  
  1219. detailuser.xml
  1220. --------------
  1221. <?xml version="1.0" encoding="utf-8"?>
  1222. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1223. android:orientation="vertical" android:layout_height="wrap_content"
  1224. android:layout_width="fill_parent" android:background="#00FF00">
  1225. <TextView android:text="TextView" android:id="@+id/txtVwUser"
  1226. android:layout_width="wrap_content" android:layout_height="wrap_content" />
  1227. <TextView android:text="" android:id="@+id/txtVwCity"
  1228. android:layout_width="wrap_content" android:layout_height="wrap_content" />
  1229. <TextView android:text="" android:id="@+id/txtVwHobby"
  1230. android:layout_width="wrap_content" android:layout_height="wrap_content" />
  1231. </LinearLayout>
  1232.  
  1233.  
  1234. //create a folder named as menu in res and there write the following xml file
  1235. main.xml
  1236. ---------
  1237. <?xml version="1.0" encoding="utf-8"?>
  1238. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  1239. <item android:id="@+id/list_users" android:title="List Users" />
  1240. <item android:id="@+id/add_city" android:title="Add City" />
  1241. <item android:id="@+id/exit" android:title="Exit" />
  1242. </menu>
  1243.  
  1244.  
  1245. //under values folder modify strings.xml
  1246. <?xml version="1.0" encoding="utf-8"?>
  1247. <resources>
  1248. <string name="hello">Hello World, HomeActivity!</string>
  1249. <string name="app_name">UserForm</string>
  1250. <string name="UserFormWelcome">User Registration Form</string>
  1251. <string name="firstname">First Name</string>
  1252. <string name="lastname">Last Name</string>
  1253. <string name="dob">Date of Birth:</string>
  1254. <string name="name">Enter your Name:</string>
  1255. <string name="month">Month:</string>
  1256. <string name="date">Date:</string>
  1257. <string name="year">Year:</string>
  1258. <string name="sex">Sex:</string>
  1259. <string name="city">Choose your City:</string>
  1260.  
  1261. <string name="hobby">Choose your Hobbies:</string>
  1262. <string name="dance">Dancing</string>
  1263. <string name="swim">Swimming</string>
  1264. <string name="sing">Singing</string>
  1265. <string name="cycle">Cycling</string>
  1266. <string name="save">Save</string>
  1267. <string name="addcity">Add a City:</string>
  1268. </resources>
  1269.  
  1270.  
  1271.  
  1272. AndroidManifest.xml
  1273. -------------------
  1274. <?xml version="1.0" encoding="utf-8"?>
  1275. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  1276. package="com.v3.userform" android:versionCode="1" android:versionName="1.0">
  1277. <application android:icon="@drawable/icon" android:label="@string/app_name">
  1278. <activity android:name=".HomeActivity" android:label="@string/app_name">
  1279. <intent-filter>
  1280. <action android:name="android.intent.action.MAIN" />
  1281. <category android:name="android.intent.category.LAUNCHER" />
  1282. </intent-filter>
  1283. </activity>
  1284. <activity android:name=".AddCityActivity" android:label="@string/app_name">
  1285. </activity>
  1286. <activity android:name=".ListUsersActivity" android:label="@string/app_name">
  1287. </activity>
  1288.  
  1289. <activity android:name=".DetailUserActivity" android:label="@string/app_name">
  1290. </activity>
  1291. </application>
  1292. <uses-sdk android:minSdkVersion="8" />
  1293.  
  1294. </manifest>

Report this snippet  

You need to login to post a comment.