Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a list View Program in Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to create a list view program in Android, with a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

List View (ListView) is one of the most useful view controls for displaying variable amounts of data on the Android platform.

Step 0: create the project

Create a new Android project in Eclipse. We named the project MT-List and the initial activity TutListActivity. This Activity must inherit the ListActivity class, which is a special Activity class that helps manage ListView controls. We are using Android2.3.3 's API.

Step 1: design the list interface

In fact, very little needs to be done in this step. A ListView control consisting of duplicates, each with the same layout (one template for each item). We want to display a list of article titles. Each title is an item in ListView. Therefore, only one TextView control is required for the template for each list item. Add a list_item.xml layout resource file to your project, which describes the template layout for each item in the list. In this case, it might look like this:

Font size plus fill attribute values to ensure that each of the following tables in the average screen size is large enough for the average finger size to touch the target.

Step 2: populate ListView with data

The ListView control is designed to load data from a data source. You can use adapters to read data from databases, arrays, or other data sources. In this program, we use an array as the data source. In the future, you can replace the array with some real-time data sources. Create two string arrays in your project (you can add them to strings.xml or a separate arrays.xml file, as you like). Name one array "tut_titles" and the other "tut_links". Populate the two arrays with valid headings and URL from the site. Here is our array:

Design & Build a minute iOS Phone App: Design Comp Slicing Best of Tuts+ in February 2011 Create a Brick Breaker Game with the Corona SDK: Game Controls Exporting Graphics for Mobile Apps: PNG or JPEG? Android Tablet Design Build a Titanium Mobile Pizza Ordering App: Order Form Setup Create a Brick Breaker Game with the Corona SDK: Application Setup Android Tablet Virtual Device Configurations Build a Titanium Mobile Pizza Ordering App: Topping Selection Design & Build a recent iOS Phone App: Interface Builder Setup http://mobile.tutsplus.com/tutorials/mobile-design-tutorials/80s-phone-app-slicing/ http://mobile.tutsplus.com/articles/news/best-of-tuts-in-february-2011 / http://mobile.tutsplus.com/tutorials/corona/create-a-brick-breaker-game-with-the-corona-sdk-game-controls/ http://mobile.tutsplus.com/tutorials/mobile-design-tutorials/mobile-design_png-or-jpg/ http://mobile.tutsplus.com/tutorials/android/android-tablet-design/ http://mobile.tutsplus.com/tutorials/appcelerator/build-a-titanium-mobile-pizza-ordering-app -order-form-setup/ http://mobile.tutsplus.com/tutorials/corona/corona-sdk_brick-breaker/ http://mobile.tutsplus.com/tutorials/android/android-sdk_tablet_virtual-device-configuration/ http://mobile.tutsplus.com/tutorials/appcelerator/pizza-ordering-app-part-2/ http://mobile.tutsplus.com/tutorials/iphone/1980s-phone-app_interface-builder-setup/

Of course, this data is static. In some cases, using static data makes sense for a ListView. In these cases, using a string array resource is very simple and convenient. You need to make sure that the sorting of the title and link is the same so that the two arrays can match.

Step 3: adapt the array to ListView

Now that the program has the data, let's display them. Go back to TutListActivity.java, modify the onCreate () method, and use the setListAdapter () method to load the data. Unlike regular activities, ListActivity does not need to use setContentView () in cases where the whole activity is just a ListView. When you're done, your entire ListActivity will look like this:

Public class TutListActivity extends ListActivity {@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setListAdapter (ArrayAdapter.createFromResource (getApplicationContext (), R.array.tut_titles, R.layout.list_item));}}

At this point, you can run the program. You will see a ListView with all the titles in this tutorial. Both the top and bottom strokes are normal. However, there has been no response to clicking on the title.

Step 4: process ListView entry clicks

Dealing with clicks on items in ListView is similar to that of other view objects: using a listener. Here, we focus on OnTimeClickListener. You may notice that we haven't dealt with ListView objects directly. Now is the time. In ListActivity, simply call the getListView () method to traverse the ListView, then call the setOnItemClickListener () method and implement them all at once:

GetListView () .setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {/ / TBD}})

The onItemClick () method is called each time the user clicks on each item in the list view. For convenience, it can pass in several useful parameters, one of which we need to start the viewer activity (viewer activity). Wait, what is the viewer activity?

Step 5: create a viewer activity

That's a very good question. Let's create a viewer activity now! This activity will be used to display the tutorial content to the user. Create a new class by inheriting from Activity and name it TutViewerActivity.java. Create a layout resource file for it that contains only one item: a WebView control. The layout file should look like this:

In the onCreate () method of the TutViewerActivity class, call the setContentView () method and

Pass in this layout. Don't forget to add this activity to your AndroidManifest.xml file.

Step 6: start Details Activity

Focus on TutListActivity for a while. Let's see what we need to do to launch the viewer Activity to display the appropriate article link. In the onItemClick () method, the position of the clicked item is passed in as an int value. This is the value we need to access the article link array.

The string value in the linked array is URL. An easy way to pass a URL to another activity is to add a Uri to the Intent through the setData () method. Here is the final implementation of onItemClick (), which starts the viewer activity, passing in the appropriate URL:

Override public void onItemClick (AdapterView parent, View view, int position, long id) {String content = links [position]; Intent showContent = new Intent (getApplicationContext (), TutViewerActivity.class); showContent.setData (Uri.parse (content)); startActivity (showContent);}

If you just paste this code directly into the * side of the onCreate () method, you will notice that the links variable has not been defined. Because it will be used for the OnItemClickListener class, the variable must be a find value, as follows:

Final String [] links = getResources () .getStringArray (R.array.tut_links)

This line must be placed before the definition of OnItemClickListener. Yes, you can define it as a non-final member variable. For more complex situations, that is even necessary. But here, we can put all the code in the method. Now if you run this program, you will get a blank viewer interface. The activity starts correctly, but we need to connect to the viewer activity to load the URL in the WebView control.

Step 7: load URL

Attention goes back to the TutViewerActivity.java file. After calling the setContentView () method, add code to retrieve the Uri in the incoming Intent and convert it to a String variable. Then add a call to the loadUrl () method of the WebView class. The code for the entire TutViewerActivity class will look like this:

Public class TutViewerActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.tut_view); Intent launchingIntent = getIntent (); String content = launchingIntent.getData (). ToString (); WebView viewer = (WebView) findViewById (R.id.tutView); viewer.loadUrl (content);}}

Now run the program again. Is it normal? No, not yet! * one detail: you must add end-of-net permissions to the AndroidManifest.xml file. Now run this program, and you will see that the program is working properly:

Step 8: improve the experience (optional) for now, the user experience is only available. ListView displays all article titles, and users can click on an item in the list and go to an activity with a WebView control to display the appropriate URL content. To make this most basic implementation more elegant, you may consider the following aspects:

Set the initial zoom of the browse view to cope with the subsequent zoom

Configure WebView to include more controls that enhance the browsing experience

Load the article list dynamically instead of using a static array

Add special effects visual effects to ListView

Make full use of the big screen

Add additional information to ListView: subtitle, difficulty level, icon, read indication, favorite button, etc.

Thank you for reading this article carefully. I hope the article "how to create a list View Program in Android" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report