dzstr
dzstr

Reputation: 73

Listview from ArrayList class

I'm trying to figure out how to display the items of an ArrayList which I have stored in a separate class into a ListView. I've been looking it up all day but the problem is that I can't find something like this specifically in which I had created a different class where I stored the ArrayList. It's a task I got for an internship and they asked me to use a local database or hardcode it so I chose to hardcode it.

import com.mircea.bookapp.Book;
import java.util.ArrayList;

public class Books
{
    private ArrayList<Book> booklist;

    public Books()
    {
        booklist = new ArrayList<Book>();

        booklist.add(new Book("In Search of Lost Time", "Marcel Proust", 1913, "Modernist"));
        booklist.add(new Book("Ulysses", "James Joyce", 1922, "Modernist novel"));
        booklist.add(new Book("Don Quixote", "Miguel de Cervantes", 1612, "Novel"));
        booklist.add(new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925, "Tragedy"));
        booklist.add(new Book("One Hundred Years Of Solitude", "Gabriel Garcia Marquez", 1967, "Magic realism"));
    }

    public Book getBook(int index)
    {
        return this.booklist.get(index);
    }
}

This is the code from the Class from which I wanna get the info for the ListView.

Upvotes: 2

Views: 80

Answers (2)

iknow
iknow

Reputation: 9892

In your MainActivity onCreate method:

ArrayList<String> books = new ArrayList<>();
//here You have to load all books from Books class as a string to this array
ListView list = (ListView) findViewById(R.id.listView);
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item_layout, books);
list.setAdapter(adapter);

To resources/layouts add this file. (here You can modify row style in ListView)

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:textSize="20sp"
          android:textColor="@color/colorPrimaryDark"
          android:textAlignment="center"
          android:gravity="center">
</TextView>

You can also use a custom style, e.g.

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, books);

Upvotes: 2

dlam
dlam

Reputation: 3905

The pattern / UI widget you're looking for is called RecyclerView. This is the standard way to show a list of items as it has some nice performance optimizations to re-use inflated layouts.

d.android.com has a nice guide on how to achieve what you want here: https://developer.android.com/guide/topics/ui/layout/recyclerview

The key thing to implement is the adapter methods, onBindViewHolder and getItemCount which will call into your Books class to get the total number of Books and access each Book to bind the view to. Modified from this example: https://developer.android.com/guide/topics/ui/layout/recyclerview#Adapter

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private Books books;

    public MyAdapter(Books books) {
        this.books = books;
    }

    @Override
    public MyViewHolder onCreateViewHolder(
        ViewGroup parent,
        int viewType
    ) {
        return new MyViewHolder(parent); // you need to implement this class!
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.bind(books.getBook(position));

    }

    @Override
    public int getItemCount() {
        return books.getBookCount();
    }
}

Upvotes: 1

Related Questions