Search code examples
javaandroidlistviewscrollview

How to manage a list of objects?


I'd like to make an Android (I'm a beginner) application in which I can manage a list of objects (for example Animals). I need to be able to click on each animal of the list to remove it or to edit its attributes, the problem is that I have no idea how to do that.

Here is my Animal class (animal.java) as you can see it's really basic:

public class Animal {
    private String name;
    private int age;
    private float size;
    private double weight;

    public Animal(String name, int age, float size, double weight) {
        this.name = name;
        this.age = age;
        this.size = size;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getSize() {
        return size;
    }

    public void setSize(float size) {
        this.size = size;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
}

Here is my MainActivity class (MainActivity.java) :

package fr.lap.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = findViewById(R.id.lv);

        String[] animals = new String[15];
        for (int i = 0;i < 15; i++)
            animals[i] = "animal " + i;

        final List<String> tests_list = new ArrayList<String>(Arrays.asList(animals));

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tests_list);

        lv.setAdapter(arrayAdapter);
        arrayAdapter.notifyDataSetChanged();
    }
}

And here is the activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

The application result

So as you can see, I have a list of String with the animal "names". I found this code on the Internet but I don't really understand how everything works so I can't adapt it to make the items clickable, ...

What I would like to know is :

  • in my case, is the ListView the better solution or using a ScrollView would be a better idea ?
  • how make the items in the list clickable (to open a new Activity and be able to access a "control panel" of the selected item)
  • how to add and remove the items of the list
  • I guess I can't put the Animals in the list, so should I have two lists: one with Animals and one with their name ?

Thanks a lot !


Solution

  • use these, it is a recyclerView and it's adapter.

    main xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
    

    cell xml:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp">
    
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />
    
    </RelativeLayout>
    

    the Adapter class :

    public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalViewHolder> {
    
    
        public class AnimalViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener {
    
            TextView name;
    
    
    
            NewsItemViewHolder(View itemView) {
                super(itemView);
    
                name = (TextView) itemView.findViewById(R.id.name);
    
                itemView.setOnClickListener(this);
    
            }
            @Override
            public void onClick(View v) {
                ///do what you want when clicking on your animal object
            }
    
        }
    
    
        public List<Animal> animalList = new ArrayList<>();
    
        public AnimalAdapter(List<Animal> animalList){
            this.animalList = animalList;
    
    
        }
    
        @Override
        public int getItemCount() {
            return newsList.size();
        }
    
        @Override
        public AnimalViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View itemView;
            itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_animal, viewGroup, false);
            return new AnimalViewHolder(itemView);
        }
    
        @Override
        public void onBindViewHolder(AnimalViewHolder holder, int i) {
    
            holder.name.setText(animalList.get(i).getName());
    
        }
    
    
    
        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }
    
    }
    

    your activity:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            RecyclerView lv = findViewById(R.id.lv);
    
            List<Animal> animals = new ArrayList<>();
            for (int i = 0;i < 15; i++){
                ///addd your animals like:
                Animal rabbit = new Animal("rabbit",2,1,15);
                animals.add(rabbit)
              }
    
    
            lv.setLayoutManager(new LinearLayoutManager(this));
    
            lv.setAdapter(new AnimalAdapter(animals));
    
        }