Example analysis of listactivity usage in Android _android

Source: Internet
Author: User

This article analyzes the usage of listactivity in Android. Share to everyone for your reference, specific as follows:

The procedure is as follows:

Import android.app.ListActivity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.Toast;
 public class A08activity extends listactivity {private int selecteditem=-1;
 Private arrayadapter<string> AA;
 Private string[] s;
 private static final int menu_list01=menu.first;
  private static final int menu_list02=menu.first+1; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
   } protected void Onlistitemclick (ListView l,view v,int position,long id) {selecteditem=position;
   Toast.maketext (A08activity.this, S[selecteditem], Toast.length_long). Show ();
  Super.onlistitemclick (l, V, position, id);
   public boolean Oncreateoptionsmenu (Menu menu) {int idgroup1=0; int Ordermenuitem01=meNu.
   NONE;
   int ordermenuitem02=menu.none+1;
   Menu.add (IdGroup1, menu_list01, ORDERMENUITEM01, r.string.str_menu_list01);
 Menu.add (IdGroup1, menu_list02, orderMenuItem02, r.string.str_menu_list02);
  return Super.oncreateoptionsmenu (menu); public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case Menu_list01:s=new String []{getresources (). getString (r.string.str_list01), Getresources (). getString (r.string.str_list02), getresources
   (). getString (r.string.str_list03), Getresources (). getString (r.string.str_list04)};
   Aa=new arrayadapter<string> (a08activity.this,r.layout.main,s);
   A08Activity.this.setListAdapter (AA);
   Break Case Menu_list02:s=new string[]{getresources (). getString (r.string.str_list05), Getresources (). getString (R.str
       ing.str_list06), Getresources (). getString (r.string.str_list07), Getresources (). getString (r.string.str_list08)
   }; Aa=new arrayadapter<string> (A08Activity.this,r.layout.main,s);
   A08Activity.this.setListAdapter (AA);
   Break
  return super.onoptionsitemselected (item);

 }
}

Res/layout/main.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <textview xmlns:android=
  "http://schemas.android.com/apk/" Res/android "
  android:id=" @+id/mytextview1 "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent"
  android:text= "@string/hello"
/>

Listactivity type layout configuration application, commonly used to display menu lists, columns indicate thin items. It does not have to be like an activity, you can set the version layout without using Setcontentview to display the page, listactivity can not overwrite protected void onCreate (Bundle savedinstancestate), the list is loaded directly into the listactivity, often ufida such as voting options, multiple Items list column display, file resource Manager, and so on.

This example creates two menu menus in listactivity, dynamically displaying different list items to Listactivity, and overrides Onlistitemclick () in order to capture a user's click item on Listactivity. method to get the ListItem item that the user clicked, in order to simplify the program, use Toast to prompt.

More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced tutorials" and "the activity of Android programming skills summary"

I hope this article will help you with the Android program.

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.