César Pereira
César Pereira

Reputation: 249

ArrayList in an adapter?

So guys, i'll try to explain a bit of this part of my project i wanted to do so you guys can better understand my problem. I'm trying no develop an app to a bus station, to show the schedule. And to show it, i was using a ListView, a costum one. And it works if i only pass Strings to the adapter. But in order to do only in an Array of Strings would take a lot of memory cause i would have to build a lot of arrays. Because, try to imagine, i have the schedule of the bus leaving a certain place, but then, depending in what destination the user selects i have to exclude some of items from the array because not all bus's end at the same place, some won't go to the destination selected, so i have to take those out. So if i was going to do that in just multiple arrays e would have to build dozens of array to every single possibilitie of departure and destination. So i thought in doing it with an arraylist, so would be easier to remove the items i don't want. Now the problem is that i can't send to the adapter of the costum listview and arraylist, like this:

ArrayList<String> horarios= new ArrayList<String>();
ListAdapter horarioAdapter = new costum_adapter(this, horarios);
ListView horarioListView = (ListView) findViewById(R.id.horario_listView);
horarioListView.setAdapter(horarioAdapter);

It gives me error in the second line, because i can't put "horarios" in adapter because its an arraylist. How can i solve this ? Hope you guys understood what i was trying to do. Sorry for the long description.

Here is my costum_adapter, doesn't have much, only 2 textView, i was just trying this to work, and then add a few things.

class costum_adapter extends ArrayAdapter<String>{

public costum_adapter(Context context, String[] horarios) {
    super(context, R.layout.costum_listview ,horarios);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater horarioInflater = LayoutInflater.from(getContext());
    View costumView = horarioInflater.inflate(R.layout.costum_listview, parent, false);

    String singleHorario = getItem(position);
    TextView hora = (TextView) costumView.findViewById(R.id.Hora);
    TextView nota = (TextView) costumView.findViewById(R.id.Nota);

    hora.setText(singleHorario);
    nota.setText(" ");
    return costumView;
}

}

Upvotes: 0

Views: 269

Answers (1)

Eli Blokh
Eli Blokh

Reputation: 12293

Fix arguments in adapter constructor

public costum_adapter(Context context, ArrayList<String> horarios) {
    super(context, R.layout.costum_listview ,horarios);
}

Upvotes: 1

Related Questions