Creating a Custom ListView

Similar documents
ListView Containers. Resources. Creating a ListView

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

ListView (link) An ordered collection of selectable choices. key attributes in XML:

Practical 1.ListView example

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Developing Android Applications

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are:

Adapter.

Mobile Computing Practice # 2a Android Applications - Interface

Introductory Mobile App Development

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Upon completion of the second part of the lab the students will have:

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

INTRODUCTION COS MOBILE DEVELOPMENT WHAT IS ANDROID CORE OS. 6-Android Basics.key - February 21, Linux-based.

Chapter 7: Reveal! Displaying Pictures in a Gallery

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { }

Intents. Your first app assignment

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University

Graphical User Interfaces

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

10.1 Introduction. Higher Level Processing. Word Recogniton Model. Text Output. Voice Signals. Spoken Words. Syntax, Semantics, Pragmatics

Mobile Programming Lecture 3. Resources, Selection, Activities, Intents

GUI Widget. Lecture6

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Mobile Computing Fragments

B6: Time to coding. Đi tới src\at.exam tạo một class mới là CustomViewGroup với nội dung sau: Mã: package at.exam;

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

05. RecyclerView and Styles

South Africa

Android Basics. Android UI Architecture. Android UI 1

Mobile Computing Practice # 2c Android Applications - Interface

Programming with Android: Introduction. Layouts. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Topics of Discussion

List-Based Widgets: Lists, Grids, and Scroll Views

The Suggest Example layout (cont ed)

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps.

Chapter 8 Positioning with Layouts

DEPARTMENT OF COMPUTER SCIENCE THE UNIVERSITY OF HONG KONG. CSIS0801 Final Year Project. WhozzUp - A Novel Big Data App for HK (Individual Report)

Our First Android Application

More Effective Layouts

CS 4330/5390: Mobile Application Development Exam 1

android:layout_margintop="5dp" > <EditText android:layout_width="210dp"

Lecture 14. Android Application Development

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Fragments. Lecture 11

Fragment Example Create the following files and test the application on emulator or device.

OPTIMIZING ANDROID UI PRO TIPS FOR CREATING SMOOTH AND RESPONSIVE APPS

Arrays of Buttons. Inside Android

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java.

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Overview. Lecture: Implicit Calling via Share Implicit Receiving via Share Launch Telephone Launch Settings Homework

Android User Interface Android Smartphone Programming. Outline University of Freiburg

The World of List View. Romain Guy and Adam Powell May 19, 2010

Accelerating Information Technology Innovation

Introduction. Who Should Read This Book. Key Topics That This Book Covers

Android Application Development

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Fragments. Lecture 10

Android Layout Types

Intents, Intent Filters, and Invoking Activities: Part I: Using Class Name

Mobile User Interfaces

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Android User Interface

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

Building User Interface for Android Mobile Applications II

Android List-Based Selection Widgets

1. Simple List. 1.1 Simple List using simple_list_item_1

LECTURE 08 UI AND EVENT HANDLING

Android UI: Overview

Android Data Binding: This is the DSL you're looking for

Android Programs Day 5

Mobile Application Development

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Android UI Development

Stanislav Rost CSAIL, MIT

Mobila applikationer och trådlösa nät, HI1033, HT2012

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

StoppUhr. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start1"

Programming with Android: Layouts, Widgets and Events. Dipartimento di Scienze dell Informazione Università di Bologna

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Developed and taught by well-known Contact author and developer. At public for details venues or onsite at your location.

ANDROID USER INTERFACE

Embedded Systems Programming - PA8001

Solving an Android Threading Problem

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Bluetooth Low Energy in Android Java

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.

1 Radio Button Radio Group, CheckBox, Spinner

Chapter 5 Flashing Neon FrameLayout

Transcription:

