JavascriptProva

sabato 5 novembre 2016

Ripasso di CustomAdapter che estende ArrayAdapter

ArrayAdapter ha diversi tipi di costruttori. Mi sono rinfrescate le idee su cose viste, peraltro, anche abbastanza recentemente, ma che scordo con estrema facilità.
Ecco qui l'elenco delle firme dei costruttori:
Context, int
Context, int, int
Context, int, T[]
Context, int, int, T[]
Context, int, List
Context, int, int, List
In particolare, quello che ho usato prima è un costruttore a quattro parametri, precisamente l'ultimo in quanto ho usato una classe che eredita da List.
A quattro parametri posso usare anche il terzultimo, che invece di una classe di tipo List usa un semplice Array.
Poi si può usare a tre parametri, senza selezionare la view del layout.
La creazione di un CustomView estende ArrayAdapter e ne usa il costruttore a tre parametri context, layout e array, ossia il penultimo, Context, int, List
Provo a estendermi ArrayAdapter per crearmi un adapter personalizzato. Mi creo un tipo con due membri:
    class Tipo{
        public String testoText;
        public String testoButton;
    }
Di questo tipo creerò diverse istanze, che collocherò in un'arrayList.

Ora provo a estendere ArrayAdapter:
    class CustomAdapter extends ArrayAdapter{

        Context context;
        int resource;
        ArrayList<Tipo> arrayList;
        public CustomAdapter(Context context, int resource, ArrayList<Tipo> objects) {
            super(context, resource, objects);
            this.context=context;
            this.resource=resource;
            this.arrayList=(ArrayList)objects;
        }

        @Override
        public View getView(int position,View convertView,ViewGroup group){
            LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView=layoutInflater.inflate(this.resource,null);
            TextView textView=(TextView)convertView.findViewById(R.id.textView);
            Button button=(Button)convertView.findViewById(R.id.button2);
            textView.setText(arrayList.get(position).testoText);
            button.setText(arrayList.get(position).testoButton);
            return convertView;
        }
    } 
Creo diverse istanze di Tipo, le metto in un arrayList e do l'arrayList in pasto a una istanza di CustomAdapter.
        Tipo tipo=new Tipo();
        tipo.testoButton="Bottone";
        tipo.testoText="Testo";
        
        Tipo tipo1=new Tipo();
        tipo1.testoButton="Bottone secondo";
        tipo1.testoText="Testo secondo";
        
        Tipo tipo2=new Tipo();
        tipo2.testoButton="Altro bottone";
        tipo2.testoText="Altro testo";

        arrayList=new ArrayList<Tipo>();
        arrayList.add(tipo);
        arrayList.add(tipo1);
        arrayList.add(tipo2);

        customAdapter=new CustomAdapter(this,R.layout.row,arrayList);

        listView=(ListView) findViewById(R.id.listView);
        listView.setAdapter(customAdapter); 
Proviamo...



Sì, eccolo.

Nessun commento:

Posta un commento