Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Change SpinnerTextFormatter to accept generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo Marchesin committed May 24, 2019
1 parent 4001bd5 commit aa42151
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
18 changes: 9 additions & 9 deletions app/src/main/java/org/angmarc/app/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Person> persons = new ArrayList<>();
List<Person> 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<Person>() {
@Override
public Spannable format(Object item) {
Person person = (Person) item;
public Spannable format(Person person) {
return new SpannableString(person.getName() + " " + person.getSurname());
}
};
Expand All @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/org/angmarch/views/NiceSpinner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -383,12 +383,12 @@ public PopUpTextAlignment getPopUpTextAlignment() {
return horizontalAlignment;
}

private void setAdapterInternal(NiceSpinnerBaseAdapter adapter) {
private <T> void setAdapterInternal(NiceSpinnerBaseAdapter<T> 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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import android.text.Spannable;

public interface SpinnerTextFormatter {
Spannable format(String text);
public interface SpinnerTextFormatter<T> {

Spannable format(Object item);
Spannable format(T item);
}

0 comments on commit aa42151

Please sign in to comment.