Sunteți pe pagina 1din 4

How to display the data from database by ListView on Android

Fan Jiang
School of Computer Science and Technology Wuhan University of Technology Wuhan, China jiangfan@embedinfo.com
AbstractAndroid is an integrated open platform for mobile devices provided by Google Inc.. It includes operating system, middleware and some key applications. It has also an excellent development and debugging environment. In order to enable the ListView, an Android application development widget, to display images and data from database, a customized layout file and extend class are used to implement a customized ListAdapter. The feature of the ListView is realized, and the achieved result can show the flexibility and power of ListView on Android. Keywords- Android; ListView; ListAdapter; Database

Shaoping Ku
School of Computer Science and Technology Wuhan University of Technology Wuhan, China kushaoping@whut.edu.cn

II.

ANDROID ARCHITECTURE

Android software frame is shown in Fig.1 and the software structure can be divided into the following several levels from top to down. Application. Application Framework. Libraries and Android RunTime. Operating System.

I.

INTRODUCTION

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language [1]. The feature of the openness of the Android platform can promote the innovation of technology (including platform itself). In addition, it not only help to reduce cost of development, but also is convenient to customize products with characteristic by the operators. Therefore, the Android platform has a large market potential [2]. Developers need ListAdapter to define the display of ListView. ListAdapter is an adapter just like a bridge between data and ListView. The Android API has provider some ListAdapter for developers. ArrayAdapter only allows storing an array of String, so in that list, each list item contains only one String, it is relatively simple to display information. For this reason, there are some limitations of ArrayAdapter. Although SimpleAdapter could give the content of list item more individuation, but the layouts that provided by system are still drab and only support to display strings. What is the most important issue is that they are unlikely to associate the date from database with ListView [3] [4]. By customizing ListAdapter class as well as layout file, the author complete to display picture and more than one text message in list items, and show the data of database by ListView.

Figure 1. Android Architecture

A. Applications Android application usually involves interaction between user interface and user. Currently the Android application is developed by Java language. Android will ship with a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others. At the same time, the application developers can use the API of application framework to achieve their programs; it is also the source of the enormous potential of Android [5].

978-1-4244-5874-5/10/$26.00 2010 IEEE

B. Application Framework Developers have full access to the same framework APIs used by the core applications. The application architecture is designed to simplify the reuse of components; any application can publish its capabilities and any other application may then make use of those capabilities (subject to security constraints enforced by the framework). This same mechanism allows components to be replaced by the user. C. Libraries & RunTime Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework. Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language. Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint. The VM is register-based, and runs classes compiled by a Java language compiler that have been transformed into the .dex format by the included "dx" tool. The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management. D. Linux Kernel Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack [6]. III. EXAMPLE OF THE ANDROID APPLICATION DEVELOPMENT WIDGET LISTVIEW

A. Example Effect

Figure 2. Example Effect

B. Module Framework of Example

Figure 3. Module Framework of Example

Database has provided two methods to be called: query and insert; Insert date into database once the process started; Get date from database by querying, prepare to display; ListAdapter is responsible for defining the layout of list items, and distribute data to each item. Associate ListAdapter to ListView to achieve the display of list.

The ListView provided by Android API is one of the most widely used widget during application development. It has features such as simple operation, intuitive display, and flexible style etc. In addition, it is particularly convenient to display the database data with ListView. This example will produce a table of film program information to show movies poster, name, duration and category.

C. Database Database is mainly responsible for storing the information of films and providing data for UI. The SQLite, a lightweight embedded database provided by Android system, has been adopted in the development of this project. It supports SQL statements. After database is created, the database file will be generated automatically under a specified path of the file system in Android mobile device, and the data is stored in it. In our database, a table is created, including six fields: _id, title, year, duration, category, and image. The _id is primary key of table, unique and could not be null.

TABLE I. Attribute _id title year duration category image Data Type String String String String String int

ATTRIBUTES OF DATABASE Description ID of film, primary key, unique and could not be null Title of film Showtime of film Duration of film Category of film, such as comedy, Action, etc. Image of film, a resource index of the image

used directly, we need to extend it and override the abstract method bindView() to bind the data of Cursor and view together. Then we can display database data with this extension ListAdapter. We could display pictures if the customize layout file contain ImageViews. The code of extension ListAdapter is as follows: 1) 2) 3) 4) 5) 6) 7) 8) 9) 10) 11) 12) 13) 14) 15) 16) public class MyListAdapter extends ResourceCursorAdapter implements IConstants { public CursorListAdapter(Context context, int layout, Cursor c) { super(context, layout, c); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView title = (TextView) view.findViewById(R.id.rowTitle); title.setText(cursor.getString(ROW_TITLE)); TextView duration = (TextView) view.findViewById(R.id.rowDuration); duration.setText(cursor.getString(ROW_DURATI ON)); TextView category = (TextView) view.findViewById(R.id.rowCategory); category.setText(cursor.getString(ROW_CATEG ORY)); ImageView icon = (ImageView) view.findViewById(R.id.rowIcon); icon.setBackgroundResource(cursor.getInt(ROW _ICON)); } }

