Simple android studio Listview instance

Source: Internet
Author: User

Simple android studio Listview instance

// Layout <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingLeft = "@ dimen/users" android: paddingRight = "@ dimen/users" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/activity_v Ertical_margin "> <ListView android: layout_width =" match_parent "android: layout_height =" match_parent "android: id =" @ android: id/list "/> </RelativeLayout> // code package com. example. test. testlistview; import android. app. listActivity; import android. OS. bundle; import android. view. view; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. textView; import android. widget. toast; im Port java. util. arrayList; import java. util. list; public class ListViewArrayadapter extends ListActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_list_view_arrayadapter); List <String> ls = new ArrayList <String> (); ls. add ("test data 1"); ls. add ("Test Data 2"); ls. add ("test data 1"); ls. add ("Test Data 2"); ls. add ("test data 1"); ls. add ("Test Data 2 "); ls. add ("test data 1"); ls. add ("Test Data 2"); ls. add ("test data 1"); ls. add ("Test Data 2"); ls. add ("test data 1"); ls. add ("Test Data 2"); ArrayAdapter adapter = new ArrayAdapter (this, android. r. layout. simple_list_item_1, ls); setListAdapter (adapter); // This function is available only when ListActivity is inherited.} @ Override protected void onListItemClick (ListView l, View v, int position, long id) {String s = (TextView) v ). getText (). toString (); Toast. makeText (this, "prompt" + po Sition + s, Toast. LENGTH_LONG ). show (); super. onListItemClick (l, v, position, id) ;}// simple listview, the key is the adaptation of layout and data in ArrayAdapter. The style that comes with the android. R. layout. simple_list_item_1 system. You can change the layout style as required. List <String> ls = new ArrayList <String> () indicates the data to be displayed.

 

Related Article

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.