Im trying to create a bunch of rows in a Listview to show from an ArrayList of object class
my object class Locations has
private String name;
private int currentCapacity;
private int maxCapacity;
private int id;
public Locations(String name, int currentCapacity, int maxCapacity, int id){
this.currentCapacity = currentCapacity;
this.id = id;
this.maxCapacity = maxCapacity;
this.name = name;
}
Locations loc1 = new Locations("foodplace", 10, 100, 1);
Locations loc2 = new Locations("area", 15, 25, 2);
Locations loc3 = new Locations("otherplace", 25, 25, 3);
locationsArrayList.add(loc1);
locationsArrayList.add(loc2);
locationsArrayList.add(loc3);
ListView list = (ListView) findViewById(R.id.theList);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, locationsArrayList);
list.setAdapter(adapter);
how do i input my ArrayList such that i can change the color of the row based on the currentCapacity and show the name on the block?
2
Answers
Hie i would suggest you use
RecyclerView
to achieve your list viewif you are using gradle for your dependencies you need to implement it in your app level gradle file
In your
activity.xml
file you implement like thisAnd then create a item file that will represent item in a list item
for example lets call it
location_list_item.xml
Then after that you would need to implement an Adapter lets call it
LocationAdapter.java
In in your activity file then
You have to use RecyclerView
Now in your Adapter class you can change each item color or font… based on ArrayList attribute.
I know it’s complicated but RecyclerView make your list more flexible.