Android [elementary tutorial] Chapter 5 ListView Control

Source: Internet
Author: User

 

This time, we will learn the ListView control. The ListView control can display the data in rows and listen to the event of a single click. The monsters will catch Tang Miao's mentoring one by one. What? Do you not like traveling to the West? I like to see the Water Margin, Mom! You want me to type it to death? Haha, joke.

Let's take a look at main. xml.

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<ListView android: layout_height = "wrap_content" android: id = "@ + id/listView"
Android: layout_width = "match_parent"> </ListView>
<TextView android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "@ string/hello"
Android: id = "@ + id/text"> </TextView>
</LinearLayout>
Java code of the main Activity:

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. ArrayAdapter;
Import android. widget. ListView;
Import android. widget. TextView;
 
Public class ButtonDemoActivity extends Activity implements OnItemClickListener
{
Private TextView text = null;
Private String [] item = {"Tang Seng", "Sun Wukong", "", "Sha Monk "};
Private ListView listView;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );
 
// Find the ListView control in main. xml by ID
ListView = (ListView) findViewById (R. id. listView );

// Set an Array adapter to put Array data into the adapter
ArrayAdapter adapter = new ArrayAdapter (this,
Android. R. layout. simple_list_item_1, item );

// Adapt the ListView
ListView. setAdapter (adapter );

// Listener for Event Selection in ListView
ListView. setOnItemClickListener (this );
 
}
 
Private void updateText (String string)
{
// Set the text information to display in the TextView control.
Text. setText (string );
}
 
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int position, long arg3)
{
String str = "this time the goblin grabbed" + item [position] +! ";
UpdateText (str );

}
 
}

In fact, many controls use the adapter mode, such as Spinner, ListView, and GridView. Android can set various types of adapters, such as array adapter, List adapter, and basic adapter, it's like a monster wants to catch Tang Miao. Does he have to know how many people are migrating to different places? You caught my master, and those younger siblings certainly did not agree. Haha, these are all digression. Well, this chapter is over.

 
 

From: kangkangz4 Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.