简体   繁体   中英

ArrayList<Object> adapter to Spinner

I've tried several ways to do this, I followed this -> Android Spinner databind using array list answer, but my object has an ArrayList inside. The whole thing is an ArrayList where Object has a list on his own.

How can I show this information properly? Another field in this class is something I don't want to be selectable, just the arraylist inside. The class definition will make everything clear:

public class Horario{
    private String fecha;
    private ArrayList<String> hora;

    public Horario(String _fecha, ArrayList<String> _hora){
        fecha=_fecha;
        hora = _hora;
    }

    public Horario(){}

    public void setFecha(String _fecha){
        fecha = _fecha;
    }
    public void setHora(ArrayList<String> _hora){
        hora=_hora;
    }

    public String getFecha(){
        return fecha;
    }
    public ArrayList<String> getHora(){
        return hora;
    }
}

This is a class for a schedule, the date (fecha) is what I dont want to be selectable, the hour (hora) is. I imagine this as a dropdown menu with categories (the date) and a group of clickable items (hours).

Date1
    hour 1
    hour 2
    hour 3
Date2
    hour 1

If this is difficult/impossible then I'm thinking of just listing the date + hour in each option of spinner.

Date1 - hour 1
Date1 - hour 2
Date1 - hour 3 ....

How can I achieve this? How can I personalize an adapter to get to this? Thanks in advance!

EDIT:

I apologize, i think i understand what you had in mind now. i think what you want is for everything to be in the getDropDown view. I haven't exactly tested this but you could try for a get DropDownView that uses a different layout depending on how you wanna designate a marker for it. The kicker is that this custom layout has a selector xml that shows no difference.

public class HorarioArrayAdapter extends ArrayAdapter {

    private Horario horario;

    public HorarioArrayAdapter(Context context, int textViewResourceId, Horario horario) {
        super(context, textViewResourceId);
        this.horario = horario;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO whatever you had in mind
        return super.getView(position, convertView, parent);
    }


    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View v;
        if (// marker at this position says something) {
            LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = viewInflater.inflate(R.layout.custom_spinner_row, parent, false);   

            TextView text = (TextView) v.findViewById(R.id.txt_date);
            text.setText(horario.getHora().get(position));          
        } else {
            View v = super.getDropDownView(position, convertView, parent);
            TextView text = (TextView) v.findViewById(android.R.id.text1);
            text.setText(horario.getHora().get(position));              
        }
        return v;       
    }

}

The rub though would be making the spinner do nothing on the if those positions were clicked, as in not even close. You might need make a custom spinner for that takes in a int position array to tell it when to close or not close. i gotta say that it seems really involved for simply doing a display, and you might wanna consider compromises on this if you don't think it's worth it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM