Chapter One, simple usage of the ListView (Android)

Source: Internet
Author: User

This article focuses on the simple filling of the arrayadapter and Baseadapter of the Android ListView.

1.arrayadapter padding

Layout:

<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/main_listview "        android:layout_width=" Match_parent "        android:layout_height= "Match_parent"        ></ListView></RelativeLayout>

Code:

Package Com.example.demo02;import Android.os.bundle;import Android.app.activity;import android.view.Menu;import Android.widget.arrayadapter;import Android.widget.listview;public class Mainactivity extends Activity {private ListView Main_listview; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//Initialize control and Data Initview ();} private void Initview () {Main_listview = (ListView) This.findviewbyid (R.id.main_listview);//Initialize Data arrayadapter< string> adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1); for (int i = 0; i <; i++) {Adapter.add ("item-->" + i);} Add Itemmain_listview.setadapter (Adapter) to the ListView;}}
Debugging:



2.baseadapter padding

Layout:

<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/main_listview "        android:layout_width=" Match_parent "        android:layout_height= "match_parent"        ></ListView>    </RelativeLayout>
Mainactivity:

Package Com.example.demo03;import Android.app.activity;import Android.os.bundle;import Android.widget.ListView; public class Mainactivity extends Activity {private ListView main_listview;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Initialize Data        initview ();    } private void Initview () {//TODO auto-generated Method Stubmain_listview = (ListView) This.findviewbyid (R.id.main_ ListView); Mainadapter adapter = new Mainadapter (this); Main_listview.setadapter (adapter);}    }

Mainadapter:

Package Com.example.demo03;import Android.content.context;import Android.view.view;import android.view.ViewGroup; Import Android.widget.baseadapter;import Android.widget.textview;public class Mainadapter extends Baseadapter { Private context Context;public Mainadapter (context context) {This.context = context;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn 10;} @Overridepublic Object getItem (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic view GetView (int arg0, view arg1, ViewGroup arg2) {//TODO auto-generated method Stubtextview TV = new Tex TView (context); Tv.settext ("Item > "+arg0); return TV;}}
Debugging:




Chapter One, simple usage of the ListView (Android)

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.