0

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!


The problem is that unless you call ToList, the d.Select(t => new Person()) is re-enumerated each time the Repeat goes through the list, creating duplicate Persons. The technique is known as the deferred execution.

In general, LINQ does not assume that each time it enumerates a sequence it would get the same sequence, or even a sequence of the same length. If this effect is not desirable, you can always "materialize" the sequence inside your Repeat method by calling ToList right away, like this:

public static List<T> Repeat<T>(this IEnumerable<T> lstEnum, int count) {
    if (count < 0)
        throw new ArgumentOutOfRangeException("count");

    var lst = lstEnum.ToList(); // Enumerate only once
    var ret = Enumerable.Empty<T>();

    for (var i = 0; i < count; i++)
        ret = ret.Concat(lst);

    return ret.ToList();
}
4

1 に答える 1

1

編集:

申し訳ありませんが、今あなたが何を考えていたのか理解できたと思います。あなたが望むのは、すべてが getDropDown ビューにあることだと思います。私はこれを正確にテストしていませんが、マーカーを指定する方法に応じて、異なるレイアウトを使用する get DropDownView を試すことができます。キッカーは、このカスタム レイアウトに違いを示さないセレクター xml があることです。

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;       
    }

}

ただし、これらの位置がクリックされた場合、近くにさえない場合のように、スピナーは何もしません。int positionいつ閉じるか閉じないかを伝えるために、配列を受け取るカスタムスピナーを作成する必要がある場合があります。単純に表示するだけでもかなりの負担がかかると言わざるを得ません。その価値がないと思われる場合は、妥協を検討することをお勧めします。

于 2012-11-21T20:34:50.120 に答える