0

So I have spend almost 3 hours searching for an answer on how to set value to my spinner using ArrayAdapter of POJO.

Here is My POJO :

public class ArchiveIssueModel implements Serializable {

private String Issues;
private String id;
private String Picture;

public ArchiveIssueModel(String issues, String id, String picture) {
    Issues = issues;
    this.id = id;
    Picture = picture;
}



public ArchiveIssueModel() {
}

public ArchiveIssueModel(String issue) {
    this.Issues = issue;
}

public String getIssues() {
    return Issues;
}

public void setIssues(String issues) {
    Issues = issues;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getPicture() {
    return Picture;
}

public void setPicture(String picture) {
    Picture = picture;
}

//to display object as a string in spinner
@Override
public String toString() {
    return Issues;
}

@Override
public boolean equals(Object obj) {
    if(obj instanceof ArchiveIssueModel){
        ArchiveIssueModel c = (ArchiveIssueModel )obj;
        if(c.getIssues().equals(Issues) && c.getId()== id ) return true;
    }

    return false;
}

I know how to set value using and ArrayAdapter of String. I have done it like this and that's very easy to understand.

ArrayAdapter<String> spinnerArrayAdapter =
            new ArrayAdapter<>(this,
                    android.R.layout.simple_spinner_item, yearList);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp_year.setAdapter(spinnerArrayAdapter);
    sp_year.setSelection(spinnerArrayAdapter.getPosition(year));

But I can not figure out how to set a value to my spinner in case of ArrayAdapter o ArchiveIssueModel(i.e My POJO). Any help would be appreciated. TY :)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Manish Gupta
  • 129
  • 9

2 Answers2

0

It is same as you have written but simply provides a list of your POJO class to the adapter.

ArrayList<ArchiveIssueModel> list = new ArrayList<>();

ArrayAdapter<ArchiveIssueModel> spinnerArrayAdapter =
        new ArrayAdapter<ArchiveIssueModel>(getActivity(),
                android.R.layout.simple_spinner_item, list);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_year.setAdapter(spinnerArrayAdapter);

// What it means?
sp_year.setSelection(spinnerArrayAdapter.getPosition(year));
Dumbo
  • 1,630
  • 18
  • 33
Nil
  • 312
  • 4
  • 19
  • spinnerArrayAdapter.getPosition(year) this required a new instance of my POJO. 'year' wont work. – Manish Gupta Mar 27 '18 at 05:59
  • @ManishGupta, do you know the use of getPosition() and setSelection()? It has no relation to the new instance. – Nil Mar 27 '18 at 06:06
  • Read more for ArrayAdapter's method getPosition from here: https://developer.android.com/reference/android/widget/ArrayAdapter.html#getPosition(T) Read more about Spinner's method setSelection() from here: https://developer.android.com/reference/android/widget/AbsSpinner.html#setSelection(int) – Nil Mar 27 '18 at 06:14
  • 1
    spinnerArrayAdapter.getPosition(year) is 'year' a String ? then it wont work because I have used ArrayAdapter not ArrayAdapter – Manish Gupta Mar 27 '18 at 06:25
  • exactly. So you need to find externally using a function and provide matched position to setSelection() method. – Nil Mar 27 '18 at 06:30
0

we need to define the adapter to describe the process of converting the Java object to a View (in the getView method). The naive approach to this (without any view caching) looks like the following:

    public class UsersAdapter extends ArrayAdapter<User> {

    // View lookup cache

    private static class ViewHolder {

        TextView name;

        TextView home;

    }



    public UsersAdapter(Context context, ArrayList<User> users) {

       super(context, R.layout.item_user, users);

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

       // Get the data item for this position

       User user = getItem(position);    

       // Check if an existing view is being reused, otherwise inflate the view

       ViewHolder viewHolder; // view lookup cache stored in tag

       if (convertView == null) {

          // If there's no view to re-use, inflate a brand new view for row

          viewHolder = new ViewHolder();

          LayoutInflater inflater = LayoutInflater.from(getContext());

          convertView = inflater.inflate(R.layout.item_user, parent, false);

          viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);

          viewHolder.home = (TextView) convertView.findViewById(R.id.tvHome);

          // Cache the viewHolder object inside the fresh view

          convertView.setTag(viewHolder);

       } else {

           // View is being recycled, retrieve the viewHolder object from tag

           viewHolder = (ViewHolder) convertView.getTag();

       }

       // Populate the data from the data object via the viewHolder object 

       // into the template view.

       viewHolder.name.setText(user.name);

       viewHolder.home.setText(user.hometown);

       // Return the completed view to render on screen

       return convertView;

   }

}

Note: Change User pojo to your pojo class.

tech.mohit.garg
  • 631
  • 8
  • 18