From 11e8f7c6a23469ffa83e3a0563822fd30b9b28a4 Mon Sep 17 00:00:00 2001 From: Ahmed Yusuf Date: Sun, 12 Jan 2020 20:19:22 +0700 Subject: [PATCH] update spinner data, get spinner selected position --- .../androidformbuilder/ExampleJavaActivity.java | 5 +++++ .../paperplay/myformbuilder/adapter/SpinnerAdapter.java | 5 +++++ .../java/com/paperplay/myformbuilder/view/MySpinner.java | 9 ++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/paperplay/androidformbuilder/ExampleJavaActivity.java b/app/src/main/java/com/paperplay/androidformbuilder/ExampleJavaActivity.java index 9f826b0..ad7f2dc 100644 --- a/app/src/main/java/com/paperplay/androidformbuilder/ExampleJavaActivity.java +++ b/app/src/main/java/com/paperplay/androidformbuilder/ExampleJavaActivity.java @@ -138,6 +138,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { .setText("Sumbit").create(); button.getView(); + ArrayList cityList2 = new ArrayList<>(); + cityList2.add(new SpinnerData(1, "1", "Zimbabwe")); + cityList2.add(new SpinnerData(2, "2", "Malang", true)); + cityList2.add(new SpinnerData(3, "3", "Surabaya")); + spinCity.updateListDropdown(cityList2); }catch (CloneNotSupportedException e){ e.printStackTrace(); diff --git a/myformbuilder/src/main/java/com/paperplay/myformbuilder/adapter/SpinnerAdapter.java b/myformbuilder/src/main/java/com/paperplay/myformbuilder/adapter/SpinnerAdapter.java index fa775d6..b922c2e 100644 --- a/myformbuilder/src/main/java/com/paperplay/myformbuilder/adapter/SpinnerAdapter.java +++ b/myformbuilder/src/main/java/com/paperplay/myformbuilder/adapter/SpinnerAdapter.java @@ -49,4 +49,9 @@ public View getDropDownView(int position, @Nullable View convertView, @NonNull V txtItem.setText(dataList.get(position).getValue()); return txtItem; } + + public void updateListDropdown(ArrayList dataList){ + this.dataList = dataList; + notifyDataSetChanged(); + } } diff --git a/myformbuilder/src/main/java/com/paperplay/myformbuilder/view/MySpinner.java b/myformbuilder/src/main/java/com/paperplay/myformbuilder/view/MySpinner.java index 303b67a..aa48dae 100644 --- a/myformbuilder/src/main/java/com/paperplay/myformbuilder/view/MySpinner.java +++ b/myformbuilder/src/main/java/com/paperplay/myformbuilder/view/MySpinner.java @@ -172,6 +172,7 @@ public MySpinner(Builder builder){ public void onItemSelected(AdapterView adapterView, View view, int i, long l) { if(onSelectedListener !=null) { onSelectedListener.onSelectedData(item.get(i)); + onSelectedListener.onSelectedPosition(i); } } @@ -194,6 +195,7 @@ public void onNothingSelected(AdapterView adapterView) { public interface OnSelectedListener { void onSelectedData(SpinnerData spinnerData); + void onSelectedPosition(int position); } public void setSpinnerOnSelectedListener(OnSelectedListener onSelectedListener) { @@ -221,6 +223,10 @@ public void setValue(String value){ } } + public void setPosition(int position){ + spinnerAnswer.setSelection(position); + } + public void setId(int id){ if(id != -1) { SpinnerData spinnerData = null; @@ -354,7 +360,8 @@ public void updateListDropdown(ArrayList data) { for (SpinnerData items : data) { //show all but hidden item if (!items.isHidden()) this.itemDropDown.add(items); } - spinnerAdapter.notifyDataSetChanged(); + spinnerAdapter.updateListDropdown(this.itemDropDown); + spinnerAnswer.setSelection(0); } } }