Android's Simpleadaper app

Source: Internet
Author: User

ListView Page Layout: Layout/activity_main.xml:

<relativelayout xmlns:android= "Http://"
Xmlns:tools= "Http://"
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= "Com.example.day_05_02.MainActivity" >

Android:id= "@+id/lvgenerals"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:divider= "#ccc"
android:dividerheight= "10DP"

Java code in this layout:

Package com.example.day_05_02;

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

Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.BaseAdapter;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;

public class Mainactivity extends Actionbaractivity {
Private ListView lvgenerals;//listview Object
Private list<map<string,object>> generals;//To display a collection of data
Private final String ImageSource = "ImageSource";value of key in//map
Private final String generalname = "name";value of key in//map
Private Baseadapter Generaladapter;//Adapter
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
InitData ();
Initview ();
private void InitData () {
TODO auto-generated Method Stub
//Initialize the data collection to be displayed
Generals = new Arraylist<map<string,object>> ();
//Initialize picture resources
int [] Images = {
//Initialize text resources
String [] names = Getresources (). Getstringarray (R.array.generals);
//Put the image and text resources in a map and add the map to the list of generals
for (int i=0;i<names.length;i++) {
map<string,object> general = new hashmap<string,object> ();
General.put (ImageSource, images[i]);
General.put (Generalname, names[i]);
Generals.add (General);

private void Initview () {
TODO auto-generated Method Stub
Lvgenerals = (ListView) Findviewbyid (;
//Initialize adapter

Emphasis explains that the first parameter is the activity object where the layout is to be displayed, the second parameter is the collection object to display the data, and the third is the layout page for adaptation.

Generaladapter = new Simpleadapter (This, generals,
R.layout.activity_ General_,
New String[]{imagesource,generalname}, new Int[]{,});
//set adapter
Lvgenerals.setadapter (Generaladapter);


Fit Page Layout: layout/activity_general_.xml

<linearlayout xmlns:android= "Http://"
Xmlns:tools= "Http://"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

Android:id= "@+id/ivtumb"
Android:layout_width= "80DP"
android:layout_height= "80DP"
android:src= "@drawable/baiqi"

Android:id= "@+id/tvname"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "White"/>


String string resource: Values/strings.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<string name= "App_name" >Day_05_02</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Action_settings" >Settings</string>
<string name= "Title_activity_general_" >General_Activity</string>
<string-array name= "Generals" >
<item> White </item>
<item> Caocao </item>
<item> Genghis Khan </item>
<item> Han </item>
<item> Li Shimin </item>
<item> Noor Nurhachi </item>
<item> Sun Bin </item>
<item> Sun Wu </item>
<item> Zhu Yuanzhang </item>
<item> Yue Fei </item>


650) this.width=650; "src=" "/>

This article from "Follow the heart of the other side" blog, declined reprint!

Android's Simpleadaper app

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: 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.