Skip to content

Instantly share code, notes, and snippets.

@Butch78
Created October 15, 2017 09:25
Show Gist options
  • Save Butch78/0fa18c0cf46db3e71315cfb6ddf743f0 to your computer and use it in GitHub Desktop.
Save Butch78/0fa18c0cf46db3e71315cfb6ddf743f0 to your computer and use it in GitHub Desktop.
SimpleCustomList
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal">
<ListView
android:id="@+id/listViewBook"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package com.example.matthew.simplecustomerlist;
/**
* Created by Matthew on 12/10/2017.
*/
public class Book {
private String bookTitle;
private String rating;
public Book(String bookTitle, String rating) {
this.bookTitle = bookTitle;
this.rating = rating;
}
public String getBookTitle() {
return bookTitle;
}
public String getRating() {
return rating;
}
}
package com.example.matthew.simplecustomerlist;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Matthew on 12/10/2017.
*/
public class bookAdapter extends ArrayAdapter<Book> {
public bookAdapter(Context context, ArrayList<Book> books){
super(context, 0, books);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Book book = getItem(position);
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_book, parent, false);
}
TextView textViewBookTitle = (TextView)convertView.findViewById(R.id.TextViewBookTitle);
TextView textViewBookRating = (TextView)convertView.findViewById(R.id.TextViewRating);
ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
textViewBookTitle.setText("Book Title: " + book.getBookTitle());
textViewBookRating.setText("Book Rating: " + book.getRating());
icon.setImageResource(R.drawable.book);
return convertView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="@+id/TextViewBookTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextViewRating"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
package com.example.matthew.simplecustomerlist;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUI();
}
private void initializeUI()
{
ArrayList<Book> bookArrayList = new ArrayList<Book>();
Book book1 = new Book("To kill a bird", "5");
Book book2 = new Book("How to Program", "2");
Book book3 = new Book("Icons How do they work?", "4");
Book book4 = new Book("Little Red Riding Hood", "5");
Book book5 = new Book("Golf Digest", "4");
Book book6 = new Book("A Sticky note guide to life", "3");
Book book7 = new Book("The little book of golf Wisdom", "2");
Book book8 = new Book("Steph's youtube book", "5");
Book book9 = new Book("Home Living", "1");
Book book10 = new Book("Travel", "2");
bookArrayList.add(book1);
bookArrayList.add(book2);
bookArrayList.add(book3);
bookArrayList.add(book4);
bookArrayList.add(book5);
bookArrayList.add(book6);
bookArrayList.add(book7);
bookArrayList.add(book8);
bookArrayList.add(book9);
bookArrayList.add(book10);
bookAdapter adapter = new bookAdapter(getApplicationContext(), bookArrayList);
ListView bookListView = (ListView) findViewById(R.id.listViewBook);
bookListView.setAdapter(adapter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment