Android Development Learning notes (11) listview/activitymanager/arrayadapter Learning

Source: Internet
Author: User

I am working on an android course design. The main function is the task manager, which requires activitymanager. So I will use this class as a listview exercise.

This task adds all running processes to the arrayadapter adapter and displays them in the listview.

The layout file is simple. There is only one refresh button and one listview.

Activity_main.xml

<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"    tools:context=".MainActivity" >    <ListView        android:id="@+id/myListView1"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_above="@+id/refresh"         />    <Button        android:id="@id/refresh"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="refresh"        android:layout_alignParentBottom="true"/></RelativeLayout>

Main activity mainactivity. Java

Package COM. xujin. listviewtest; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. app. activitymanager; import android. OS. bundle; import android. view. view; import android. widget. arrayadapter; import android. widget. button; import android. widget. listview; import android. widget. toast; public class mainactivity extends activity {private button refresh; private listview prolist; private arrayadapter <string> arrayadapter; private arraylist <string> arraylistpro; private activitymanager myactivitymanager; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); refresh = (button) findviewbyid (R. id. refresh); prolist = (listview) findviewbyid (R. id. mylistview1); // obtain information about the running process and display the getrunningappprocessinfo () in listview; // bind the listener refresh to the refresh button for the refresh button. setonclicklistener (New button. onclicklistener () {@ override public void onclick (view v) {getrunningappprocessinfo (); Toast = toast. maketext (mainactivity. this, "Refresh... ", toast. length_long); toast. show () ;}}) ;}public void getrunningappprocessinfo () {myactivitymanager = (activitymanager) mainactivity. this. getsystemservice (activity_service); arraylistpro = new arraylist <string> (); List <activitymanager. runningappprocessinfo> mrunningpros = myactivitymanager. getrunningappprocesses (); int I = 1; for (activitymanager. runningappprocessinfo ampro: mrunningpros) arraylistpro. add ("+ (I ++) +": "+ ampro. processname + "(ID =" + ampro. PID + ")"); arrayadapter = new arrayadapter <string> (mainactivity. this, android. r. layout. simple_list_item_1, arraylistpro); prolist. setadapter (arrayadapter );}}

Below are some explanations:

Private button refresh;
Private listview prolist;
Private arrayadapter <string> arrayadapter;
Private arraylist <string> arraylistpro; // string-type array
Private activitymanager myactivitymanager;

The simplest application of arrayadapter is to display a line of text information.

Public arrayadapter (context, int textviewresourceid, list <t> objects)

The first parameter is the context, the second parameter is the XML to be displayed, and the third parameter is the content to be displayed in XML, which is a list <t> type data.

More detailed explanation:

Public void getrunningappprocessinfo () {// service information of the system from getsystemservice (activity_service). The data type is activitymanager myactivitymanager = (activitymanager) mainactivity. this. getsystemservice (activity_service); // create a string-type arraylist to store data. arraylistpro = new arraylist <string> (); // myactivitymanager. the data type retrieved by getrunningappprocesses () is list <activitymanager. list of runningappprocessinfo> <activitymanager. runningappprocessinfo> mrunningpros = myactivitymanager. getrunningappprocesses (); int I = 1; // enumerate each element sequentially, and add the element for (activitymanager) using the Add METHOD OF THE arraylist <string> type. runningappprocessinfo ampro: mrunningpros) arraylistpro. add ("+ (I ++) +": "+ ampro. processname + "(ID =" + ampro. PID + ")"); arrayadapter = new arrayadapter <string> (mainactivity. this, android. r. layout. simple_list_item_1, arraylistpro); // use setadapter () to bind listview and adapter to prolist. setadapter (arrayadapter );}

Final effect:

Source File: listview.zip

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.