skip to Main Content

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


  1. Hie i would suggest you use RecyclerView to achieve your list view

    if 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 this

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/locations_list"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    And then create a item file that will represent item in a list item

    for example lets call it location_list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            >
    
        <TextView
            android:id="@+id/location_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
      <TextView
            android:id="@+id/max_capacity"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
      <TextView
            android:id="@+id/current_capacity"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
       
    </LinearLayout>
    

    Then after that you would need to implement an Adapter lets call it LocationAdapter.java

        public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.ViewHolder> {
            ArrayList<Location> locations;//list of locations
            public LoacationAdapter(ArrayList<Location> locations) {
                this.locations = locations //initialising list in constructor
            }
        
            @NonNull
            @Override
            public Locations.Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //here this method inflates the row view
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.location_list_item, parent, false);
                return new ViewHolder(view);
            }
        
            @Override
            public void onBindViewHolder(@NonNull LocationAdapter.ViewHolder holder, int position) {  
    //this method sets values in the views  
        holder.locationName.settext(locations.get(position).getLocationName())
        holder.maxCapacity.settext(locations.get(position).getMaxCapacity())
     holder.currentCapacity.settext(locations.get(position).getCurrentCapacity())
            }
        
            @Override
            public int getItemCount() {
                return locatioins.size();// number of locations
            }
        
    //this class will repressent the list item
            public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    // this are the views in the list item
                TextView locationName, maxCapacity, currrentCapacity;
                public ViewHolder(@NonNull View itemView) {
                    super(itemView);
                    locationName = itemView.findViewById(R.id.location_name);
                    maxCapacity= itemView.findViewById(R.id.max_capacity);
                    currentCapacity= itemView.findViewById(R.id.current_capacity);
                    itemView.setOnClickListener(this::onClick);
        
                }
        
                @Override
                public void onClick(View v) {
                   //do what ever you want when an item is clicked
                }
            }
        }
    

    In in your activity file then

    ArrayList<Location> locationsArraylist = new ArrayLists<> ();
    //initializing recylcer view 
    RecyclerView  locationList =  findViewById(R.id.locations_list);
    // setting layout manager
    locationsList.setLayoutManager(new LinearLayoutManager(getContext, RecyclerView.Vertical, false));
    //setting adapter  adapter with the list of locations you want to display
    locationList.setAdapter(new LocationsAdapter(locationsArraylist));
    
    Login or Signup to reply.
  2. You have to use RecyclerView

    //Declare some items in your ArrayList
    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);
    
    //make your own adapter class which extend RecyclerView.Adapter
    MyAdapter myAdapter = new MyAdapter(locationsArrayList,this);
     
    //setup your RecylerView
    recyclerView.setAdapter(itemsListAdapter);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    

    Now in your Adapter class you can change each item color or font… based on ArrayList attribute.

    public class MyAdapter extends RecyclerView.Adapter{
    
        private ArrayList<Location> locationsArrayList;
        private Context context;
    
        public MyAdapter(ArrayList<Location> locationsArrayList, Context context) {
            this.locationsArrayList= locationsArrayList;
            this.context = context;
        }
    
        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }
    
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.item_layout,parent,false));
        }
    
    
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder viewHolder = (ViewHolder) holder;
    //here you can set your own conditions based on your arraylist using position parameter
            viewHolder.itemNameTextView.setText(locationsArrayList.get(position).getName());
        }
    
        @Override
        public int getItemCount() {
            return locationsArrayList.size();
        }
    
        public Context getContext() {
            return context;
        }
    
        class ViewHolder extends RecyclerView.ViewHolder {
            //setup item layout attributes
            TextView itemNameTextView;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                itemNameTextView = itemView.findViewById(R.id.itemNameTextView);
            }
        }
    }
    

    I know it’s complicated but RecyclerView make your list more flexible.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search