Two methods have been provided for calling by the database: a) query: Cursor queryRecord(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) b) insert: void insertRecord(ContentValues values) D. ListAdapter ListAdapter is the bridge between a ListView and the data that backs the list. Because of existence of ListAdapter, ListView have very strong flexibility and bountiful manifestation. The most widely used adapters in Android API are ArrayAdapter and SimpleAdapter. ArrayAdapter: it is a bridge between array and ListView, which is responsible for showing the data of an array through ListView one by one. At this time, each row of list usually contains only one TextView, and the content TextView displayed could only be a string, so the style of list generated by ArrayAdapter is relatively simple, and the content expressed is limited. SimpleAdapter: it is a bridge between ArrayList with Maps and ListView. Each row of list corresponds to a Map. Accordingly, each row could hold multiple TextViews to enrich the content expressed. At the same time, SimpleAdapter could also define the layout of each list row referring to a custom layout file. Compared with ArrayAdapter, the style of list generated by SimpleAdapter is much more flexible. However, the process of binding data to SimpleAdapter is complicated, and it is still hard to display images. Whats more, it is impossible to show database data through ArrayAdapter or SimpleAdapter. In fact, the developers could not only use the ListAdapters already existing in Android development framework, but also extend them to implement personalized ListAdapter and control the style of list flexible. ResourceCursorAdapter provided by Android API is a ListAdapter that supports to display database data as well as refer to custom layout file. Since ResourceCursorAdapter, an abstract class, could not be

Code explanation: The parameter named layout in line 3 is a resource identifier of a layout file that defines the views for this list rows. The parameter named cursor in line 7 is a cursor from which to get the data. The cursor must include a column named "_id" or the adapter will not work.

E. ListView The ListView is a view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view. ListView has provided a variety of interfaces for developers in UI development. For example: 1) setDivider(Drawable divider) Sets the drawable that will be drawn between each item in the list. 2) addHeaderView(View v) Add a fixed view to appear at the top of the list. 3) setOnScrollListener(AbsListView.OnScrollListener l) Set the listener that will receive notifications every time the list scrolls.

4) setOnItemClickListener(AdapterView.OnItemClickListe ner listener) Register a callback to be invoked when an item in this list has been clicked. It is the most frequently used method of ListView. The code of displaying list is as follows: DataFactory.datebaseAdapter.open(); cur = DataFactory.datebaseAdapter.queryRecord(null, null, null, null, null, null); 3) CursorListAdapter adapter = new CursorListAdapter(this, R.layout.list_row, cur); 4) listView.setAdapter(adapter); 5) listView.setOnItemClickListener(new OnItemClickListener() { 6) @Override 7) public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 8) View backView = findViewById(R.id.ParentLayout); 9) showProgInfo(position, backView); 10) } 11) }); 12) DataFactory.datebaseAdapter.close(); Code explanation: Opening database is needed before any reading or writing operation. Make sure to close database when you no longer need it. Line 2: Query all the records in database and return a Cursor. Line 3: Generate an adapter by passing the index of layout file and Cursor as parameters. 1) 2)

Line 4: Associate the adapter to list. Line 5: Register a listener to respond to click action on list items. When an item is clicked, process will show details information of this program by switching to a details page, which has shown in Fig.2. IV. CONCLUSION

In this paper, the author have achieved a beautiful list base on reference to a custom layout file to change the layout of each list row, at the same time, completed to display database data by ListView by implementing a personalized ListAdapter. About the source of data, How to obtain the XML data stream that contains program information through connecting to the Internet, and to be parsed to store into database, it will be the direction for further research. REFERENCES
[1] Gong Lei, Zhou Cong. Development and research of mobile terminal application based on Android (in Chinese)[J]. Computer and Modernization, 2008 Yu Zhilong, Chen Yuxun, Zheng Jie etc. Collection of Google Android SDK development paradigms (in Chinese) [M]. Posts & Telecom Press, 2009 Gao Huantang. Principles of Android application framework and 36 plans of program design (in Chinese) [M]. MISOO Design Center, 2008 GoogleGroups. Android Discussion Groups. [Online] http://code.google.com/android/groups.html. Jin Yan, Yao Shanglang. Getting started and actual development of Google Android (in Chinese) [M]. Posts & Telecom Press, 2009 Android Developers, http://developer.android.com/intl/zhCN/guide/index.html.

[2]

[3] [4] [5] [6]

S-ar putea să vă placă și