Skip to content

Commit

Permalink
update spinner data, get spinner selected position
Browse files Browse the repository at this point in the history
  • Loading branch information
c0deslinger committed Jan 12, 2020
1 parent af90e7e commit 11e8f7c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.setText("Sumbit").create();
button.getView();

ArrayList<SpinnerData> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpinnerData> dataList){
this.dataList = dataList;
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -354,7 +360,8 @@ public void updateListDropdown(ArrayList<SpinnerData> 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);
}
}
}

0 comments on commit 11e8f7c

Please sign in to comment.