Code :
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".deTails">
<EditText
android:id="@+id/txt_unm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Username"
android:inputType="textPersonName"
/>
<EditText
android:id="@+id/txt_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
/>
<EditText
android:id="@+id/txt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Email"
android:inputType="textEmailAddress"
/>
<EditText
android:id="@+id/txt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Phone"
android:inputType="phone"
/>
<Spinner
android:id="@+id/country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/txt_bdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date"
/>
<TextView
android:id="@+id/textView"
android:layout_width="63dp"
android:layout_height="wrap_content"
android:text="Intrest:"
android:textSize="18sp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
tools:text="Coding" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
tools:text="Hacking" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gaming" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
package
com.example.devang.exhello;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import
android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class JsonSpinner extends
AppCompatActivity {
Spinner spinnerCountry, spinnerState;
ArrayList<String> countryList = new
ArrayList<String>();
ArrayList<String> stateList = new
ArrayList<String>();
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_spinner);
bindControls();
final String jsonObj = getJSON();
if (jsonObj != null) {
try {
JSONObject jsonObject = new
JSONObject(jsonObj);
JSONArray jsonArrayCountry =
jsonObject.getJSONArray("countries");
for (int i = 0; i <
jsonArrayCountry.length(); i++) {
JSONObject inner_jo =
jsonArrayCountry.getJSONObject(i);
String country =
inner_jo.getString("country");
countryList.add(country);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.d("ERROR_CODE",
"NO DATA FOUND");
}
ArrayAdapter adapterCountry = new
ArrayAdapter(JsonSpinner.this, android.R.layout.simple_spinner_dropdown_item,
countryList);
spinnerCountry.setAdapter(adapterCountry);
spinnerCountry.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
//Toast.makeText(user_registration_basics.this,
"Item Selected "+pos, Toast.LENGTH_SHORT).show();
getStates(jsonObj, pos);
ArrayAdapter adapterState = new
ArrayAdapter(JsonSpinner.this, android.R.layout.simple_spinner_dropdown_item,
stateList);
spinnerState.setAdapter(adapterState);
}
@Override
public void
onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void getStates(String jsonObj, int
pos) {
try {
stateList.clear();
JSONObject jsonObject = new
JSONObject(jsonObj);
JSONArray jsonArray =
jsonObject.getJSONArray("countries");
JSONObject json_inner =
jsonArray.getJSONObject(pos);
JSONArray jsonArrayStates =
json_inner.getJSONArray("states");
for (int i = 0; i < jsonArrayStates.length();
i++) {
stateList.add(jsonArrayStates.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void bindControls() {
spinnerCountry = findViewById(R.id.country);
spinnerState =
findViewById(R.id.state);
}
public String getJSON() {
String json;
try {
InputStream is =
this.getAssets().open("list.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer,
"UTF-8");
} catch (IOException e) {
e.printStackTrace();
return null;
}
return json;
}
}
No comments:
Post a Comment