Simple use of the ListView--android

Source: Internet
Author: User

1, this example realizes 2, mainly isActivity_main.xml(Layout file) andActivityclass file, the implementation process is relatively simple, directly attached to the source of ha!Activity_main.xml:<?XML version="1.0"encoding="Utf-8"?>
<RelativelayoutxmlnsAndroid="Http://schemas.android.com/apk/res/android"
Android: layout_width="Match_parent"Android: layout_height="Match_parent"
Android: background="@color/COLOR_MAIN_BG" >
<Listview
android:id= android: Layout_width= android: Layout_height= />
</RELATIVELAYOUT>
Mainactivity.java:Package livetelecast.thonlon.example.cn.thonlonlivetelecast;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.Toast;

public class MainactivityExtends Appcompatactivity {
   Private ListViewListView;
   @Override
   protected void OnCreate (Bundle savedinstancestate) {
     Super.oncreate (savedinstancestate);
Setcontentview (r.layout.Activity_main);
     listview= (ListView) Findviewbyid (r.id.List_view);
     Final arrayadapter<string> adapter=New Arrayadapter<string> (This,android. R.layout.Simple_expandable_list_item_1,getdata ());
     Listview.setadapter (adapter);Don't care where the data is coming from only need to set Adapter,adapter will give the ListView data
     Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () {
     @OverrideClick events for items
     public void Onitemclick (adapterview<?> adapterview, view view,int I, long l) {
String txt=adapter.getitem (i);
//Toast.maketext (mainactivity.this,txt,0). Show ();
//Setcontentview (r.layout.activity_play);
}
});
}
private string[] GetData () {
return NE W string[]{ "CCTV2", "CCTV3", "CCTV4", " CCTV6 ", " CCTV7 ", "CCTV8", "CCTV9", }
/span>
< Span style= "color: #000080;" >< Span style= "color: #808080;" >< Span style= "color: #008000;" >
3. Source code Download:Listview.zip

Simple use 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.