Skip to content

Instantly share code, notes, and snippets.

@bangiqi
Created April 30, 2017 09:34
Show Gist options
  • Save bangiqi/1a55e36daa202d410beaafe38a45b40b to your computer and use it in GitHub Desktop.
Save bangiqi/1a55e36daa202d410beaafe38a45b40b to your computer and use it in GitHub Desktop.
package com.labs.ramdani.klubpompibpom.activity.klubpompi.view;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.labs.ramdani.klubpompibpom.R;
import com.labs.ramdani.klubpompibpom.activity.klubpompi.config.MyPreferences;
import com.labs.ramdani.klubpompibpom.activity.klubpompi.fungsi.Config;
import com.labs.ramdani.klubpompibpom.activity.klubpompi.fungsi.StringWithTag;
import com.labs.ramdani.klubpompibpom.activity.klubpompi.survey.SurveyFirstQuestionSiswaActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class BiodataSiswaActivity extends AppCompatActivity {
private static final String TAG = "debug_biodata_fas";
@Bind(R.id.txtNama)
EditText txtNama;
@Bind(R.id.til)
TextInputLayout til;
@Bind(R.id.txtKelas)
EditText txtKelas;
@Bind(R.id.til2)
TextInputLayout til2;
@Bind(R.id.txtAsalSekolah)
EditText txtAsalSekolah;
@Bind(R.id.til3)
TextInputLayout til3;
@Bind(R.id.txt_alamat)
EditText txtAlamatSekolah;
@Bind(R.id.til4)
TextInputLayout til4;
@Bind(R.id.fab)
FloatingActionButton fab;
//inisiasi SharedPreferences
SharedPreferences sharedPref;
//Declaring an Spinner
private Spinner spinner;
private Spinner spinnerRegencies;
HashMap<String,String> spinnerMap;
HashMap<String,String> spinnerMapRegencies;
List<StringWithTag> itemList;
List<StringWithTag> itemListRegencies;
String getProvinsi;
String getRegencies;
//JSON Array
private JSONArray provinsi;
private JSONArray regencies;
RequestQueue requestQueue;
RequestQueue requestQueue2;
ArrayAdapter<StringWithTag> myAdapterRegencies;
AlertDialog.Builder alertDialogBuilder;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biodata_siswa);
ButterKnife.bind(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
spinner = (Spinner) findViewById(R.id.provinsi_spin);
spinnerRegencies = (Spinner) findViewById(R.id.kotakabupaten_spin);
spinnerMap = new HashMap<String, String>();
spinnerMapRegencies = new HashMap<String, String>();
itemList = new ArrayList<StringWithTag>();
itemListRegencies = new ArrayList<StringWithTag>();
getData();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
itemListRegencies.clear();
StringWithTag ss = (StringWithTag) parent.getItemAtPosition(position);
String key = ss.tag;
getProvinsi = ss.string;
String getURLRegencies = Config.DATA_URL_REGENCIES+key;
Log.d(TAG, "URL: "+getURLRegencies);
Log.d(TAG, "provinsi: "+getProvinsi);
StringRequest stringRequestRegencies = new StringRequest(getURLRegencies,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
regencies = j.getJSONArray(Config.JSON_ARRAY_REGENCIES);
System.out.print("tampil data regencies json "+regencies.toString());
Log.d(TAG, "onResponse length: "+j.length());
Log.d(TAG, "onResponse: "+regencies.toString());
Log.d(TAG, "itemlistregen: "+itemListRegencies);
getListRegencies(regencies);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequestRegencies);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerRegencies.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//itemListRegencies.clear();
StringWithTag ss = (StringWithTag) parent.getItemAtPosition(position);
String key = ss.tag;
getRegencies = ss.string;
Log.d(TAG, "kota/kabupaten: "+getRegencies);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get value from editText
String getNama = String.valueOf(txtNama.getText());
String getKelas = String.valueOf(txtKelas.getText());
String getAsalSekolah = String.valueOf(txtAsalSekolah.getText());
String getAlamatSekolah = String.valueOf(txtAlamatSekolah.getText());
String getAsalDaerahProvinsi = getProvinsi;
String getAsalDaerahKotKab = getRegencies;
Log.d(TAG, "tampil_prov :"+getAsalDaerahProvinsi);
Log.d(TAG, "tampil_regencies :"+getAsalDaerahKotKab);
if (TextUtils.isEmpty(getNama) || TextUtils.isEmpty(getKelas) || TextUtils.isEmpty(getAsalSekolah) || TextUtils.isEmpty(getAlamatSekolah)
|| TextUtils.isEmpty(getAsalDaerahProvinsi) || TextUtils.isEmpty(getAsalDaerahKotKab)) {
validasi();
return;
}else {
Log.d(TAG, "onClick: terisi semua");
Intent i = new Intent(getApplicationContext(), SurveyFirstQuestionSiswaActivity.class);
// put to Bundle Extra Intent
i.putExtra("GET_NAME", getNama);
i.putExtra("GET_KELAS", getKelas);
i.putExtra("GET_ASAL_SEKOLAH", getAsalSekolah);
i.putExtra("GET_ALAMAT_SEKOLAH", getAlamatSekolah);
i.putExtra("GET_ASAL_DAERAH_PROVINSI", getAsalDaerahProvinsi);
i.putExtra("GET_ASAL_DAERAH_KOTAKAB", getAsalDaerahKotKab);
//setting shared preference MODE_PRIVATE
sharedPref = getApplicationContext().getSharedPreferences(MyPreferences.MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
// add string on key from editText
editor.putString(MyPreferences.KEY_NAMA, getNama);
editor.putString(MyPreferences.KEY_KELAS, getKelas);
editor.putString(MyPreferences.KEY_ASAL_SEKOLAH, getAsalSekolah);
editor.putString(MyPreferences.KEY_ALAMAT_SEKOLAH, getAlamatSekolah);
editor.putString(MyPreferences.KEY_ASAL_DAERAH_PROVINSI_SISWA, getProvinsi);
editor.putString(MyPreferences.KEY_ASAL_DAERAH_KOTAKAB_SISWA, getRegencies);
// save
editor.commit();
startActivity(i);
}
}
});
}
public void validasi(){
alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Peringatan");
// set dialog message
alertDialogBuilder
.setMessage("maaf biodata siswa harus diisi semua!")
.setCancelable(true)
.setNegativeButton("kembali",
null).create();
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
private void getData(){
//Creating a string request
StringRequest stringRequest = new StringRequest(Config.DATA_URL_PROVINCE,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
provinsi = j.getJSONArray(Config.JSON_ARRAY);
System.out.print("tampil data json "+provinsi.toString());
Log.d(TAG, "onResponse: "+provinsi.toString());
//Calling method getStudents to get the students from the JSON Array
//getStudents(result);
getProvince(provinsi);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getProvince(JSONArray j){
//Traversing through all the items in the json array
for(int i=0;i<j.length();i++){
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
Log.d(TAG, "count json : "+json.length());
Log.d(TAG, "count j : "+j.length());
spinnerMap.put(json.getString(Config.TAG_ID_PROVINCE),json.getString(Config.TAG_NAME_PROVINCE));
itemList.add(new StringWithTag(json.getString(Config.TAG_NAME_PROVINCE),json.getString(Config.TAG_ID_PROVINCE)));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<StringWithTag>(BiodataSiswaActivity.this, android.R.layout.simple_spinner_dropdown_item, itemList));
}
private void getListRegencies(JSONArray j){
//Traversing through all the items in the json array
for(int i=0;i<j.length();i++){
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
Log.d(TAG, "count json : "+json.length());
Log.d(TAG, "count j : "+j.length());
spinnerMapRegencies.put(json.getString(Config.TAG_ID_PROVINCE),json.getString(Config.TAG_NAME_PROVINCE));
itemListRegencies.add(new StringWithTag(json.getString(Config.TAG_NAME_PROVINCE),json.getString(Config.TAG_ID_PROVINCE)));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
myAdapterRegencies = new ArrayAdapter<StringWithTag>(BiodataSiswaActivity.this, android.R.layout.simple_spinner_dropdown_item);
myAdapterRegencies.clear();
myAdapterRegencies.addAll(itemListRegencies);
myAdapterRegencies.notifyDataSetChanged();
spinnerRegencies.setAdapter(myAdapterRegencies);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment