Android learning notes: simple use of ListView, androidlistview

Source: Internet
Author: User

Android learning notes: simple use of ListView, androidlistview

(1)

<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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true" >    </ListView></RelativeLayout>

(2)

Package com. example. listview1; import java. util. arrayList; import java. util. list; import android. r. anim; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listAdapter; import android. widget. listView; import Droid. widget. toast; public class MainActivity extends Activity {private ListView listView1; private ArrayAdapter <String> arrayAdapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView1 = (ListView) this. findViewById (R. id. listView1); // it can be set to optional or single-choice CHOICE_MODE_SINGLE. The following simple should also correspond to listView1.setChoiceMode (Li StView. CHOICE_MODE_MULTIPLE); // sets the selected mode of listView1. arrayAdapter = new ArrayAdapter <String> (MainActivity. this, android. r. layout. listener, getDataSource (); listView1.setAdapter (arrayAdapter);/** listView1 Click Event */listView1.setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (MainActivity. this, "-->" + arrayAdapter. getItem (position), Toast. LENGTH_SHORT ). show () ;}}) ;}public List <String> getDataSource () {List <String> list = new ArrayList <String> (); for (int I = 0; I <30; I ++) {list. add ("jack" + I);} return list;} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}


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.