Android Studio ListView Simple Instance

Source: Internet
Author: User


Layout layouts

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

<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:paddingleft= "@dimen/activity_horizontal_margin"

android:paddingright= "@dimen/activity_horizontal_margin"

android:paddingtop= "@dimen/activity_vertical_margin"

android:paddingbottom= "@dimen/activity_vertical_margin"

>

<listview

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

Android:id= "@android: Id/list"

/>

</RelativeLayout>

Code

Package Com.example.test.testlistview;


Import android.app.ListActivity;

Import Android.os.Bundle;

Import Android.view.View;

Import Android.widget.ArrayAdapter;

Import Android.widget.ListView;

Import Android.widget.TextView;

Import Android.widget.Toast;


Import java.util.ArrayList;

Import java.util.List;


public class Listviewarrayadapter extends Listactivity {


@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_list_view_arrayadapter);

List<string>ls = new arraylist<string> ();

Ls.add ("test data 1");

Ls.add ("Test Data 2");

Ls.add ("test data 1");

Ls.add ("Test Data 2");

Ls.add ("test data 1");

Ls.add ("Test Data 2");

Ls.add ("test data 1");

Ls.add ("Test Data 2");

Ls.add ("test data 1");

Ls.add ("Test Data 2");

Ls.add ("test data 1");

Ls.add ("Test Data 2");


Arrayadapter Adapter =new arrayadapter (this,android. R.LAYOUT.SIMPLE_LIST_ITEM_1,LS);

Setlistadapter (adapter); This function is only inherited by Listactivity

}


@Override

protected void Onlistitemclick (ListView l, View v, int position, long ID) {


String s = ((TextView) v). GetText (). toString ();

Toast.maketext (This, "hint" +position+s,toast.length_long). Show ();


Super.onlistitemclick (l, V, position, id);

}

}

//

A simple listview, the key is the Arrayadapter layout and data adaptation. Android. R.layout.simple_list_item_1 the style of the system, you can change the layout style according to your own requirements. List<string>ls = new Arraylist<string> () is used to display the data.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/76/0F/wKiom1ZJicygZY0ZAAEjzAEQO9I345.png "style=" float: none; "Title=" Jinshan Mobile assistant 20151116 153820.png "alt=" Wkiom1zjicygzy0zaaejzaeqo9i345.png "/>

Simpleadapter fit in a ListView

Layout

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

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Horizontal" android:layout_width= "Match_parent"

android:layout_height= "Match_parent" >

<imageview

Android:id= "@+id/img2"

android:layout_height= "Wrap_content"

Android:layout_width= "Wrap_content"

/>

<linearlayout

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:orientation= "Vertical" >

<textview

Android:id= "@+id/title2"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"/>

<textview

Android:id= "@+id/price"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"/>


</LinearLayout>

</LinearLayout>

Key Code Settings ListView

Simpleadapter adapter1 =new simpleadapter (this,lm,r.layout.simple2, new string[]{"title", "IMG", "Price"},new int[]{ R.id.title2,r.id.img2,r.id.price});

Setlistadapter (Adapter1);

Get the data

Public list<map<string, object>> GetData2 ()

{

Map<string,object>value =new hashmap<string,object> ();

LIST<MAP<STRING,OBJECT>>LM =new arraylist<map<string,object>> ();

Value.put ("title", "Apple");

Value.put ("img", R.drawable.aphone);

Value.put ("Prices", "Price: 5800");

Lm.add (value);


Value =new hashmap<string,object> ();

Value.put ("title", "Samsung");

Value.put ("img", R.drawable.sphone);

Value.put ("Prices", "Price: 4800");

Lm.add (value);


Value =new hashmap<string,object> ();

Value.put ("title", "Huawei");

Value.put ("img", R.drawable.hphone);

Value.put ("Prices", "Price: 6000");

Lm.add (value);

return LM;

}

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/76/0D/wKioL1ZJih7Q8B4NAAD1ZatxsP0152.png "style=" float: none; "Title=" Jinshan Mobile assistant 20151116 154132.png "alt=" Wkiol1zjih7q8b4naad1zatxsp0152.png "/>

Simplecursoradapter adaptation ListView for the display of database data

Key code

Get a Cursor object pointing to the system contacts database to get the data source

cursor cursor = getcontentresolver (). query (Contacts.people.content_uri,null,null,null,null);

Startmanagingcursor (cursor);

Instantiating a list adapter

ListAdapter la = new Simplecursoradapter (this,android. R.layout.simple_list_item_2,cursor,new string[]{contacts.people.name,contacts.people.number},new int[]{android. R.id.text1,android. R.ID.TEXT2});

Setlistadapter (LA);

Be sure to read the contact's permissions in Androidmanifest.xml.

<uses-permission android:name= "Android.permission.READ_CONTACTS"/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/76/0D/wKioL1ZJih-gv2TXAAChxck-4xs125.png "style=" float: none; "Title=" Jinshan Mobile assistant 20151116 154156.png "alt=" Wkiol1zjih-gv2txaachxck-4xs125.png "/>

Code download

http://down.51cto.com/data/2119105


Android Studio ListView Simple Instance

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.