Simple use of Android ListView ArrayAdapter, androidarrayadapter

Source: Internet
Author: User

Simple use of Android ListView ArrayAdapter, androidarrayadapter

I wrote three articles about android in the previous section. The demo programs are all written in a project. At that time, three buttons were placed in the MainActivity on the startup page for convenience of testing, click different buttons to go to different sample programs. The MainActivity interface is as follows:

According to the above design, every time you write a demo program, you need to add a button in MainActivity and write a click event for it. This is a little troublesome, so I want to improve it, by the way, use ListView and ArrayAdapter. The improved startup page is shown in:

A new MainActivity2 is created, and a ListView is used to display the list of demo programs. Click the corresponding list item to go to the corresponding demo page. The function is the same as that of the previous MainActivity, however, you do not need to add buttons and click events. You only need to add a line of code for each demo page: catalogs. add (new Catalog ("demo name", Activity to jump. class). The Code is as follows.

The MainActivity2 layout file is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/lvCatalog"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </ListView></LinearLayout>

The MainActivity2 Java code is as follows:

Package chengyujia. demo. aty; import java. util. arrayList; import java. util. list; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listView; import chengyujia. demo. r; import chengyujia. demo. model. catalog; public class MainActivity2 e Xtends BaseActivity {private ListView lvCatalog; private List <Catalog> catalogs; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main2); lvCatalog = (ListView) findViewById (R. id. lvCatalog); init ();} private void init () {catalogs = new ArrayList <Catalog> (); // Add entries to the home directory. In the future, each example is written, add an entry here. // Unlike every previous example in MainActivity, you need to add a button and write the corresponding click event. Catalogs. add (new Catalog ("screen direction", OrientationActivity. class); catalogs. add (new Catalog ("call", CallActivity. class); catalogs. add (new Catalog ("version information", VersionActivity. class); // The ListView adapter selects the ArrayAdapter, And the simple_list_item_1 of the system is used for the layout of each item in the ListView. ArrayAdapter <Catalog> adapter = new ArrayAdapter <Catalog> (this, android. r. layout. simple_list_item_1, catalogs); lvCatalog. setAdapter (adapter); // you can use the onItemClick method of the anonymous class that implements the OnItemClickListener interface to process click events for each item in the ListView. LvCatalog. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {startActivity (new Intent (MainActivity2.this, catalogs. get (position). cls ));}});}}

A custom Model class Catalog is also used to indicate the data bound to each item in the list. The Code is as follows:

Package chengyujia. demo. model; import android. app. activity; // home page directory list item public class Catalog {// entry name public String name; // click the type public Class of the target Activity to jump to by entry <? Extends Activity> cls; // write a constructor with parameters to facilitate value assignment. Public Catalog (String name, Class <? Extends Activity> cls) {this. name = name; this. cls = cls;} // when ListView uses ArrayAdapter <T>, the content displayed for each list item is the value returned by T's toString method, // if this parameter is not overwritten, The toString method of the parent class Object will be called. @ Override public String toString () {return name ;}}

Note that the toString method must be rewritten in the Catalog class. Otherwise, each item in the ListView displays a composite string of "object class name + @ + hashCode.

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.