I've got a problem that I am really confused, doing so much debugging, but still cannot solve the problem.
id_kecamatan = categoriesList.get(position).getId();
value changed when the second spinner generated with second Asynctask, which is not suppose to be happen, but if I'm not execute the code which is found in public void onItemSelected
the value of ID position not changed, which is suppose to be happen, and if I'm not put this code I can't populate the second spinner because the second spinner is based on first spinner value. This problem causing multiple looping onSelectedItem. please help. thanks.
if I put this code down below, categoriesList.get(position).getId();
is changed.
if (cekspin != cekstatusspin1){
spinnerFood2.setEnabled(true);
id_kecamatan = categoriesList.get(position).getId();
Log.d("MASUK KE 2: ", "> " + id_kecamatan);
cekstatusspin1 = cekspin;
//run the second asynctask for second spinner
runMultipleAsyncTask2();
}
Here the complete code:
public class MainActivity extends Activity implements OnItemSelectedListener {
private Spinner spinnerFood;
private Spinner spinnerFood2;
// array list for spinner adapter
private ArrayList<Category> categoriesList;
private ArrayList<Category> categoriesList2;
ProgressDialog pDialog;
int id_kecamatan,id_kelurahan,vv;
// API urls
// Url to get all categories
private String URL_CATEGORIES = "http://192.168.1.88/android_spinnner/get_categories.php";
private String URL_CATEGORIES2 = "http://192.168.1.88/android_spinnner/get_kelurahan.php?id_kabupaten=";
private String URL2 = URL_CATEGORIES2;
TextView texji;
String cekstatusspin1,cekspin,cekspin2,b= null;
String nilai = "ada";
ArrayAdapter<String> spinnerAdapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerFood = (Spinner) findViewById(R.id.spinFood);
spinnerFood2 = (Spinner) findViewById(R.id.spinner2);
spinnerFood2.setEnabled(false);
categoriesList = new ArrayList<Category>();
categoriesList2 = new ArrayList<Category>();
texji = (TextView)findViewById(R.id.text);
// spinner item select listener
spinnerFood.setOnItemSelectedListener(this);
spinnerFood2.setOnItemSelectedListener(this);
runMultipleAsyncTask();
}
/**
* Adding spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getName());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerFood.setAdapter(spinnerAdapter);
}
/**
* Adding spinner data
* */
private void populateSpinner2() {
if (spinnerAdapter2 != null) {
spinnerAdapter2 = null;
}
Log.d("adapter terakhir: ", "> " + spinnerAdapter2);
List<String> lables2 = new ArrayList<String>();
Log.d("lables2: ", "> " + lables2);
lables2.clear();
Log.d("lables2 clear: ", "> " + lables2);
for (int i = 0; i < categoriesList2.size(); i++) {
lables2.add(categoriesList2.get(i).getName());
}
// Creating adapter for spinner
spinnerAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables2);
spinnerAdapter2.notifyDataSetChanged();
Log.d("adapter terakhir: ", "> " + spinnerAdapter2);
// Drop down layout style - list view with radio button
spinnerAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerFood2.setAdapter(spinnerAdapter2);
}
private void runMultipleAsyncTask() // Run Multiple Async Task
{
GetCategories asyncTask = new GetCategories(); // First
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void runMultipleAsyncTask2() // Run Multiple Async Task//
{
GetCategories2 asyncTask2 = new GetCategories2(); // Second
// Above Api Level 13
asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
//Start First Async Task:
/**
* Async task to get all food categories
* */
private class GetCategories extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Mengambil data KECAMATAN..");
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj
.getJSONArray("categories");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("id"),
catObj.getString("name"));
categoriesList.add(cat);
Log.d("kecamatan BBBBBA: ", "> " + id_kecamatan);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinner();
}
}
//Start Second Async Task:
/**
* Async task to get all food categories
* */
private class GetCategories2 extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Mengambil data kELURAHAN..");
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser2 = new ServiceHandler();
String json2 = jsonParser2.makeServiceCall(URL2+cekspin, ServiceHandler.GET);
String x = URL2+cekspin;
Log.d("URL: ", "> " + x);
if (json2 != null) {
try {
JSONObject jsonObj2 = new JSONObject(json2);
if (jsonObj2 != null) {
JSONArray categories = jsonObj2
.getJSONArray("categories");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj2 = (JSONObject) categories.get(i);
Category cat2 = new Category(catObj2.getInt("id"),
catObj2.getString("kecamatan"));
categoriesList2.add(cat2);
Log.d("kecamatan BACKGOUNR: ", "> " + id_kecamatan);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("JSON3: ", "> " + json2);
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinner2();
}
}
@Override
public void onItemSelected (AdapterView<?> parent, View view,int position,
long id){
Toast.makeText(
getApplicationContext(),
parent.getItemAtPosition(position).toString() + " Selected",
Toast.LENGTH_LONG).show();
Log.d("cekspinA: ", "> " + cekspin);
Log.d("cekstatuspin1A: ", "> " + cekstatusspin1);
Log.d("kecamatan Q: ", "> " + id_kecamatan);
id_kecamatan = categoriesList.get(position).getId();
Log.d("kecamatan Z: ", "> " + id_kecamatan);
// cekspin = cekstatusspin1;
Log.d("categorylist: ", "> " + categoriesList);
cekspin = String.valueOf(id_kecamatan);
texji.setText(cekspin);
Log.d("cekspin B: ", "> " + cekspin);
Log.d("cekstatuspin1 B: ", "> " + cekstatusspin1);
if (cekspin != cekstatusspin1){
spinnerFood2.setEnabled(true);
id_kecamatan = categoriesList.get(position).getId();
Log.d("MASUK KE 2: ", "> " + id_kecamatan);
cekstatusspin1 = cekspin;
//run the second asynctask for second spinner
runMultipleAsyncTask2();
}
}
@Override
public void onNothingSelected (AdapterView < ? > arg0){
}
}
logcat
05-29 22:14:43.747 7541-7612/info.androidhive.spinnermysql E/Response:: > {"categories":[{"id":"64","name":"Kecamatan Bombarai"},{"id":"65","name":"Kecamatan Fakfak"},{"id":"66","name":"Kecamatan Fakfak Barat"},{"id":"67","name":"Kecamatan Fakfak Tengah"},{"id":"68","name":"Kecamatan Fakfak Timur"},{"id":"69","name":"Kecamatan Karas"},{"id":"70","name":"Kecamatan Kokas"},{"id":"71","name":"Kecamatan Kramongmongga"},{"id":"72","name":"Kecamatan Teluk Patipi"}]}
05-29 22:14:43.937 7541-7541/info.androidhive.spinnermysql D/cekspinA:: > null
05-29 22:14:43.937 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1A:: > null
05-29 22:14:43.937 7541-7541/info.androidhive.spinnermysql D/kecamatan Q:: > 0
05-29 22:14:43.937 7541-7541/info.androidhive.spinnermysql D/kecamatan Z:: > 64
05-29 22:14:43.937 7541-7541/info.androidhive.spinnermysql D/categorylist:: > [info.androidhive.spinnermysql.Category@28f0463, info.androidhive.spinnermysql.Category@31cd3860, info.androidhive.spinnermysql.Category@35ce7119, info.androidhive.spinnermysql.Category@21b1f5de, info.androidhive.spinnermysql.Category@196383bf, info.androidhive.spinnermysql.Category@63d7a8c, info.androidhive.spinnermysql.Category@37cc05d5, info.androidhive.spinnermysql.Category@36391ea, info.androidhive.spinnermysql.Category@cd69cdb]
05-29 22:14:43.947 7541-7541/info.androidhive.spinnermysql D/cekspin B:: > 64
05-29 22:14:43.947 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1 B:: > null
05-29 22:14:43.947 7541-7541/info.androidhive.spinnermysql D/MASUK KE 2:: > 64
05-29 22:14:43.997 7541-7541/info.androidhive.spinnermysql D/DisplayManager: DisplayManager()
05-29 22:14:44.007 7541-7640/info.androidhive.spinnermysql I/System.out: Thread-221084(ApacheHTTPLog):isSBSettingEnabled false
05-29 22:14:44.007 7541-7640/info.androidhive.spinnermysql I/System.out: Thread-221084(ApacheHTTPLog):isShipBuild true
05-29 22:14:44.007 7541-7640/info.androidhive.spinnermysql I/System.out: Thread-221084(ApacheHTTPLog):SMARTBONDING_ENABLED is false
05-29 22:14:44.007 7541-7640/info.androidhive.spinnermysql I/System.out: Thread-221084(ApacheHTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
05-29 22:14:44.037 7541-7640/info.androidhive.spinnermysql I/System.out: AsyncTask #2 calls detatch()
05-29 22:14:44.037 7541-7640/info.androidhive.spinnermysql D/URL:: > http://192.168.1.88/android_spinnner/get_kelurahan.php?id_kabupaten=64
05-29 22:14:44.037 7541-7640/info.androidhive.spinnermysql D/JSON3:: > {"categories":[{"id":"415","id2":"64","kecamatan":"Kelurahan\/Desa Bimajaya"},{"id":"416","id2":"64","kecamatan":"Kelurahan\/Desa Bumimoroh Indah"},{"id":"417","id2":"64","kecamatan":"Kelurahan\/Desa Mekar Sari"},{"id":"418","id2":"64","kecamatan":"Kelurahan\/Desa Onim Sari"},{"id":"419","id2":"64","kecamatan":"Kelurahan\/Desa Otoweri"},{"id":"420","id2":"64","kecamatan":"Kelurahan\/Desa Pinang Agung"},{"id":"421","id2":"64","kecamatan":"Kelurahan\/Desa Tomage"},{"id":"422","id2":"64","kecamatan":"Kelurahan\/Desa Warimo"},{"id":"423","id2":"64","kecamatan":"Kelurahan\/Desa Warisa Mulya\/Mulia"},{"id":"424","id2":"64","kecamatan":"Kelurahan\/Desa Wonodadi Mulya\/Mulia"}]}
05-29 22:14:44.127 7541-7541/info.androidhive.spinnermysql D/adapter terakhir:: > null
05-29 22:14:44.127 7541-7541/info.androidhive.spinnermysql D/lables2:: > []
05-29 22:14:44.127 7541-7541/info.androidhive.spinnermysql D/lables2 clear:: > []
05-29 22:14:44.127 7541-7541/info.androidhive.spinnermysql D/adapter terakhir:: > android.widget.ArrayAdapter@3e2349c1
05-29 22:14:44.167 7541-7541/info.androidhive.spinnermysql D/cekspinA:: > 64
05-29 22:14:44.167 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1A:: > 64
05-29 22:14:44.167 7541-7541/info.androidhive.spinnermysql D/kecamatan Q:: > 64
05-29 22:14:44.167 7541-7541/info.androidhive.spinnermysql D/kecamatan Z:: > 64
05-29 22:14:44.167 7541-7541/info.androidhive.spinnermysql D/categorylist:: > [info.androidhive.spinnermysql.Category@28f0463, info.androidhive.spinnermysql.Category@31cd3860, info.androidhive.spinnermysql.Category@35ce7119, info.androidhive.spinnermysql.Category@21b1f5de, info.androidhive.spinnermysql.Category@196383bf, info.androidhive.spinnermysql.Category@63d7a8c, info.androidhive.spinnermysql.Category@37cc05d5, info.androidhive.spinnermysql.Category@36391ea, info.androidhive.spinnermysql.Category@cd69cdb]
05-29 22:14:44.167 7541-7541/info.androidhive.spinnermysql D/cekspin B:: > 64
05-29 22:14:44.167 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1 B:: > 64
05-29 22:14:47.387 7541-7541/info.androidhive.spinnermysql D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
05-29 22:14:48.467 7541-7541/info.androidhive.spinnermysql D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
05-29 22:14:48.627 7541-7541/info.androidhive.spinnermysql W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
05-29 22:14:48.647 7541-7541/info.androidhive.spinnermysql D/cekspinA:: > 64
05-29 22:14:48.647 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1A:: > 64
05-29 22:14:48.647 7541-7541/info.androidhive.spinnermysql D/kecamatan Q:: > 64
05-29 22:14:48.647 7541-7541/info.androidhive.spinnermysql D/kecamatan Z:: > 65
05-29 22:14:48.647 7541-7541/info.androidhive.spinnermysql D/categorylist:: > [info.androidhive.spinnermysql.Category@28f0463, info.androidhive.spinnermysql.Category@31cd3860, info.androidhive.spinnermysql.Category@35ce7119, info.androidhive.spinnermysql.Category@21b1f5de, info.androidhive.spinnermysql.Category@196383bf, info.androidhive.spinnermysql.Category@63d7a8c, info.androidhive.spinnermysql.Category@37cc05d5, info.androidhive.spinnermysql.Category@36391ea, info.androidhive.spinnermysql.Category@cd69cdb]
05-29 22:14:48.657 7541-7541/info.androidhive.spinnermysql D/cekspin B:: > 65
05-29 22:14:48.657 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1 B:: > 64
05-29 22:14:48.657 7541-7541/info.androidhive.spinnermysql D/MASUK KE 2:: > 65
05-29 22:14:48.687 7541-7972/info.androidhive.spinnermysql I/System.out: Thread-221086(ApacheHTTPLog):isSBSettingEnabled false
05-29 22:14:48.687 7541-7972/info.androidhive.spinnermysql I/System.out: Thread-221086(ApacheHTTPLog):isShipBuild true
05-29 22:14:48.687 7541-7972/info.androidhive.spinnermysql I/System.out: Thread-221086(ApacheHTTPLog):SMARTBONDING_ENABLED is false
05-29 22:14:48.687 7541-7972/info.androidhive.spinnermysql I/System.out: Thread-221086(ApacheHTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
05-29 22:14:48.737 7541-7972/info.androidhive.spinnermysql I/System.out: AsyncTask #3 calls detatch()
05-29 22:14:48.737 7541-7972/info.androidhive.spinnermysql D/URL:: > http://192.168.1.88/android_spinnner/get_kelurahan.php?id_kabupaten=65
05-29 22:14:48.747 7541-7972/info.androidhive.spinnermysql D/JSON3:: > {"categories":[{"id":"425","id2":"65","kecamatan":"Kelurahan\/Desa Fak Fak Selatan"},{"id":"426","id2":"65","kecamatan":"Kelurahan\/Desa Gewerpe"},{"id":"427","id2":"65","kecamatan":"Kelurahan\/Desa Kapaurtutin"},{"id":"428","id2":"65","kecamatan":"Kelurahan\/Desa Lusy Pkeri"},{"id":"429","id2":"65","kecamatan":"Kelurahan\/Desa Sekban"},{"id":"430","id2":"65","kecamatan":"Kelurahan\/Desa Sekru"},{"id":"431","id2":"65","kecamatan":"Kelurahan\/Desa Torea"},{"id":"432","id2":"65","kecamatan":"Kelurahan\/Desa Wagom"},{"id":"433","id2":"65","kecamatan":"Kelurahan\/Desa Fak Fak Utara"},{"id":"434","id2":"65","kecamatan":"Kelurahan\/Desa Dulan Pok Pok"},{"id":"435","id2":"65","kecamatan":"Kelurahan\/Desa Tanama"},{"id":"441","id2":"65","kecamatan":"Kelurahan\/Desa Werba"}]}
05-29 22:14:48.777 7541-7541/info.androidhive.spinnermysql D/adapter terakhir:: > null
05-29 22:14:48.777 7541-7541/info.androidhive.spinnermysql D/lables2:: > []
05-29 22:14:48.777 7541-7541/info.androidhive.spinnermysql D/lables2 clear:: > []
05-29 22:14:48.777 7541-7541/info.androidhive.spinnermysql D/adapter terakhir:: > android.widget.ArrayAdapter@16f81dd
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/cekspinA:: > 65
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1A:: > 65
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/kecamatan Q:: > 65
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/kecamatan Z:: > 64
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/categorylist:: > [info.androidhive.spinnermysql.Category@28f0463, info.androidhive.spinnermysql.Category@31cd3860, info.androidhive.spinnermysql.Category@35ce7119, info.androidhive.spinnermysql.Category@21b1f5de, info.androidhive.spinnermysql.Category@196383bf, info.androidhive.spinnermysql.Category@63d7a8c, info.androidhive.spinnermysql.Category@37cc05d5, info.androidhive.spinnermysql.Category@36391ea, info.androidhive.spinnermysql.Category@cd69cdb]
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/cekspin B:: > 64
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1 B:: > 65
05-29 22:14:48.817 7541-7541/info.androidhive.spinnermysql D/MASUK KE 2:: > 64
05-29 22:14:48.857 7541-7976/info.androidhive.spinnermysql I/System.out: Thread-221087(ApacheHTTPLog):isSBSettingEnabled false
05-29 22:14:48.857 7541-7976/info.androidhive.spinnermysql I/System.out: Thread-221087(ApacheHTTPLog):isShipBuild true
05-29 22:14:48.857 7541-7976/info.androidhive.spinnermysql I/System.out: Thread-221087(ApacheHTTPLog):SMARTBONDING_ENABLED is false
05-29 22:14:48.857 7541-7976/info.androidhive.spinnermysql I/System.out: Thread-221087(ApacheHTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
05-29 22:14:48.887 7541-7976/info.androidhive.spinnermysql I/System.out: AsyncTask #4 calls detatch()
05-29 22:14:48.887 7541-7976/info.androidhive.spinnermysql D/URL:: > http://192.168.1.88/android_spinnner/get_kelurahan.php?id_kabupaten=64
05-29 22:14:48.887 7541-7976/info.androidhive.spinnermysql D/JSON3:: > {"categories":[{"id":"415","id2":"64","kecamatan":"Kelurahan\/Desa Bimajaya"},{"id":"416","id2":"64","kecamatan":"Kelurahan\/Desa Bumimoroh Indah"},{"id":"417","id2":"64","kecamatan":"Kelurahan\/Desa Mekar Sari"},{"id":"418","id2":"64","kecamatan":"Kelurahan\/Desa Onim Sari"},{"id":"419","id2":"64","kecamatan":"Kelurahan\/Desa Otoweri"},{"id":"420","id2":"64","kecamatan":"Kelurahan\/Desa Pinang Agung"},{"id":"421","id2":"64","kecamatan":"Kelurahan\/Desa Tomage"},{"id":"422","id2":"64","kecamatan":"Kelurahan\/Desa Warimo"},{"id":"423","id2":"64","kecamatan":"Kelurahan\/Desa Warisa Mulya\/Mulia"},{"id":"424","id2":"64","kecamatan":"Kelurahan\/Desa Wonodadi Mulya\/Mulia"}]}
05-29 22:14:48.907 7541-7541/info.androidhive.spinnermysql D/adapter terakhir:: > null
05-29 22:14:48.907 7541-7541/info.androidhive.spinnermysql D/lables2:: > []
05-29 22:14:48.907 7541-7541/info.androidhive.spinnermysql D/lables2 clear:: > []
05-29 22:14:48.907 7541-7541/info.androidhive.spinnermysql D/adapter terakhir:: > android.widget.ArrayAdapter@3e613650
05-29 22:14:48.927 7541-7541/info.androidhive.spinnermysql D/cekspinA:: > 64
05-29 22:14:48.927 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1A:: > 64
05-29 22:14:48.927 7541-7541/info.androidhive.spinnermysql D/kecamatan Q:: > 64
05-29 22:14:48.927 7541-7541/info.androidhive.spinnermysql D/kecamatan Z:: > 64
05-29 22:14:48.937 7541-7541/info.androidhive.spinnermysql D/categorylist:: > [info.androidhive.spinnermysql.Category@28f0463, info.androidhive.spinnermysql.Category@31cd3860, info.androidhive.spinnermysql.Category@35ce7119, info.androidhive.spinnermysql.Category@21b1f5de, info.androidhive.spinnermysql.Category@196383bf, info.androidhive.spinnermysql.Category@63d7a8c, info.androidhive.spinnermysql.Category@37cc05d5, info.androidhive.spinnermysql.Category@36391ea, info.androidhive.spinnermysql.Category@cd69cdb]
05-29 22:14:48.937 7541-7541/info.androidhive.spinnermysql D/cekspin B:: > 64
05-29 22:14:48.937 7541-7541/info.androidhive.spinnermysql D/cekstatuspin1 B:: > 64
Oh, i find a solution, the problem is on onItemSelected
i just add this and problem solved!
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinner1)
{
//do this
}
else if(spinner.getId() == R.id.spinner2)
{
//do this
}