SoFunction
Updated on 2025-03-11

Analysis of the usage example of ListActivity in Android

This article analyzes the usage of ListActivity in Android. Share it for your reference, as follows:

The procedure is as follows:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class A08Activity extends ListActivity {
 private int selectedItem=-1;
 private ArrayAdapter<String> aa;
 private String[] s;
 private static final int MENU_LIST01=;
 private static final int MENU_LIST02=+1;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    //setContentView();
  }
  protected void onListItemClick(ListView l,View v,int position,long id){
   selectedItem=position;
   (, s[selectedItem], Toast.LENGTH_LONG).show();
   (l, v, position, id);
  }
  public boolean onCreateOptionsMenu(Menu menu){
   int idGroup1=0;
   int orderMenuItem01=;
   int orderMenuItem02=+1;
   (idGroup1, MENU_LIST01, orderMenuItem01, .str_menu_list01);
   (idGroup1, MENU_LIST02, orderMenuItem02, .str_menu_list02);
 return (menu);
  }
  public boolean onOptionsItemSelected(MenuItem item){
   switch(()){
   case MENU_LIST01:
   s=new String[]{
    getResources().getString(.str_list01),
    getResources().getString(.str_list02),
    getResources().getString(.str_list03),
    getResources().getString(.str_list04)
       };
   aa=new ArrayAdapter<String>(,,s);
   (aa);
   break;
   case MENU_LIST02:
   s=new String[]{
    getResources().getString(.str_list05),
    getResources().getString(.str_list06),
    getResources().getString(.str_list07),
    getResources().getString(.str_list08)
       };
   aa=new ArrayAdapter<String>(,,s);
   (aa);
   break;
   }   
 return (item);
  }
}

res/layout/ as follows:

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:andro
  android:
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="@string/hello"
/>

ListActivity's type layout configuration application is often used to display menu lists and list details items. It does not have to be like Activity. You can set the layout Layout without using setContentView to display the page. ListActivity can directly load the list into ListActivity without rewriting protected void onCreate(Bundle savedInstanceState). Common users such as voting option selection, multi-item list list display, file explorer, etc.

This example creates two Menu menu functions in ListActivity, dynamically displaying different list items on ListActivity. In order to capture the user's clicked items on ListActivity, the onListItemClick() method is rewritten to get the ListItem item clicked by the user. In order to simplify the program, Toast is used to prompt.

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial"and"Android programming activity operation skills summary

I hope this article will be helpful to everyone's Android programming design.