Creating a Custom ListView References https://developer.android.com/guide/topics/ui/declaring-layout.html#adapterviews Overview The ListView in the previous tutorial creates a TextView object for each string in an array of strings. More often than not, we ll want to display more than just a string in each View object of the ListView. Moreover, we ll often have an ArrayList that holds references to objects of some custom class that we ve written. What we ll need is for the ListView to display, not a TextView, but a custom View for each object in the ArrayList. In order to make this happen we need to do a few things. 1. Create a custom View layout that specifies how to display the information in our custom class. 2. Create a custom ArrayAdapter that creates an instance of the custom View object for each object in the ArrayList 3. Attach the custom ArrayAdapter to the ListView. Creating a Custom View Layout Suppose we have a class named Message like the one below. public class Message {

private String sender; private String subject; private String text;... /* Constructors, getters, setter, etc. */ Suppose also that for each Message object, we want to display the sender and the subject in the ListView. Then the layout file, named message_view.xml, might look like the one below. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@id/messagesendertextview" android:layout_width="wrap_content"

android:layout_height="wrap_content"/> <TextView android:id="@id/messagesubjecttextview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> This layout file contains a horizontally oriented LinearLayout that contains two TextView objects, one to hold the message s sender and the other to hold the message s subject. Creating a Custom ArrayAdapter We re going to build a custom adapter that reads Message objects from an ArrayList<Message> object. For each Message in the ArrayList, it will populate the ListView with a View based on the custom layout we defined above. Creating a custom array adapter is easy. We just need to: 1. Create a new class that extends ArrayAdapter<Message> 2. Provide in it a constructor with a specific signature 3. Include an implementation of the getview() method The code for a file named MessageListAdapter.java is given below. The MessageListAdapter constructor takes a context, layout resource, and a reference to the ArrayList as arguments. The getview() method creates a new View object based on the custom layout we created earlier.

public class MessageListAdapter extends ArrayAdapter<Message> { private Context context; private int resource; private ArrayList<Message> messages; // local reference public MessageListAdapter(Context ctx, int res, ArrayList<Message> mssgs) { super(ctx, res, mssgs); this.context = ctx; this.resource = res; this.messages = mssgs; @Override public View getview(int position, View convertview, ViewGroup parent) { View view = convertview; if (view == null) { /* Create a new View object */ LayoutInflater li = (LayoutInflater) context.getsystemservice( Context.LAYOUT_INFLATER_SERVICE);

view = li.inflate(r.layout.message_view, null); /* Populate the View object */ Message message = messages.get(position); if (message!= null) { TextView v = (TextView) view.findviewbyid(r.id.messagesendertextview); v.settext(message.getsender() + : ); v = (TextView) view.findviewbyid(r.id.messagesubjecttextview); v.settext(message.getsubject()); /* Return the View object */ return view;

Setting the ListView Adapter Add a ListView widget to the layout file of the activity in which you wish the ListView to appear. In the example below we assume the id for the ListView is named messagelistview. In the oncreate() method of the activity that displays the ListView, after we initialize the messages ArrayList, we create an instance of our custom MessageListAdapter and pass it to the ListView s setadapter() method. ListView listview = (ListView) findviewbyid(r.id.messagelistview); MessageListAdapter adapter = new MessageListAdapter(this, android.r.layout.simple_list_item_1, messages); listview.setadapter(adapter); Modifying the ListView s onitemclicklistener Every onitemclick event from the ListView invokes the onitemclick() method in the onitemclicklistener. Passed to this listener is the position of the View that was clicked. This position directly corresponds to a position (index) of a Message in the messages ArrayList that was passed to the adapter s constructor. Therefore, every time a user presses a View in the ListView, we can get the original Message object in the messages ArrayList and use any of the data that is in the object.

For example, below I show a simple modification to the onitemclicklistner that starts a different activity when a View object in the ListView is pressed. Here, we assume the ListView is in a class named MessageList and we are starting an activity in a class named MessageEditor. Before calling startactivity() we put the Message object associated with the View that was pressed in the Intent. AdapterView.OnItemClickListener handler = new AdapterView.OnItemClickListener() { public void onitemclick(adapterview parent, View v, int position, long id) { Intent i = new Intent(MessageList.this, MessageEditor.class); i.putextra("message", messages.get(position)); startactivity(i); ;