arcadefire / nice-spinner

A nice spinner for Android
Apache License 2.0
2.84k stars 446 forks source link

Set Custom Adapter #149

Closed edgarscr closed 4 years ago

edgarscr commented 4 years ago

Hello how are you? i need help to add custom adapter because appear like this image

pzienowicz commented 4 years ago

Just implement toString function in your model and should work.

edgarscr commented 4 years ago

Hello my adapter its like that i dont understand how i can add like string?

public class ReasonAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private List<Reason> listData;
    private Context context;
    public ReasonAdapter(Context context, List<Reason> listData) {
        this.context = context;
        layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listData = listData;
    }
    @Override
    public int getCount() {
        return listData.size();
    }
    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder spinnerHolder;
        if(convertView == null){
            spinnerHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_reason, parent, false);
            spinnerHolder.spinnerItemList = convertView.findViewById(R.id.reason_list_item);
            convertView.setTag(spinnerHolder);
        }else{
            spinnerHolder = (ViewHolder)convertView.getTag();
        }
        spinnerHolder.spinnerItemList.setText(listData.get(position).getTitle());
        return convertView;
    }
    class ViewHolder{
        TextView spinnerItemList;
    }
}

reason class

public class Reason {
    private int id;
    private String title;
    private String value;
    private int price;
    private int sort;
   //@SerializedName("name")
    public int getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    public String getValue() {
        return this.value;
    }

    public int getPrice() {
        return this.price;
    }

    public int getSort() {
        return this.sort;
    }

    // Constructor to convert JSON object into a Java class instance
    public Reason(JSONObject object){
        try {
            this.id = object.getInt("id");
            this.title = object.getString("title");
            this.value = object.getString("value");
            this.price = object.getInt("price");
            this.sort = object.getInt("sort");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // Factory method to convert an array of JSON objects into a list of objects
    // User.fromJson(jsonArray);
    public static ArrayList<Reason> fromJson(JSONArray jsonObjects) {
        ArrayList<Reason> reasons = new ArrayList<Reason>();
        for (int i = 0; i < jsonObjects.length(); i++) {
            try {
                reasons.add(new Reason(jsonObjects.getJSONObject(i)));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return reasons;
    }
}
pzienowicz commented 4 years ago

toString should be implemented in Reason class - and should return the string, you want to display on spinner list.