The ListView for Android Control development

Source: Internet
Author: User

The ListView for Android Control development

This article mainly describes the use of the ListView control in Android development.

Java code: Package Com.example.listview;


Import java.util.ArrayList;
Import Java.util.HashMap;


Import android.app.ListActivity;
Import Android.os.Bundle;


Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;


public class Mainactivity extends Listactivity {



@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Declares a list, and the cells in the list are map
arraylist
Declares a map, and the element in the map is a string key-value pair
hashmap<string, string> map1 = new hashmap<string, string> ();
hashmap<string, string> map2 = new hashmap<string, string> ();
hashmap<string, string> map3 = new hashmap<string, string> ();

Put key value pair data to map
Map1.put ("name", "Zhangsan");
Map1.put ("IP", "192.168.1.1");
Map2.put ("name", "Lisi");
Map2.put ("IP", "192.168.1.2");
Map3.put ("name", "Wangwu");
Map3.put ("IP", "192.168.1.3");

To add a map to the list
List.add (MAP1);
List.add (MAP2);
List.add (MAP3);

Build Adapter Adapter
Simpleadapter listadapter = new Simpleadapter (this, list,
R.layout.user, new string[]{"name", "IP"},
New Int[]{r.id.name,r.id.ip});
Setlistadapter (ListAdapter);


}

Binds a listener to the list, and when a cell in the list is pressed, the function
@Override
protected void Onlistitemclick (ListView l, View v, int position, long ID) {
TODO auto-generated Method Stub
Super.onlistitemclick (l, V, position, id);
SYSTEM.OUT.PRINTLN ("id =" + ID);
System.out.println ("position =" + position);

}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


}


main layout file Mail.xml code: <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/linearlayout1"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
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 ">

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<linearlayout
Android:id= "@+id/listlinearlayout"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"

>
<listview
Android:id= "@id/android:list"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:drawselectorontop= "false"
android:scrollbars= "Vertical" ></ListView>
</LinearLayout>


</LinearLayout>



User.xml Code implementation: <?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/maplinear"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Horizontal"
Tools:ignore= "Uselessparent" >


<textview
Android:id= "@+id/name"
Android:layout_width= "180dip"
android:layout_height= "30dip"
Android:textsize= "20dip"
android:gravity= "Center_vertical"/>


<textview
Android:id= "@+id/ip"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:textsize= "20dip"/>
</LinearLayout>


The implementation results are as follows:

Clicking on the list of related lists will output the relevant list information in Logcat as follows:

The ListView for Android Control development

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.