Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch data in Spinner from api code

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.24k
    Comment on it

    In the blelow code example I have clearly describe "how to fetch data from api server side"? for this first I have created activity_signup.xml layout here I have added TextView, ImageView, ScrollView, EditText, Spinner and Button. Now See Coding part here i have used ArrayList for SelectCountry, SelectProvince, SelectCity, SelectSuberb these all spinners objects, go through the below example to know how to fetch data in Spinner from api code.

     

    Step(1)signup_main.xml layout-
     

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:layout_width="match_parent"
    5. android:layout_height="wrap_content"
    6. android:orientation="vertical"
    7. android:background="@mipmap/background">
    8.  
    9. <TextView
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:text="@string/signup"
    13. android:gravity="center"
    14. android:textSize="@dimen/text_large"
    15. android:textColor="@color/font_color"
    16. android:layout_marginTop="@dimen/margin_large"/>
    17. <ImageView
    18. android:layout_width="match_parent"
    19. android:layout_height="0.5dp"
    20. android:src="@color/font_color"
    21. android:layout_marginTop="@dimen/margin_medium"
    22. />
    23.  
    24. <ScrollView
    25. android:layout_width="match_parent"
    26. android:layout_height="wrap_content">
    27.  
    28. <LinearLayout
    29. android:orientation="vertical"
    30. android:layout_width="match_parent"
    31. android:layout_height="match_parent"
    32.  
    33. android:paddingLeft="@dimen/margin_medium"
    34. android:paddingRight="@dimen/margin_medium"
    35. >
    36.  
    37.  
    38. <TextView
    39. android:layout_width="match_parent"
    40. android:layout_height="wrap_content"
    41. android:text="@string/select_user_type"
    42. android:textSize="@dimen/text_large"
    43. android:textColor="@color/font_color"
    44. android:layout_marginTop="@dimen/margin_large"/>
    45.  
    46. <!--Select user type layout-->
    47. <LinearLayout
    48. android:layout_width="match_parent"
    49. android:layout_height="wrap_content"
    50. android:orientation="horizontal"
    51. android:layout_marginTop="@dimen/margin_medium"
    52. android:layout_marginLeft="@dimen/margin_xlarge"
    53. android:layout_marginRight="@dimen/text_xlarge"
    54. android:weightSum="2">
    55. <LinearLayout
    56. android:layout_weight="1"
    57. android:layout_width="wrap_content"
    58. android:layout_height="wrap_content">
    59. <ImageView
    60. android:layout_width="wrap_content"
    61. android:layout_height="wrap_content"
    62. android:id="@+id/radioButtonUser"
    63. android:src="@mipmap/rediobtn_select"/>
    64. <TextView
    65. android:layout_width="match_parent"
    66. android:layout_height="wrap_content"
    67. android:text="@string/user"
    68. android:layout_marginLeft="@dimen/margin_medium"
    69. android:textSize="@dimen/text_large"
    70. android:textColor="@color/font_color"
    71. />
    72. </LinearLayout>
    73. <LinearLayout
    74. android:layout_weight="1"
    75. android:layout_width="wrap_content"
    76. android:layout_height="wrap_content">
    77. <ImageView
    78. android:layout_width="wrap_content"
    79. android:layout_height="wrap_content"
    80. android:id="@+id/radioButtonBusinessUser"
    81. android:src="@mipmap/rediobtn"/>
    82. <TextView
    83. android:layout_width="match_parent"
    84. android:layout_height="wrap_content"
    85. android:text="@string/business_user"
    86. android:layout_marginLeft="@dimen/margin_medium"
    87. android:textSize="@dimen/text_large"
    88. android:textColor="@color/font_color"
    89. />
    90. </LinearLayout>
    91.  
    92. </LinearLayout>
    93. <!--End of select user type layout-->
    94.  
    95.  
    96. <!--Field layout -->
    97. <LinearLayout
    98. android:layout_width="match_parent"
    99. android:layout_height="wrap_content"
    100. android:orientation="vertical"
    101. android:layout_marginTop="@dimen/margin_large"
    102. android:layout_marginLeft="@dimen/margin_medium"
    103. android:layout_marginRight="@dimen/text_medium"
    104. android:background="@drawable/login_bg">
    105.  
    106. <!--Email address layout-->
    107. <LinearLayout
    108. android:layout_width="match_parent"
    109. android:layout_height="wrap_content"
    110. android:orientation="horizontal"
    111. android:gravity="center_vertical"
    112. android:layout_margin="@dimen/margin_large">
    113.  
    114. <ImageView
    115. android:layout_width="wrap_content"
    116. android:layout_height="wrap_content"
    117. android:src="@mipmap/icon_mail"/>
    118. <EditText
    119. android:id="@+id/emailAddressEditText"
    120. android:layout_width="match_parent"
    121. android:layout_height="wrap_content"
    122. android:hint="@string/email_address"
    123. android:inputType="textEmailAddress"
    124. android:textColorHint="@color/font_color"
    125. android:background="#00000000"
    126. android:textCursorDrawable="@drawable/custom_cursor_color"
    127. android:layout_marginLeft="@dimen/margin_medium"
    128. android:textColor="@color/font_color"
    129. />
    130. </LinearLayout>
    131.  
    132. <!--divider line -->
    133. <LinearLayout
    134. android:layout_width="match_parent"
    135. android:layout_height="0.5dp"
    136. android:background="@color/font_color">
    137.  
    138. </LinearLayout>
    139.  
    140.  
    141. <!--User name layout-->
    142. <LinearLayout
    143. android:layout_width="match_parent"
    144. android:layout_height="wrap_content"
    145. android:orientation="horizontal"
    146. android:gravity="center_vertical"
    147. android:layout_margin="@dimen/margin_large">
    148.  
    149. <ImageView
    150. android:layout_width="wrap_content"
    151. android:layout_height="wrap_content"
    152. android:src="@mipmap/icon_user"/>
    153. <EditText
    154. android:id="@+id/userNameEditText"
    155. android:layout_width="match_parent"
    156. android:layout_height="wrap_content"
    157. android:hint="@string/username"
    158. android:inputType="text"
    159. android:textColorHint="@color/font_color"
    160. android:background="#00000000"
    161. android:textCursorDrawable="@drawable/custom_cursor_color"
    162. android:layout_marginLeft="@dimen/margin_medium"
    163. android:textColor="@color/font_color"
    164. />
    165. </LinearLayout>
    166.  
    167. <!--divider line -->
    168. <LinearLayout
    169. android:layout_width="match_parent"
    170. android:layout_height="0.5dp"
    171. android:background="@color/font_color">
    172.  
    173. </LinearLayout>
    174.  
    175. <!--Enter name and surname layout-->
    176. <LinearLayout
    177. android:layout_width="match_parent"
    178. android:layout_height="wrap_content"
    179. android:orientation="horizontal"
    180. android:layout_margin="@dimen/margin_large">
    181.  
    182. <ImageView
    183. android:layout_width="wrap_content"
    184. android:layout_height="wrap_content"
    185. android:src="@mipmap/icon_user"/>
    186. <EditText
    187. android:id="@+id/userSurNameEditText"
    188. android:layout_width="match_parent"
    189. android:layout_height="wrap_content"
    190. android:hint="@string/enter_name_sirname"
    191. android:inputType="text"
    192. android:textColorHint="@color/font_color"
    193. android:background="#00000000"
    194. android:textCursorDrawable="@drawable/custom_cursor_color"
    195. android:layout_marginLeft="@dimen/margin_medium"
    196. android:textColor="@color/font_color"
    197. />
    198. </LinearLayout>
    199.  
    200. <!--divider line -->
    201. <LinearLayout
    202. android:layout_width="match_parent"
    203. android:layout_height="0.5dp"
    204. android:background="@color/font_color">
    205.  
    206. </LinearLayout>
    207.  
    208.  
    209. <!--Cell phone layout-->
    210. <LinearLayout
    211. android:layout_width="match_parent"
    212. android:layout_height="wrap_content"
    213. android:orientation="horizontal"
    214. android:gravity="center_vertical"
    215. android:layout_margin="@dimen/margin_large">
    216.  
    217. <ImageView
    218. android:layout_width="wrap_content"
    219. android:layout_height="wrap_content"
    220. android:src="@mipmap/icon_cell"/>
    221. <EditText
    222. android:id="@+id/userCellPhoneEditText"
    223. android:layout_width="match_parent"
    224. android:layout_height="wrap_content"
    225. android:hint="@string/cell_phone_nuber"
    226. android:inputType="phone"
    227. android:textColorHint="@color/font_color"
    228. android:background="#00000000"
    229. android:textCursorDrawable="@drawable/custom_cursor_color"
    230. android:layout_marginLeft="@dimen/margin_medium"
    231. android:textColor="@color/font_color"
    232. />
    233. </LinearLayout>
    234.  
    235. <!--divider line -->
    236. <LinearLayout
    237. android:layout_width="match_parent"
    238. android:layout_height="0.5dp"
    239. android:background="@color/font_color">
    240.  
    241. </LinearLayout>
    242.  
    243.  
    244.  
    245. <!--Select country layout-->
    246. <RelativeLayout
    247. android:layout_width="match_parent"
    248. android:layout_height="50dp"
    249. android:orientation="horizontal"
    250. android:gravity="center_vertical"
    251. android:layout_marginLeft="@dimen/margin_large"
    252. android:layout_marginRight="@dimen/margin_large"
    253. android:layout_marginTop="@dimen/margin_small"
    254. android:layout_marginBottom="@dimen/margin_small">
    255.  
    256. <ImageView
    257. android:id="@+id/countryImageView"
    258. android:layout_width="wrap_content"
    259. android:layout_height="wrap_content"
    260. android:layout_centerVertical="true"
    261. android:src="@mipmap/icon_country"/>
    262.  
    263. <Spinner
    264. android:layout_toRightOf="@+id/countryImageView"
    265. android:id="@+id/country_spinner"
    266. android:layout_width="match_parent"
    267. android:layout_height="wrap_content"
    268. android:inputType="text"
    269. android:background="@android:color/transparent"
    270. android:layout_centerVertical="true"/>
    271.  
    272.  
    273. <ImageView
    274. android:layout_centerVertical="true"
    275. android:layout_alignParentRight="true"
    276. android:layout_width="wrap_content"
    277. android:layout_height="wrap_content"
    278. android:layout_gravity="end"
    279. android:src="@mipmap/icon_drop_down"/>
    280.  
    281. </RelativeLayout>
    282.  
    283. <!--divider line -->
    284. <LinearLayout
    285. android:layout_width="match_parent"
    286. android:layout_height="0.5dp"
    287. android:background="@color/font_color">
    288.  
    289. </LinearLayout>
    290.  
    291. <!--Select province layout-->
    292. <RelativeLayout
    293. android:layout_width="match_parent"
    294. android:layout_height="50dp"
    295. android:orientation="horizontal"
    296. android:gravity="center_vertical"
    297. android:layout_marginLeft="@dimen/margin_large"
    298. android:layout_marginRight="@dimen/margin_large"
    299. android:layout_marginTop="@dimen/margin_small"
    300. android:layout_marginBottom="@dimen/margin_small">
    301.  
    302. <ImageView
    303. android:id="@+id/provinceImageView"
    304. android:layout_width="wrap_content"
    305. android:layout_height="wrap_content"
    306. android:layout_centerVertical="true"
    307. android:src="@mipmap/icon_country"/>
    308.  
    309. <Spinner
    310. android:layout_toRightOf="@+id/provinceImageView"
    311. android:id="@+id/province_spinner"
    312. android:layout_width="match_parent"
    313. android:layout_height="wrap_content"
    314.  
    315. android:inputType="text"
    316. android:background="@android:color/transparent"
    317. android:layout_centerVertical="true"/>
    318.  
    319.  
    320. <ImageView
    321. android:layout_centerVertical="true"
    322. android:layout_alignParentRight="true"
    323. android:layout_width="wrap_content"
    324. android:layout_height="wrap_content"
    325. android:layout_gravity="end"
    326. android:src="@mipmap/icon_drop_down"/>
    327.  
    328. </RelativeLayout>
    329.  
    330.  
    331. !--divider line -->
    332. <LinearLayout
    333. android:layout_width="match_parent"
    334. android:layout_height="0.5dp"
    335. android:background="@color/font_color">
    336.  
    337. </LinearLayout>
    338.  
    339.  
    340. <!--Select city layout-->
    341. <RelativeLayout
    342. android:layout_width="match_parent"
    343. android:layout_height="50dp"
    344. android:orientation="horizontal"
    345. android:gravity="center_vertical"
    346. android:layout_marginLeft="@dimen/margin_large"
    347. android:layout_marginRight="@dimen/margin_large"
    348. android:layout_marginTop="@dimen/margin_small"
    349. android:layout_marginBottom="@dimen/margin_small">
    350.  
    351. <ImageView
    352. android:id="@+id/cityImageView"
    353. android:layout_width="wrap_content"
    354. android:layout_height="wrap_content"
    355. android:layout_centerVertical="true"
    356. android:src="@mipmap/icon_country"/>
    357.  
    358. <Spinner
    359. android:layout_toRightOf="@+id/cityImageView"
    360. android:id="@+id/city_spinner"
    361. android:layout_width="match_parent"
    362. android:layout_height="wrap_content"
    363.  
    364. android:inputType="text"
    365. android:background="@android:color/transparent"
    366. android:layout_centerVertical="true"/>
    367.  
    368.  
    369. <ImageView
    370. android:layout_centerVertical="true"
    371. android:layout_alignParentRight="true"
    372. android:layout_width="wrap_content"
    373. android:layout_height="wrap_content"
    374. android:layout_gravity="end"
    375. android:src="@mipmap/icon_drop_down"/>
    376.  
    377. </RelativeLayout>
    378.  
    379. !--divider line -->
    380. <LinearLayout
    381. android:layout_width="match_parent"
    382. android:layout_height="0.5dp"
    383. android:background="@color/font_color">
    384.  
    385. </LinearLayout>
    386.  
    387. <!--Select city layout-->
    388. <RelativeLayout
    389. android:layout_width="match_parent"
    390. android:layout_height="50dp"
    391. android:orientation="horizontal"
    392. android:gravity="center_vertical"
    393. android:layout_marginLeft="@dimen/margin_large"
    394. android:layout_marginRight="@dimen/margin_large"
    395. android:layout_marginTop="@dimen/margin_small"
    396. android:layout_marginBottom="@dimen/margin_small">
    397.  
    398. <ImageView
    399. android:id="@+id/suberbImageView"
    400. android:layout_width="wrap_content"
    401. android:layout_height="wrap_content"
    402. android:layout_centerVertical="true"
    403. android:src="@mipmap/icon_country"/>
    404.  
    405. <Spinner
    406. android:layout_toRightOf="@+id/suberbImageView"
    407. android:id="@+id/suberb_spinner"
    408. android:layout_width="match_parent"
    409. android:layout_height="wrap_content"
    410.  
    411. android:inputType="text"
    412. android:background="@android:color/transparent"
    413. android:layout_centerVertical="true"/>
    414.  
    415.  
    416. <ImageView
    417. android:layout_centerVertical="true"
    418. android:layout_alignParentRight="true"
    419. android:layout_width="wrap_content"
    420. android:layout_height="wrap_content"
    421. android:layout_gravity="end"
    422. android:src="@mipmap/icon_drop_down"/>
    423.  
    424. </RelativeLayout>
    425.  
    426. !--divider line -->
    427. <LinearLayout
    428. android:layout_width="match_parent"
    429. android:layout_height="0.5dp"
    430. android:background="@color/font_color">
    431.  
    432. </LinearLayout>
    433.  
    434.  
    435. <!-- &lt;!&ndash;Select country layout&ndash;&gt;
    436. <LinearLayout
    437. android:layout_width="match_parent"
    438. android:layout_height="wrap_content"
    439. android:orientation="horizontal"
    440. android:layout_margin="@dimen/margin_large">
    441.  
    442. <ImageView
    443. android:layout_width="wrap_content"
    444. android:layout_height="wrap_content"
    445. android:src="@mipmap/icon_country"/>
    446.  
    447. <Spinner
    448. android:id="@+id/country_spinner"
    449. android:layout_width="match_parent"
    450. android:layout_height="wrap_content"
    451. android:inputType="text" />
    452.  
    453. </LinearLayout>
    454. -->
    455. <!--divider line -->
    456. <LinearLayout
    457. android:layout_width="match_parent"
    458. android:layout_height="0.5dp"
    459. android:background="@color/font_color">
    460.  
    461. </LinearLayout>
    462.  
    463.  
    464. <!--Password layout-->
    465. <LinearLayout
    466. android:layout_width="match_parent"
    467. android:layout_height="wrap_content"
    468. android:orientation="horizontal"
    469. android:gravity="center_vertical"
    470. android:layout_margin="@dimen/margin_large">
    471.  
    472. <ImageView
    473. android:layout_width="wrap_content"
    474. android:layout_height="wrap_content"
    475. android:src="@mipmap/icon_passwrod"/>
    476. <EditText
    477. android:id="@+id/userPasswordEditText"
    478. android:layout_width="match_parent"
    479.  
    480. android:layout_height="wrap_content"
    481. android:hint="@string/enter_your_password"
    482. android:inputType="textPassword"
    483. android:textColorHint="@color/font_color"
    484. android:background="#00000000"
    485. android:textCursorDrawable="@drawable/custom_cursor_color"
    486. android:layout_marginLeft="@dimen/margin_medium"
    487. android:textColor="@color/font_color"
    488. />
    489. </LinearLayout>
    490.  
    491. <!--divider line -->
    492. <LinearLayout
    493. android:layout_width="match_parent"
    494. android:layout_height="0.5dp"
    495. android:background="@color/font_color">
    496.  
    497. </LinearLayout>
    498.  
    499.  
    500. <!--Confirm Password layout-->
    501. <LinearLayout
    502. android:layout_width="match_parent"
    503. android:layout_height="wrap_content"
    504. android:orientation="horizontal"
    505. android:gravity="center_vertical"
    506. android:layout_margin="@dimen/margin_large">
    507.  
    508. <ImageView
    509. android:layout_width="wrap_content"
    510. android:layout_height="wrap_content"
    511. android:src="@mipmap/icon_passwrod"/>
    512. <EditText
    513. android:id="@+id/userConfirmPasswordEditText"
    514. android:layout_width="match_parent"
    515. android:layout_height="wrap_content"
    516. android:hint="@string/confirm_password"
    517. android:inputType="textPassword"
    518. android:textColorHint="@color/font_color"
    519. android:background="#00000000"
    520. android:textCursorDrawable="@drawable/custom_cursor_color"
    521. android:layout_marginLeft="@dimen/margin_medium"
    522. android:textColor="@color/font_color"
    523. />
    524. </LinearLayout>
    525.  
    526.  
    527.  
    528. </LinearLayout>
    529.  
    530. <!--Login button-->
    531. <Button
    532. android:id="@+id/signUpButton"
    533. android:layout_marginLeft="@dimen/margin_large"
    534. android:layout_marginRight="@dimen/margin_large"
    535. android:layout_width="match_parent"
    536. android:layout_height="wrap_content"
    537. android:layout_marginTop="@dimen/margin_large"
    538. android:layout_marginBottom="@dimen/margin_large"
    539. android:background="@drawable/login_btn"
    540. android:text="@string/signup"
    541. android:textSize="@dimen/text_large"
    542. android:textColor="@color/font_color"/>
    543.  
    544.  
    545. </LinearLayout>
    546. </ScrollView>
    547. </LinearLayout>
    548.  
    549.  
    550.  

    Step(2)SignUpActivity-
     

    1. public class SignUpActivity extends ActionBarActivity implements View.OnClickListener,ActivityInterface ,AdapterView.OnItemSelectedListener {
    2.  
    3. private ImageView radioButtonUser,radioButtonBusinessUser;
    4. private EditText userEmailEditText,userNameEditText,userSurNameEditText,cellNuberEditText,userPasswordEditText,userConfirmPasswordEditText;
    5. private Spinner countrySpinner,provinceSpinner,citySpinner,suberbSpinner;
    6. private Button singupButton;
    7.  
    8. private String emailAddress,userName,userSurName,selectCountry,userCellPhone,userPassword,confirmPassword;
    9. private int userType=0;
    10. private int selectedPosition;
    11. public static ArrayList<SelectCountry> selectCountriesList=new ArrayList<SelectCountry>();
    12. public ArrayList<SelectProvince> selectProvinceArrayList;
    13. public ArrayList<SelectCity> selectCityArrayList;
    14. public ArrayList<SelectSuberb> selectSuberbArrayList;
    15. private DataOffersArrayList dataOffers;
    16.  
    17. private SelectCity selectCity;
    18. private SelectProvince selectProvince;
    19. private SelectSuberb selectSuberb;
    20. private String countryID=null, provinceID=null, cityID=null,superbID=null;
    21.  
    22.  
    23. private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    24. public static final String EXTRA_MESSAGE = "message";
    25. private String SENDER_ID="1025757029424";
    26. public static final String PROPERTY_REG_ID = "registration_id";
    27. private static final String PROPERTY_APP_VERSION = "appVersion";
    28. private GoogleCloudMessaging gcm;
    29. private Context context;
    30. private String regid;
    31. private Dialog dialog;
    32.  
    33.  
    34. @Override
    35. protected void onCreate(Bundle savedInstanceState) {
    36. super.onCreate(savedInstanceState);
    37. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    38.  
    39. setContentView(R.layout.activity_signup);
    40.  
    41. /* Google cloud messaging */
    42. context = getApplicationContext();
    43. if (checkPlayServices()) {
    44. gcm = GoogleCloudMessaging.getInstance(this);
    45. regid = getRegistrationId(context);
    46. MyUtility.showLog("regID.....",regid);
    47. registerInBackground();
    48. }
    49.  
    50. //Initialize UI
    51. intializeUI();
    52. // Intialize spinner firs hint values
    53. intializeSpinnerHintValues();
    54.  
    55. // fetch country name and code from server side
    56. fetchCountryItems();
    57. }
    58.  
    59. /**
    60. * This function fetching country items as name and code when first activity open
    61. *
    62. */
    63. private void fetchCountryItems() {
    64.  
    65. if (MyUtility.isConnected(this)) {
    66. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    67. nameValuePairs.add(new BasicNameValuePair(Constsants.SELECT_COUNTRY, Constsants.SELECT_COUNTRY));
    68. WebServiceTask lineDataServiceTask =
    69. new WebServiceTask(this, ServerUrl.BASE_URL+ServerUrl.SELECT_COUNTRY_API, nameValuePairs, Constsants.SELECT_COUNTRY_API_CODE,this);
    70. lineDataServiceTask.execute();
    71. }else
    72. {
    73. Toast.makeText(this,getResources().getString(R.string.internet_connectivity_msg),Toast.LENGTH_SHORT).show();
    74. }
    75. }
    76.  
    77.  
    78. // Intialize UI ID'S
    79. public void intializeUI(){
    80.  
    81.  
    82. dataOffers=DataOffersArrayList.getInstance();
    83. selectProvinceArrayList=dataOffers.getProvinceArrayList();
    84. selectCityArrayList=dataOffers.getCityArrayList();
    85. selectSuberbArrayList=dataOffers.getSuberbArrayList();
    86.  
    87.  
    88. radioButtonUser=(ImageView)findViewById(R.id.radioButtonUser);
    89. radioButtonUser.setOnClickListener(this);
    90. radioButtonBusinessUser=(ImageView)findViewById(R.id.radioButtonBusinessUser);
    91. radioButtonBusinessUser.setOnClickListener(this);
    92.  
    93. singupButton=(Button)findViewById(R.id.signUpButton);
    94. singupButton.setOnClickListener(this);
    95.  
    96. userEmailEditText=(EditText)findViewById(R.id.emailAddressEditText);
    97. userNameEditText=(EditText)findViewById(R.id.userNameEditText);
    98. userSurNameEditText=(EditText)findViewById(R.id.userSurNameEditText);
    99. cellNuberEditText=(EditText)findViewById(R.id.userCellPhoneEditText);
    100.  
    101. countrySpinner=(Spinner)findViewById(R.id.country_spinner);
    102. citySpinner=(Spinner)findViewById(R.id.city_spinner);
    103. provinceSpinner=(Spinner)findViewById(R.id.province_spinner);
    104. suberbSpinner=(Spinner)findViewById(R.id.suberb_spinner);
    105.  
    106. // Set here selected item listener to all spinners
    107. citySpinner.setOnItemSelectedListener(this);
    108. provinceSpinner.setOnItemSelectedListener(this);
    109. suberbSpinner.setOnItemSelectedListener(this);
    110. countrySpinner.setOnItemSelectedListener(this);
    111.  
    112.  
    113. userPasswordEditText=(EditText)findViewById(R.id.userPasswordEditText);
    114. userConfirmPasswordEditText=(EditText)findViewById(R.id.userConfirmPasswordEditText);
    115.  
    116.  
    117. }
    118.  
    119.  
    120. // intialize all spiner with intial values
    121. private void intializeSpinnerHintValues() {
    122.  
    123.  
    124. selectProvinceArrayList.clear();
    125. selectProvince = new SelectProvince();
    126. selectProvince.setProvinceName("Select Province");
    127. selectProvince.setProvinceID("0");
    128. selectProvinceArrayList.add(selectProvince);
    129. provinceSpinner.setAdapter(new ProvinceAdapter(selectProvinceArrayList, this));
    130.  
    131. selectCityArrayList.clear();
    132. selectCity = new SelectCity();
    133. selectCity.setCityName("Select City");
    134. selectCity.setCityID("0");
    135. selectCityArrayList.add(selectCity);
    136. citySpinner.setAdapter(new CityAdapter(selectCityArrayList, this));
    137.  
    138.  
    139. selectSuberbArrayList.clear();
    140. selectSuberb = new SelectSuberb();
    141. selectSuberb.setSuberbName("Select Suberb");
    142. selectSuberb.setSuberbID("0");
    143. selectSuberbArrayList.add(selectSuberb);
    144. suberbSpinner.setAdapter(new SuberbAdapter(selectSuberbArrayList, this));
    145.  
    146. }
    147.  
    148. @Override
    149. public void onSuccess(int apiCode) {
    150. if(apiCode==Constsants.SELECT_COUNTRY_API_CODE){
    151. // Toast.makeText(this,"fetch list successfully", Toast.LENGTH_SHORT).show();
    152. //set adapter to spinner
    153. countrySpinner.setAdapter(new BrandAdapter(selectCountriesList));
    154.  
    155.  
    156. }
    157. if(apiCode==Constsants.SIGN_UP_API_CODE){
    158. if(SharedPreferenceManager.getInstance().getUserDetails(this,Constsants.RESPONSE_USER_TYPE).equals("0")) {
    159. // Toast.makeText(this, "Registered successfully", Toast.LENGTH_SHORT).show();
    160. Intent intent = new Intent(SignUpActivity.this, SelectLocationActivity.class);
    161. intent.putExtra("activity","signup");
    162. startActivity(intent);
    163. }else {
    164. Intent intent=new Intent(SignUpActivity.this, ActivityAddBusiness.class);
    165. startActivity(intent);
    166. }
    167.  
    168. }
    169. if(provinceID.isEmpty()|| provinceSpinner==null)
    170.  
    171. if(apiCode==Constsants.SELECT_CITY_API_CODE){
    172. selectCityArrayList=DataOffersArrayList.getInstance().getCityArrayList();
    173. citySpinner.setAdapter(new CityAdapter(selectCityArrayList,this));
    174. }
    175. if(apiCode==Constsants.SELECT_PROVINCE_API_CODE){
    176. selectProvinceArrayList=DataOffersArrayList.getInstance().getProvinceArrayList();
    177. provinceSpinner.setAdapter(new ProvinceAdapter(selectProvinceArrayList,this));
    178. }
    179. if(apiCode==Constsants.SELECT_SUBERB_API_CODE){
    180. selectSuberbArrayList=DataOffersArrayList.getInstance().getSuberbArrayList();
    181. MyUtility.showLog("SUBERB ARRAY LIST SIZE ",selectSuberbArrayList.size()+"");
    182. suberbSpinner.setAdapter(new SuberbAdapter(selectSuberbArrayList, this));
    183. }
    184.  
    185. }
    186.  
    187. @Override
    188. public void onFailed(int apiCode, String response) {
    189. if(apiCode==Constsants.SIGN_UP_API_CODE) {
    190.  
    191. showDialogWithSuccessfull("Username Already exists and duplicate account is not permitted");
    192.  
    193. }
    194. if(apiCode==Constsants.SELECT_COUNTRY_API_CODE){
    195. countrySpinner.setAdapter(new BrandAdapter(selectCountriesList));
    196. }
    197. }
    198.  
    199. @Override
    200. public void onClick(View v) {
    201.  
    202. switch (v.getId()){
    203. case R.id.radioButtonUser :
    204. MyUtility.showLog("click radion button user", "ok");
    205.  
    206. if(userType==0){
    207.  
    208. }else{
    209. userType=0;
    210. radioButtonUser.setImageResource(R.mipmap.rediobtn_select);
    211. radioButtonBusinessUser.setImageResource(R.mipmap.rediobtn);
    212. }
    213.  
    214.  
    215. break;
    216. case R.id.radioButtonBusinessUser :
    217. MyUtility.showLog("click radion button busines user", "ok");
    218.  
    219. if(userType==1){
    220.  
    221. }else{
    222. MyUtility.showLog("user type is one ",userType+"");
    223. userType=1;
    224. radioButtonUser.setImageResource(R.mipmap.rediobtn);
    225. radioButtonBusinessUser.setImageResource(R.mipmap.rediobtn_select);
    226. }
    227.  
    228. break;
    229. case R.id.signUpButton :
    230. // do something
    231.  
    232. boolean isValid=validateInput();
    233. if(isValid) {
    234. // check internet connectivity availability before run to API
    235. if (MyUtility.isConnected(this)) {
    236.  
    237. registerUser();// this method run to API which register user
    238.  
    239. } else {
    240. Toast.makeText(this, getString(R.string.internet_connectivity_msg), Toast.LENGTH_SHORT).show();
    241. finish();
    242. }
    243. }
    244.  
    245. break;
    246. }
    247.  
    248. }
    249.  
    250.  
    251.  
    252. /**
    253. * to validate input text fields
    254. */
    255. boolean validateInput() {
    256. EmailValidator emailValidator = new EmailValidator();
    257.  
    258. emailAddress=userEmailEditText.getText().toString().trim();
    259. userName=userNameEditText.getText().toString().trim();
    260. userSurName=userSurNameEditText.getText().toString().trim();
    261. userCellPhone=cellNuberEditText.getText().toString().trim();
    262. MyUtility.showLog("USER CELL NUMBER ",userCellPhone);
    263. selectCountry=selectCountriesList.get(selectedPosition).getCountryCode().toString().trim();
    264. userPassword=userPasswordEditText.getText().toString().trim();
    265. confirmPassword=userConfirmPasswordEditText.getText().toString().trim();
    266.  
    267. if (!emailValidator.validate(emailAddress)) {
    268. userEmailEditText.setError("Please enter a valid email");
    269. userEmailEditText.requestFocus();
    270. return false;
    271. }
    272.  
    273. if(userName.equals("")){
    274. userNameEditText.setError("Please enter name");
    275. userNameEditText.requestFocus();
    276. return false;
    277. }
    278.  
    279. if(userSurName.equals("")){
    280. userSurNameEditText.setError("Please enter surname");
    281. userSurNameEditText.requestFocus();
    282. return false;
    283. }
    284.  
    285. if(userCellPhone.equals("")){
    286. cellNuberEditText.setError("Please enter cell phone number");
    287. cellNuberEditText.requestFocus();
    288. return false;
    289. }
    290.  
    291. if(countryID.equals("null")){
    292. Toast.makeText(this,"Select Country",Toast.LENGTH_SHORT).show();
    293. return false;
    294. //((TextView)countrySpinner.getSelectedView()).setError("Select a country ");
    295. }
    296.  
    297.  
    298.  
    299. if(provinceID.equals("0") || provinceID.equals("null")){
    300. Toast.makeText(this,"Select Province",Toast.LENGTH_SHORT).show();
    301. return false;
    302. //((TextView)provinceSpinner.getSelectedView()).setError("Select province ");
    303. }
    304.  
    305.  
    306. if(cityID.equals("0") || cityID.equals("null")){
    307. Toast.makeText(this,"Select City",Toast.LENGTH_SHORT).show();
    308. return false;
    309. //((TextView)citySpinner.getSelectedView()).setError("Select city ");
    310. }
    311.  
    312. if(superbID.equals("0") || superbID.equals("null")){
    313. Toast.makeText(this,"Select Superb",Toast.LENGTH_SHORT).show();
    314. return false;
    315. //((TextView)suberbSpinner.getSelectedView()).setError("Select superb");
    316. }
    317.  
    318. else if (userPassword.equals("")) {
    319. userPasswordEditText.setError("Please enter a valid password");
    320. userPasswordEditText.requestFocus();
    321. return false;
    322.  
    323. }
    324. else if (userPassword.length()<6) {
    325. userPasswordEditText.setError("Password length must be 6 digit");
    326. userPasswordEditText.requestFocus();
    327. return false;
    328.  
    329. }
    330.  
    331. else if (!userPassword.equals(userConfirmPasswordEditText.getText()
    332. .toString())) {
    333. userConfirmPasswordEditText.setError("Passwords do not match");
    334. userConfirmPasswordEditText.requestFocus();
    335. return false;
    336. }
    337.  
    338. return true;
    339. }
    340.  
    341. // run api
    342. private void registerUser() {
    343.  
    344.  
    345. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    346. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_REGISTER, Constsants.USER_REGISTER));
    347. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_EMAIL_ID,emailAddress));
    348. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_NAME,userName));
    349. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_FIRST_NAME,userSurName));
    350. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_COUNTRY,selectCountry));
    351. nameValuePairs.add(new BasicNameValuePair(Constsants.SIGNUP_CITYID,cityID));
    352. nameValuePairs.add(new BasicNameValuePair(Constsants.SIGNUP_PROVICEID,provinceID));
    353. nameValuePairs.add(new BasicNameValuePair(Constsants.SIGNUP_SUBERBID,superbID));
    354. nameValuePairs.add(new BasicNameValuePair(Constsants.REGISTRATION_ID,regid));
    355. nameValuePairs.add(new BasicNameValuePair(Constsants.DEVICE_TYPE,"android"));
    356.  
    357. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_PASSWORD,userPassword));
    358. MyUtility.showLog("cell number registered ........",userCellPhone);
    359. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_CELL_PHONE,userCellPhone));
    360. nameValuePairs.add(new BasicNameValuePair(Constsants.USER_TYPE,String.valueOf(userType)));
    361. WebServiceTask lineDataServiceTask =
    362. new WebServiceTask(this, ServerUrl.BASE_URL+ServerUrl.SIGN_UP_API, nameValuePairs, Constsants.SIGN_UP_API_CODE,this);
    363. lineDataServiceTask.execute();
    364.  
    365. }
    366.  
    367. // this method fetch province category's items from server based on selected id
    368. private void fetchProviceItems(String countryID) {
    369.  
    370. MyUtility.showLog("Country Id########### ",countryID);
    371.  
    372. if (MyUtility.isConnected(SignUpActivity.this)) {
    373. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    374. nameValuePairs.add(new BasicNameValuePair(Constsants.SELECT_PROVINCE_SIGNUP, Constsants.SELECT_PROVINCE_SIGNUP));
    375. nameValuePairs.add(new BasicNameValuePair(Constsants.SELECT_CITY_SIGNUP, Constsants.SELECT_CITY_SIGNUP));
    376. nameValuePairs.add(new BasicNameValuePair(Constsants.COUNTRY_ID, countryID));
    377.  
    378.  
    379. WebServiceTask lineDataServiceTask =
    380. new WebServiceTask(SignUpActivity.this, ServerUrl.BASE_URL + ServerUrl.SELECT_PROVINCE_API, nameValuePairs, Constsants.SELECT_PROVINCE_API_CODE,this);
    381. lineDataServiceTask.execute();
    382. } else {
    383. Toast.makeText(SignUpActivity.this, getResources().getString(R.string.internet_connectivity_msg), Toast.LENGTH_SHORT).show();
    384. }
    385. }
    386.  
    387. // this method fetch city category's items from server based on selected id
    388. private void fetchCityItems(String provinceID) {
    389.  
    390. MyUtility.showLog("Province Id######### ",provinceID);
    391. MyUtility.showLog("Country Id######### ",countryID);
    392.  
    393.  
    394. if (MyUtility.isConnected(SignUpActivity.this)) {
    395.  
    396.  
    397. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    398. nameValuePairs.add(new BasicNameValuePair(Constsants.SELECT_CITY_SIGNUP, Constsants.SELECT_CITY_SIGNUP));
    399. nameValuePairs.add(new BasicNameValuePair(Constsants.PROVINCE_ID, provinceID));
    400. nameValuePairs.add(new BasicNameValuePair(Constsants.COUNTRY_ID, countryID));
    401.  
    402.  
    403. WebServiceTask lineDataServiceTask =
    404. new WebServiceTask(SignUpActivity.this, ServerUrl.BASE_URL + ServerUrl.SELECT_CITY_API, nameValuePairs, Constsants.SELECT_CITY_API_CODE,this);
    405. lineDataServiceTask.execute();
    406. } else {
    407. Toast.makeText(SignUpActivity.this, getResources().getString(R.string.internet_connectivity_msg), Toast.LENGTH_SHORT).show();
    408. }
    409. }
    410.  
    411. private void fetchSuberbItems(String cityID) {
    412.  
    413. if (MyUtility.isConnected(SignUpActivity.this)) {
    414. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    415. nameValuePairs.add(new BasicNameValuePair(Constsants.SELECT_SUBERB_SIGNUP, Constsants.SELECT_SUBERB_SIGNUP));
    416. nameValuePairs.add(new BasicNameValuePair(Constsants.CITY_ID, cityID));
    417. WebServiceTask lineDataServiceTask =new WebServiceTask(SignUpActivity.this, ServerUrl.BASE_URL + ServerUrl.SELECT_SUBERB_API, nameValuePairs, Constsants.SELECT_SUBERB_API_CODE,this);
    418. lineDataServiceTask.execute();
    419. } else {
    420. Toast.makeText(SignUpActivity.this, getResources().getString(R.string.internet_connectivity_msg), Toast.LENGTH_SHORT).show();
    421. }
    422. }
    423.  
    424.  
    425. @Override
    426. public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    427. if(parent.getId() == countrySpinner.getId()) {
    428. countryID = SignUpActivity.selectCountriesList.get(position).getCountryCode();
    429. // MyUtility.showLog("selcted country id ",countryID+"");
    430. // Fetch the province items list from server by hit the api
    431. if (countryID.equals("0") || countryID.equals("null")) {
    432. // MyUtility.showLog("countryID id null ", countryID + "");
    433. } else {
    434. MyUtility.showLog("calling fetch province method here ",countryID+"");
    435. fetchProviceItems(countryID);
    436. }
    437.  
    438. }
    439. else if (parent.getId() == provinceSpinner.getId()) {
    440.  
    441.  
    442. provinceID = selectProvinceArrayList.get(position).getProvinceID();
    443. MyUtility.showLog("selcted province id ",provinceID+"");
    444. // Fetch the city items from server by the hit api
    445. MyUtility.showLog("SIZE OF PROVINCE ARRAY LIST ",selectProvinceArrayList.size()+"");
    446.  
    447. if (provinceID.equals("0") || provinceID.equals("null")) {
    448.  
    449.  
    450. } else {
    451. fetchCityItems(provinceID);
    452. }
    453.  
    454. }
    455.  
    456. else if (parent.getId() ==citySpinner.getId()) {
    457.  
    458. cityID = selectCityArrayList.get(position).getCityID();
    459. // Fetch the category items from server by the hit api
    460.  
    461. System.out.println("select city items");
    462. if (cityID.equals("null") || cityID.equals("0") ) {
    463. //fetchCityItems(provinceID);
    464. } else {
    465. fetchSuberbItems(cityID);
    466. }
    467.  
    468. }
    469. else if(parent.getId() == suberbSpinner.getId()) {
    470.  
    471. superbID = selectSuberbArrayList.get(position).getSuberbID();
    472. MyUtility.showLog("sub urb id ", superbID);
    473. }
    474. }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: