Example analysis of ListView usage in Android _android

Source: Internet
Author: User
Tags event listener stub

This article analyzes the usage of ListView in Android. Share to everyone for your reference, specific as follows:

By adding the ListView widget in Layout, you can reach an interactive page that has a list effect on the page layout. Here is an example of how to add ListView to layout and how to apply it.

With the design of the two event Listener:onitemselectedlistener event for the mouse wheel rotation of the selected value, the Onitemclicklistener event is the event that is triggered when the mouse clicks. This allows you to distinguish between the "select" and "click" Differences in the list.

Use Arrayadapter (context context, int Textviewresourceid, t[] objects) This constructor, where Textviewresourceid is defined in "Res/layout/my_ Simple_list_item.xml Resources ResourceID (R.layout.my_simple_list_item), which uses Checkedtextview to get the items selected in the ListView.

The LinearLayout object is used in the program, and the TextView and ListView are dynamically appended into the original layout layout. Use Linearlayout.layoutparams to create the object param1, and then invoke the LinearLayout AddView method to pass TextView and ListView and params objects.

The program looks like this:

public class A07activity extends activity {private static final string[]week= "Sunday", "Monday", "Tuesday", "Wednesday", "
 Thursday "," Friday "," Saturday "};
 Private TextView TV;
 Private ListView LV;
 Private arrayadapter<string> AA;
  Private LinearLayout ll; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Tv=new TextView (this);
    Tv.settext (R.string.title);
    Tv.settextcolor (color.red);
    Lv=new ListView (this);
    Lv.setbackgroundcolor (Color.green);
    Ll=new LinearLayout (this);
    Ll.setorientation (linearlayout.vertical);
    Ll.setbackgroundcolor (Android.graphics.Color.YELLOW);
    Aa=new arrayadapter<string> (A07activity.this,r.layout.my_simple_list,week);
    Lv.setadapter (AA); Linearlayout.layoutparams params01=new linearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
Ll.addview (LV, params01);    Linearlayout.layoutparams params02=new linearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
    Ll.addview (TV, PARAMS02);
    Setcontentview (LL); Lv.setonitemselectedlistener (New Adapterview.onitemselectedlistener () {@Override public void onitemselected ( Adapterview<?> arg0, View arg1, int arg2, long arg3) {//TODO auto-generated Method Stub Tv.settext ("You chose:"
  +arg0.getselecteditem (). toString ());
  @Override public void onnothingselected (adapterview<?> arg0) {//TODO auto-generated Method stub}}); Lv.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (ADAPTERVIEW&LT;?
  > arg0, View arg1, int arg2, long arg3) {//TODO auto-generated Method Stub Tv.settext ("You chose:" +week[arg2));
  }
 });

 }
}

Res/layout/my_simple_list.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <checkedtextview xmlns:android= 
  "http://" Schemas.android.com/apk/res/android "
  android:id=" @+id/mycheckedtextview1 "
  android:layout_width=" Match_ Parent "
  android:layout_height=" match_parent "
  android:orientation=" vertical " 
/>

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Basic Components Usage Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.