Skip to content

Instantly share code, notes, and snippets.

@bangiqi
Created January 31, 2018 09:28
Show Gist options
  • Save bangiqi/3ecf78bb06ffd72c68270e63fd8ab737 to your computer and use it in GitHub Desktop.
Save bangiqi/3ecf78bb06ffd72c68270e63fd8ab737 to your computer and use it in GitHub Desktop.
package com.labs.ramdani.klubpompibpom.activity.klubpompi.view;
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.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.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
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.MyFunc;
import com.labs.ramdani.klubpompibpom.activity.klubpompi.fungsi.StringWithTag;
import com.labs.ramdani.klubpompibpom.activity.klubpompi.survey.SurveyFirstQuestionFasilitatorActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class BiodataFasilitatorActivity extends AppCompatActivity {
private static final String TAG = "biodata_fas";
@Bind(R.id.tv_date_now)
TextView tvDateNow;
@Bind(R.id.txt_nama_fasilitator)
EditText txtNamaFasilitator;
@Bind(R.id.til2)
TextInputLayout til2;
@Bind(R.id.til3)
TextInputLayout til3;
@Bind(R.id.txt_alamat_sekolah)
EditText txtAlamatSekolah;
@Bind(R.id.txt_objek_sekolah)
EditText txtObjekSekolah;
@Bind(R.id.txt_lokasi)
EditText txtSignatureLokasi;
@Bind(R.id.fab)
FloatingActionButton fab;
//inisiasi SharedPreferences
SharedPreferences sharedPref;
//deklarasi spinner
//Spinner spinnerParent;
//Spinner spinnerChild;
//private static final String TAG = "lihat";
private static final String TAG_SELECT = "onSelected";
//Declaring an Spinner
private Spinner spinner;
private Spinner spinnerRegencies;
//An ArrayList for Spinner Items
private ArrayList<String> students;
//private ArrayList<String> ListProvinsi;
HashMap<String,String> spinnerMap;
HashMap<String,String> spinnerMapRegencies;
List<StringWithTag> itemList;
List<StringWithTag> itemListRegencies;
//JSON Array
private JSONArray provinsi;
private JSONArray regencies;
RequestQueue requestQueue;
RequestQueue requestQueue2;
ArrayAdapter<StringWithTag> myAdapterRegencies;
SharedPreferences.Editor editor;
String getProvinsi;
String getRegencies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fasilitator);
ButterKnife.bind(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
spinnerMap = new HashMap<String, String>();
spinnerMapRegencies = new HashMap<String, String>();
itemList = new ArrayList<StringWithTag>();
itemListRegencies = new ArrayList<StringWithTag>();
Log.d(TAG, "ambil date sekarang "+MyFunc.getDateNow());
SimpleDateFormat sdfdate = new SimpleDateFormat("EEEE , dd MMMM yyyy");
Date tanggal = new Date();
System.out.println("Sekarang tanggal: " + sdfdate.format(tanggal));
tvDateNow.setText(sdfdate.format(tanggal));
//tvDateNow.setText(MyFunc.getDateNow());
//setting shared preference MODE_PRIVATE
sharedPref = getApplicationContext().getSharedPreferences(MyPreferences.MyPREFERENCES_FASILITATOR, Context.MODE_PRIVATE);
editor = sharedPref.edit();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "provinsi fab: "+getProvinsi);
Log.d(TAG, "regencies fab: "+getRegencies);
Log.d(TAG, "radio pilih: "+pilih);
// get value from editText
//String getDaydate = "Bogor";
String getNamaFasilitator = String.valueOf(txtNamaFasilitator.getText());
String getAsalDaerahProvinsi = getProvinsi;
String getAsalDaerahKotKab = getRegencies;
String getObjekSekolah = String.valueOf(txtObjekSekolah.getText());
String getAlamatSekolah = String.valueOf(txtAlamatSekolah.getText());
String getProfileBiodata = pilihBiodata;
//String getHistory = pilih;
//String getSertifikasi = pilih_sertifikasi;
String getSignature = String.valueOf(txtSignatureLokasi.getText());
//Intent i = new Intent(getApplicationContext(), SurveyFasilitatorActivity.class);
Intent i = new Intent(getApplicationContext(), SurveyFirstQuestionFasilitatorActivity.class);
// put to Bundle Extra Intent
//i.putExtra("GET_DAY_DATE", getDaydate);
i.putExtra("GET_NAME_FASILITATOR", getNamaFasilitator);
i.putExtra("GET_ASAL_DAERAH_PROVINSI", getAsalDaerahProvinsi);
i.putExtra("GET_ASAL_DAERAH_KOTAKAB", getAsalDaerahKotKab);
i.putExtra("GET_OBJEK_SEKOLAH", getObjekSekolah);
i.putExtra("GET_ALAMAT_SEKOLAH", getAlamatSekolah);
i.putExtra("GET_PROFILE_BIO", getProfileBiodata);
//i.putExtra("GET_HISTORY", getHistory);
//i.putExtra("GET_SERTIFIKASI", getSertifikasi);
i.putExtra("GET_SIGNATURE", getSignature);
// add string on key from editText
editor.putString(MyPreferences.KEY_NAMA_FAS, getNamaFasilitator);
editor.putString(MyPreferences.KEY_ASAL_DAERAH_PROVINSI, getAsalDaerahProvinsi);
editor.putString(MyPreferences.KEY_ASAL_DAERAH_KOTAKAB, getAsalDaerahKotKab);
editor.putString(MyPreferences.KEY_OBJEK_SEKOLAH, getObjekSekolah);
editor.putString(MyPreferences.KEY_ALAMAT_SEKOLAH, getAlamatSekolah);
editor.putString(MyPreferences.KEY_PROFILE_BIO, getProfileBiodata);
//editor.putString(MyPreferences.KEY_HISTORY, getHistory);
//editor.putString(MyPreferences.KEY_SERTIFIKASI, getSertifikasi);
editor.putString(MyPreferences.KEY_SIGNATURE, getSignature);
// save
editor.commit();
startActivity(i);
}
});
spinner = (Spinner) findViewById(R.id.provinsi_spin);
spinnerRegencies = (Spinner) findViewById(R.id.kotakabupaten_spin);
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) {
}
});
}
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>(BiodataFasilitatorActivity.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>(BiodataFasilitatorActivity.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