Android's Simpleadaper app

Source: Internet
Author: User

ListView Page Layout: Layout/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"
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" >


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

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.support.v7.app.ActionBarActivity;
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
@Override
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 = {
R.drawable.baiqi,r.drawable.caocao,r.drawable.chengjisihan,
R.drawable.hanxin,r.drawable.lishimin,r.drawable.nuerhachi,
R.drawable.sunbin,r.drawable.sunwu,r.drawable.yuefei,
R.drawable.zhuyuanzhang
};
//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 (r.id.lvgenerals);
//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[]{r.id.ivtumb, R.id.tvname});
//set adapter
Lvgenerals.setadapter (Generaladapter);
}




}

Fit Page Layout: layout/activity_general_.xml

<linearlayout 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"
android:orientation= "Vertical" >


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

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


</LinearLayout>

String string resource: Values/strings.xml

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


<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>
</string-array>
</resources>

Effect:

650) this.width=650; "src=" http://img.blog.csdn.net/20141130094829250 "/>

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