mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 13:38:41 +00:00
Get list district and ward from API
This commit is contained in:
Generated
-17
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="deploymentTargetDropDown">
|
||||
<runningDeviceTargetSelectedWithDropDown>
|
||||
<Target>
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="VIRTUAL_DEVICE_PATH" />
|
||||
<value value="$USER_HOME$/.android/avd/Pixel_6_API_33.avd" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2023-02-08T04:49:02.963423Z" />
|
||||
</component>
|
||||
</project>
|
||||
+7
-2
@@ -31,10 +31,15 @@ android {
|
||||
dependencies {
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.6.0'
|
||||
implementation 'com.google.android.material:material:1.8.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'com.google.android.material:material:1.8.0';
|
||||
|
||||
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
|
||||
implementation 'com.google.code.gson:gson:2.10.1'
|
||||
implementation 'com.google.android.material:material:1.8.0'
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.capstone.foodify.API;
|
||||
|
||||
import com.capstone.foodify.DistrictWardResponse.DisctrictWardResponse;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
|
||||
public interface ApiService {
|
||||
|
||||
Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss dd-MM-yyyy").create();
|
||||
|
||||
ApiService apiService = new Retrofit.Builder()
|
||||
.baseUrl("https://vn-public-apis.fpo.vn/").addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.build()
|
||||
.create(ApiService.class);
|
||||
|
||||
@GET("wards/getByDistrict")
|
||||
Call<DisctrictWardResponse> wardResponse(@Query("districtCode") int districtCode, @Query("limit") int limit);
|
||||
|
||||
@GET("districts/getByProvince")
|
||||
Call<DisctrictWardResponse> districtResponse(@Query("provinceCode") int provinceCode, @Query("limit") int limit);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.capstone.foodify.DistrictWardResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Data {
|
||||
private int nItems;
|
||||
private int nPages;
|
||||
private List<Datas> data;
|
||||
|
||||
public int getnItems() {
|
||||
return nItems;
|
||||
}
|
||||
|
||||
public void setnItems(int nItems) {
|
||||
this.nItems = nItems;
|
||||
}
|
||||
|
||||
public int getnPages() {
|
||||
return nPages;
|
||||
}
|
||||
|
||||
public void setnPages(int nPages) {
|
||||
this.nPages = nPages;
|
||||
}
|
||||
|
||||
public List<Datas> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(List<Datas> data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.capstone.foodify.DistrictWardResponse;
|
||||
|
||||
public class Datas {
|
||||
private String _id;
|
||||
private String name;
|
||||
private String type;
|
||||
private String slug;
|
||||
private String name_with_type;
|
||||
private String path;
|
||||
private String path_with_type;
|
||||
private String code;
|
||||
private String parent_code;
|
||||
private String isDeleted;
|
||||
|
||||
public String get_id() {
|
||||
return _id;
|
||||
}
|
||||
|
||||
public void set_id(String _id) {
|
||||
this._id = _id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getSlug() {
|
||||
return slug;
|
||||
}
|
||||
|
||||
public void setSlug(String slug) {
|
||||
this.slug = slug;
|
||||
}
|
||||
|
||||
public String getName_with_type() {
|
||||
return name_with_type;
|
||||
}
|
||||
|
||||
public void setName_with_type(String name_with_type) {
|
||||
this.name_with_type = name_with_type;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getPath_with_type() {
|
||||
return path_with_type;
|
||||
}
|
||||
|
||||
public void setPath_with_type(String paht_with_type) {
|
||||
this.path_with_type = paht_with_type;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getParent_code() {
|
||||
return parent_code;
|
||||
}
|
||||
|
||||
public void setParent_code(String parent_code) {
|
||||
this.parent_code = parent_code;
|
||||
}
|
||||
|
||||
public String getIsDeleted() {
|
||||
return isDeleted;
|
||||
}
|
||||
|
||||
public void setIsDeleted(String isDeleted) {
|
||||
this.isDeleted = isDeleted;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.capstone.foodify.DistrictWardResponse;
|
||||
|
||||
public class DisctrictWardResponse {
|
||||
private int exitcode;
|
||||
private Data data;
|
||||
private String message;
|
||||
|
||||
public int getExitcode() {
|
||||
return exitcode;
|
||||
}
|
||||
|
||||
public void setExitcode(int exitcode) {
|
||||
this.exitcode = exitcode;
|
||||
}
|
||||
|
||||
public Data getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,13 @@ package com.capstone.foodify.Fragment;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.capstone.foodify.R;
|
||||
import com.capstone.foodify.SignUpActivity;
|
||||
|
||||
|
||||
@@ -9,13 +9,18 @@ import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.DatePicker;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.capstone.foodify.API.ApiService;
|
||||
import com.capstone.foodify.DistrictWardResponse.Datas;
|
||||
import com.capstone.foodify.DistrictWardResponse.DisctrictWardResponse;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -25,8 +30,15 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class SignUpActivity extends AppCompatActivity {
|
||||
|
||||
private static final int PROVINCE_CODE = 48;
|
||||
private static final int LIMIT = -1;
|
||||
|
||||
TextInputLayout textInput_email, textInput_password, textInput_phone,
|
||||
textInput_address, textInput_repeatPassword, textInput_firstName, textInput_lastName, textInput_birthDay;
|
||||
final Calendar myCalendar= Calendar.getInstance();
|
||||
@@ -35,6 +47,13 @@ public class SignUpActivity extends AppCompatActivity {
|
||||
Spinner wardSpinner, districtSpinner;
|
||||
ImageView back_image;
|
||||
|
||||
private static final ArrayList<String> wardList = new ArrayList<>();
|
||||
private static final ArrayList<String> districtList = new ArrayList<>();
|
||||
private static List<Datas> districtListData = new ArrayList<>();
|
||||
private static List<Datas> wardListData = new ArrayList<>();
|
||||
|
||||
String selectedDistrict = "";
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -43,7 +62,12 @@ public class SignUpActivity extends AppCompatActivity {
|
||||
|
||||
setFontUI();
|
||||
chooseDateOfBirth();
|
||||
listWardAndDistrict();
|
||||
|
||||
districtSpinner = (Spinner) findViewById(R.id.district);
|
||||
wardSpinner = (Spinner) findViewById(R.id.ward);
|
||||
|
||||
getListDistrict();
|
||||
getListWard(0);
|
||||
|
||||
signIn_textView = (TextView) findViewById(R.id.signIn_textView);
|
||||
signIn_textView.setOnClickListener(new View.OnClickListener() {
|
||||
@@ -61,34 +85,99 @@ public class SignUpActivity extends AppCompatActivity {
|
||||
onBackPressed();
|
||||
}
|
||||
});
|
||||
|
||||
districtSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
int districtCode = 0;
|
||||
|
||||
selectedDistrict = districtSpinner.getSelectedItem().toString();
|
||||
|
||||
//Get id district from selected item district;
|
||||
for(Datas dataTemp: districtListData){
|
||||
if(selectedDistrict.equals(dataTemp.getName_with_type())){
|
||||
districtCode = Integer.parseInt(dataTemp.getCode());
|
||||
break;
|
||||
}
|
||||
}
|
||||
getListWard(districtCode);
|
||||
}
|
||||
|
||||
private void listWardAndDistrict() {
|
||||
wardSpinner = (Spinner) findViewById(R.id.ward);
|
||||
districtSpinner = (Spinner) findViewById(R.id.district);
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> adapterView) {
|
||||
|
||||
ArrayList<String> ward = new ArrayList<String>();
|
||||
ArrayList<String> district = new ArrayList<String>();
|
||||
|
||||
ward.add("---Phường");
|
||||
ward.add("Hoà Hiệp Bắc");
|
||||
ward.add("Hòa Khánh Bắc");
|
||||
ward.add("Hòa Minh");
|
||||
ward.add("Hòa Hiệp Nam");
|
||||
ward.add("Hòa Khánh Nam");
|
||||
|
||||
district.add("---Quận");
|
||||
district.add("Liên Chiểu");
|
||||
district.add("Ngũ Hành Sơn");
|
||||
|
||||
MySpinnerAdapter adapterWard = new MySpinnerAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, ward);
|
||||
MySpinnerAdapter adapterDistrict = new MySpinnerAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, district);
|
||||
wardSpinner.setAdapter(adapterWard); // this will set list of values to spinner
|
||||
districtSpinner.setAdapter(adapterDistrict); // this will set list of values to spinner
|
||||
|
||||
wardSpinner.setSelection(ward.indexOf("---Phường"));//set selected value in spinner
|
||||
districtSpinner.setSelection(district.indexOf("---Quận"));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void getListDistrict() {
|
||||
|
||||
districtList.add("---Quận");
|
||||
|
||||
ApiService.apiService.districtResponse(PROVINCE_CODE, LIMIT).enqueue(new Callback<DisctrictWardResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<DisctrictWardResponse> call, Response<DisctrictWardResponse> response) {
|
||||
DisctrictWardResponse districtResponse = response.body();
|
||||
if(districtResponse != null && districtResponse.getExitcode() == 1){
|
||||
|
||||
districtListData = districtResponse.getData().getData();
|
||||
for(Datas tempData: districtListData){
|
||||
districtList.add(tempData.getName_with_type());
|
||||
}
|
||||
};
|
||||
|
||||
setAdapter(districtList, "---Quận", districtSpinner);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<DisctrictWardResponse> call, Throwable t) {
|
||||
Toast.makeText(SignUpActivity.this, "Đã xảy ra lỗi hệ thống. Vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void getListWard(int districtCode) {
|
||||
|
||||
if(wardList.size() > 1)
|
||||
wardList.clear();
|
||||
|
||||
if(wardList.size() == 0)
|
||||
wardList.add("---Phường");
|
||||
|
||||
if(districtCode != 0){
|
||||
ApiService.apiService.wardResponse(districtCode, LIMIT).enqueue(new Callback<DisctrictWardResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<DisctrictWardResponse> call, Response<DisctrictWardResponse> response) {
|
||||
|
||||
DisctrictWardResponse wardResponse = response.body();
|
||||
if(wardResponse != null && wardResponse.getExitcode() == 1){
|
||||
|
||||
wardListData = wardResponse.getData().getData();
|
||||
for(Datas tempData: wardListData){
|
||||
wardList.add(tempData.getName_with_type());
|
||||
}
|
||||
};
|
||||
|
||||
setAdapter(wardList, "---Phường", wardSpinner);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<DisctrictWardResponse> call, Throwable t) {
|
||||
Toast.makeText(SignUpActivity.this, "Đã xảy ra lỗi hệ thống. Vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
setAdapter(wardList, "---Phường", wardSpinner);
|
||||
}
|
||||
}
|
||||
|
||||
private void setAdapter(ArrayList<String> data, String defaultItem, Spinner dataSpinner){
|
||||
MySpinnerAdapter adapterWard = new MySpinnerAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, data);
|
||||
dataSpinner.setAdapter(adapterWard); // this will set list of values to spinner
|
||||
dataSpinner.setSelection(data.indexOf(defaultItem));//set selected value in spinner
|
||||
}
|
||||
|
||||
private static class MySpinnerAdapter extends ArrayAdapter<String> {
|
||||
// Initialise custom font, for example:
|
||||
Typeface font = Typeface.createFromAsset(getContext().getAssets(),
|
||||
|
||||
@@ -190,12 +190,14 @@
|
||||
android:id="@+id/district"
|
||||
android:layout_width="190dp"
|
||||
android:layout_height="match_parent"
|
||||
android:textAlignment="viewEnd"
|
||||
android:layout_marginEnd="12dp"
|
||||
android:contentDescription="district"
|
||||
/>
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/ward"
|
||||
android:textAlignment="viewEnd"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:contentDescription="ward"
|
||||
|
||||
Reference in New Issue
Block a user