Chapter 1 List, adapter, and layout (1) make your program more elegant, Chapter 2 list

Source: Internet
Author: User

Chapter 1 List, adapter, and layout (1) make your program more elegant, Chapter 2 list

Category: C #, Android, VS2015;

Created on:

1. Configure the Application List in the AssemblyInfo. cs File

As mentioned in the previous chapter, in addition to configuring the Application List in the AndroidManifest. xml file, you can also configure the Application List in the AssemblyInfo. cs file.

To configure the Application List in the AssemblyInfo. cs file, use C # To compile the application.

In AssemblyInfo. the advantage of configuring the Application List in the cs file is that, as there is a convenient smart prompt when you type C # code, you can add or modify the configuration list directly in AndroidManifest. xml files are much easier to configure.

In fact, Android manifest can be ignored when C # is used to compile Android applications. xml files, these internal processing processes are all managed by the compiler for you, and the configuration results of the final configuration are the same. However, this configuration method with smart prompts is much easier than directly modifying the AndroidManifest. xml file.

Of course, this is just another way for the VS2015 compiler to provide you with a list of smart configuration Android applications. It is also a common configuration list method for C # programming. However, if you are still used to directly modifying AndroidManifest in projects like Java programming. xml file, and can also tolerate inexplicable errors caused by configuration inconsistency, you can also continue to use the direct modification AndroidManifest. the xml file method, Java programmers have been used to changing the original configuration method, and are also numb to this method. For C # programming, this mental retardation allows programmers to configure this configuration on their own.

In short, I still prefer to go to AssemblyInfo. configure it in the cs file. This method allows you to use smart prompts to intuitively see which configurable options are available, and you can see whether the configuration is wrong at a glance.

Starting from this chapter, we will use this method to configure the list that takes effect throughout the application, instead of directly modifying the AndroidManifest. xml file.

2. Create more flexible custom adapters

In the main interface of the previous chapter, we use the simplest method: directly create a string array to list the example navigation. This method is simple, but it is the least flexible.

In this section, we will learn how to make the content displayed on the main interface more flexible, which is also a common method in actual projects.

1. Where to define the adapter

Generally, the adapter defines the class in the file containing the class. For example, to write an adapter for the MainActivity class, define it in the MainActivity. cs file.

2. Create your own list item class

Create the list items as needed. The following code demonstrates how to create a MyItems class:

Public class MyItems

{

Public string Title {get; set ;}

Public string Desc {get; set ;}

}

3. Tips for creating a custom Adapter

Once you have your own list item (MyItems class), you can specify it in the Custom adapter.

The following demonstrates how to quickly create a custom adapter:

Using System. reflection; using System. runtime. compilerServices; using System. runtime. interopServices; using Android. app; [assembly: AssemblyTitle ("ch07demos")] [assembly: AssemblyDescription ("basic layout control usage")] [assembly: AssemblyConfiguration ("")] [assembly: assemblyCompany ("Mao Yu's blog Paradise (http://www.cnblogs/rainmj/)")] [assembly: AssemblyProduct ("rainmjAndroidDemos")] [assembly: AssemblyCopyright ("Copyright©2016 ")] [assembly: AssemblyTrademark (" ")] [assembly: AssemblyCulture (" zh-CN ")] [assembly: ComVisible (false)] [assembly: application (Theme = "@ android: style/Theme. deviceDefault. light ")] [assembly: AssemblyVersion (" 1. 0. * ")]

4. Modify the Main interface (Main. axml)

Change Main. axml to the following code:

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

5. Modify the main activity file (MainActivity. cs)

The MainActivity. cs code is as follows:

Using Android. app; using Android. views; using Android. widget; using Android. OS; using System. collections. generic; using ch07demos. srcDemos; namespace ch07demos {[Activity (Label = "ch07demos", MainLauncher = true, Icon = "@ drawable/icon")] public class MainActivity: activity {protected override void OnCreate (Bundle bundle) {base. onCreate (bundle); SetContentView (Resource. layout. main); List <MyItems> items = new List <MyItems> () {new MyItems {Title = "Demo01 -- LinearLayout", Desc = "demonstrate the basic usage of linear layout "}, new MyItems {Title = "Demo02 -- GridLayout", Desc = "demonstrate basic grid layout usage"}, new MyItems {Title = "Demo03 -- TableLayout ", desc = "demonstrate basic table layout usage"}, new MyItems {Title = "Demo04 -- RelativeLayout", Desc = "demonstrate basic layout usage "}}; listView listView1 = FindViewById <ListView> (Resource. id. listView1); listView1.Adapter = new MyAdapter (this, items); listView1.ItemClick + = (s, e) =>{ int index = e. position + 1; switch (index) {case 1: StartActivity (typeof (Demo01LinearLayout); break; case 2: StartActivity (typeof (Demo02GridLayout); break; case 3: startActivity (typeof (Demo03TableLayout); break; case 4: StartActivity (typeof (Demo04RelativeLayout); break ;};}} public class MyItems {public string Title {get; set;} public string Desc {get; set;} public class MyAdapter: BaseAdapter <MyItems> {private List <MyItems> items; private Activity context; public override int Count {get {return items. count ;}} public override MyItems this [int position] {get {return items [position] ;}} public MyAdapter (Activity context, List <MyItems> items): base () {this. context = context; this. items = items;} public override long GetItemId (int position) {return position;} public override View GetView (int position, View convertView, ViewGroup parent) {var item = items [position]; View view = null; view = context. layoutInflater. inflate (Android. resource. layout. simpleListItem2, null); view. findViewById <TextView> (Android. resource. id. text1 ). text = item. title; view. findViewById <TextView> (Android. resource. id. text2 ). text = item. desc; return view ;}}}

OK. This is the 1st lecture in this chapter.

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.