From aa42151f2578598a3bde8cf94c88eeea62f5c30d Mon Sep 17 00:00:00 2001 From: Angelo Marchesin Date: Fri, 24 May 2019 16:27:43 +0200 Subject: [PATCH] Change SpinnerTextFormatter to accept generic parameters --- .../java/org/angmarc/app/MainActivity.java | 18 +++++++++--------- .../java/org/angmarch/views/NiceSpinner.java | 12 ++++++------ .../views/SimpleSpinnerTextFormatter.java | 5 ----- .../angmarch/views/SpinnerTextFormatter.java | 5 ++--- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/angmarc/app/MainActivity.java b/app/src/main/java/org/angmarc/app/MainActivity.java index b62c729..c0d3c20 100644 --- a/app/src/main/java/org/angmarc/app/MainActivity.java +++ b/app/src/main/java/org/angmarc/app/MainActivity.java @@ -11,6 +11,7 @@ import org.angmarch.views.NiceSpinner; import org.angmarch.views.OnSpinnerItemSelectedListener; import org.angmarch.views.SimpleSpinnerTextFormatter; +import org.angmarch.views.SpinnerTextFormatter; import java.util.ArrayList; import java.util.Arrays; @@ -41,16 +42,15 @@ public void onItemSelected(NiceSpinner parent, View view, int position, long id) private void setupTintedWithCustomClass() { final NiceSpinner spinner = findViewById(R.id.tinted_nice_spinner); - List persons = new ArrayList<>(); + List people = new ArrayList<>(); - persons.add(new Person("Tony", "Stark")); - persons.add(new Person("Steve", "Rogers")); - persons.add(new Person("Bruce", "Banner")); + people.add(new Person("Tony", "Stark")); + people.add(new Person("Steve", "Rogers")); + people.add(new Person("Bruce", "Banner")); - SimpleSpinnerTextFormatter textFormatter = new SimpleSpinnerTextFormatter() { + SpinnerTextFormatter textFormatter = new SpinnerTextFormatter() { @Override - public Spannable format(Object item) { - Person person = (Person) item; + public Spannable format(Person person) { return new SpannableString(person.getName() + " " + person.getSurname()); } }; @@ -60,11 +60,11 @@ public Spannable format(Object item) { spinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() { @Override public void onItemSelected(NiceSpinner parent, View view, int position, long id) { - Person person = (Person) spinner.getSelectedItem(); //parent.getItemAtPosition(position).toString(); + Person person = (Person) spinner.getSelectedItem(); Toast.makeText(MainActivity.this, "Selected: " + person.toString(), Toast.LENGTH_SHORT).show(); } }); - spinner.attachDataSource(persons); + spinner.attachDataSource(people); } private void setupDefault() { diff --git a/library/src/main/java/org/angmarch/views/NiceSpinner.java b/library/src/main/java/org/angmarch/views/NiceSpinner.java index d3d94ac..c56bfdb 100644 --- a/library/src/main/java/org/angmarch/views/NiceSpinner.java +++ b/library/src/main/java/org/angmarch/views/NiceSpinner.java @@ -187,7 +187,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) adapter.setSelectedIndex(position); - setTextInternal(selectedTextFormatter.format(adapter.getItemInDataset(position)).toString()); + setTextInternal(adapter.getItemInDataset(position)); dismissDropDown(); } @@ -323,11 +323,11 @@ public void setArrowDrawable(Drawable drawable) { setArrowDrawableOrHide(arrowDrawable); } - public void setTextInternal(String text) { + private void setTextInternal(Object item) { if (selectedTextFormatter != null) { - setText(selectedTextFormatter.format(text)); + setText(selectedTextFormatter.format(item)); } else { - setText(text); + setText(item.toString()); } } @@ -383,12 +383,12 @@ public PopUpTextAlignment getPopUpTextAlignment() { return horizontalAlignment; } - private void setAdapterInternal(NiceSpinnerBaseAdapter adapter) { + private void setAdapterInternal(NiceSpinnerBaseAdapter adapter) { if (adapter.getCount() > 0) { // If the adapter needs to be set again, ensure to reset the selected index as well selectedIndex = 0; listView.setAdapter(adapter); - setTextInternal(selectedTextFormatter.format(adapter.getItemInDataset(selectedIndex)).toString()); + setTextInternal(adapter.getItemInDataset(selectedIndex)); } } diff --git a/library/src/main/java/org/angmarch/views/SimpleSpinnerTextFormatter.java b/library/src/main/java/org/angmarch/views/SimpleSpinnerTextFormatter.java index 608b588..ed5fca1 100644 --- a/library/src/main/java/org/angmarch/views/SimpleSpinnerTextFormatter.java +++ b/library/src/main/java/org/angmarch/views/SimpleSpinnerTextFormatter.java @@ -5,11 +5,6 @@ public class SimpleSpinnerTextFormatter implements SpinnerTextFormatter { - @Override - public final Spannable format(String text) { - return new SpannableString(text); - } - @Override public Spannable format(Object item) { return new SpannableString(item.toString()); diff --git a/library/src/main/java/org/angmarch/views/SpinnerTextFormatter.java b/library/src/main/java/org/angmarch/views/SpinnerTextFormatter.java index ba01226..6b985a7 100644 --- a/library/src/main/java/org/angmarch/views/SpinnerTextFormatter.java +++ b/library/src/main/java/org/angmarch/views/SpinnerTextFormatter.java @@ -2,8 +2,7 @@ import android.text.Spannable; -public interface SpinnerTextFormatter { - Spannable format(String text); +public interface SpinnerTextFormatter { - Spannable format(Object item); + Spannable format(T item); }