AdapterView of advanced controls and related adapters and data sources

Source: Internet
Author: User

The so-called advanced controls refer to controls that cannot be dragged and used directly. They must be managed and controlled before they can be used. Some corresponding controllers and related datasets are used together. In this article, we will first briefly introduce the AdapterView control in the advanced control. Of course, we also need to know the corresponding controller-Adapter and Data Source DataSource, and the relationship between the three.


We should have heard of or understood the MVC framework. The full name of MVC is Model View Controller, short for model-view-controller, A software design model used to organize code using a method that separates business logic from data display. The assumption of this method is that if the business logic is clustered into a component, in addition, the interface and user interaction around data can be improved and customized without re-writing the business logic. MVC is uniquely developed to map the traditional input, processing, and output functions in a logic. The structure of MVC is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/062H0K26-0.jpg "title =" Capture. JPG "/>

So far, we have understood the principle of this framework. Simply put, we can use controller C) to map dataset M to view V. Let's talk about the relationship between Adapter, AdapterView, and DataSource using this idea:

Adapter -- C Controller)

AdapterView -- V view)

DataSource -- M dataset)


Now that we understand this relationship, we can proceed to the lecture today:

1) Adapter class and its related classes and interfaces;

2) ListView, one of the AdapterView controls;


1. Use of the ArrayAdapter class

Through ArrayAdapter, we can only bind data to the TextView control. For more information, see the following code. Note that I have marked a comment:

1) MainActivity. java

Package com. example. arrayadapter; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends Activity {// declare the view ListView control private ListView lv; // declare the adapter Controller) ArrayAdapter private ArrayAdapter <String> aAdapter; // declare and define the data source private String datas [] = {"Wireless & networks", "Call settings", "Sound", "Display", "Location & security ", "Applications", "Accounts & sync", "Privacy", "CD card & phone storage", "Search", "Language & keyboard", "Voice input & output ", "Accessibility", "Date & Time", "About phone" };@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // The implementation adapter, which includes three parameters: aAdapter = new ArrayAdapter <String> (this, R. layout. cell, datas); lv = (ListView) findViewById (R. id. listView1); // bind data to the view lv by using the setAdapter method of the Adapter. setAdapter (aAdapter );}}

(2) activity_main.xml

<LinearLayout 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"    android:background="#000000"    tools:context=".MainActivity" >    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1"        >    </ListView></LinearLayout>

(3) introduced resource file cell. xml

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="50dip"    android:gravity="center_vertical"    android:id="@+id/tv"    android:textColor="#ffffff"></TextView>

4) The implementation results are as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/062H03538-1.jpg "title =" Capture. JPG "/>


2. Use of SimpleAdapter class

Compared with ArrayAdapter, SimpleAdapter provides more comprehensive functions, not only TextView binding, but also the referenced resource file cell. xml) to bind other controls such as ImageView. Next we will improve the layout above:

1) MainActivity. java

Package com. example. arrayadapter; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. OS. bundle; import android. widget. listView; import android. widget. simpleAdapter; public class MainActivity extends Activity {// declare the view ListView control private ListView lv; // declare the adapter Controller) SimpleAdapter private SimpleAdapter sAdapter; // Declare List and Map to store data in private List <Map <String, Object> lists; private Map <String, Object> maps; // declare and define the data source private String datas [] = {"Wireless & networks", "Call settings", "Sound", "Display", "Location & security ", "Applications", "Accounts & sync", "Privacy", "CD card & phone storage", "Search", "Language & keyboard", "Voice input & output ", "Accessibility", "Date & Time", "About phone"}; private int image [] = {R. drawable. ic_settings_wireless, R. drawable. ic_settings_call, R. drawable. ic_settings_sound, R. drawable. ic_settings_display, R. drawable. ic_settings_location, R. drawable. ic_settings_applications, R. drawable. ic_settings_sync, R. drawable. ic_bt_config, R. drawable. ic_settings_sim, R. drawable. ic_power_system, R. drawable. ic_settings_language, R. drawable. ic_settings_voice_cballs, R. drawable. ic_settings_accessibility, R. drawable. ic_settings_date_time, R. drawable. ic_settings_about}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lv = (ListView) findViewById (R. id. listView1); lists = new ArrayList <Map <String, Object> (); // Add data for (int I = 0; I <image. length; I ++) {maps = new HashMap <String, Object> (); maps. put ("data", datas [I]); maps. put ("image", image [I]); lists. add (maps) ;}// implement the SimpleAdapter, which has five parameters: sAdapter = new SimpleAdapter (this, lists, R. layout. cell, new String [] {"data", "image"}, new int [] {R. id. TV, R. id. iv}); // bind data to the view lv by using the setAdapter method of the Adapter. setAdapter (sAdapter );}}

2) activity_main.java same as above

3) resource file cell. java

<LinearLayout 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"    android:orientation="horizontal"    tools:context=".MainActivity" >    <ImageView        android:layout_width="50dip"        android:layout_height="50dip"        android:id="@+id/iv"/>    <TextView        android:layout_width="fill_parent"        android:layout_height="50dip"        android:gravity="center_vertical"        android:id="@+id/tv"        android:textColor="#ffffff"></TextView>    </LinearLayout>

4) Test Result diagram:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/062H03b2-2.jpg "title =" Capture. JPG "/>


3. Use the custom Adapter-BaseAdapter:

If the adapter provided by the system cannot meet the requirements of developers, we can also use BaseAdapter to customize the adapter that meets the requirements, to illustrate the situation, we use the BaseAdapter custom adapter named MyAdapter) to complete the same functions as above.

1) MyAdapter. java creates a class to implement a custom Adapter)

Package myAdapter; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. linearLayout; import android. widget. textView; import com. example. arrayadapter. r; public class MyAdapter extends BaseAdapter {private Context context; private String data []; private int image []; public MyAdapter (Context context, String [] data, int [] image) {this. context = context; this. data = data; this. image = image;} // rewrite the four methods and modify the return value @ Override public int getCount () {return this. data. length ;}@ Override public Object getItem (int position) {return data [position] ;}@ Override public long getItemId (int position) {return position ;} // This rewriting method is the most important. It is used to load the View @ Override public View getView (int position, View convertView, ViewGroup parent) {LayoutInflater lif = (LayoutInflater) context. getSystemService (context. LAYOUT_INFLATER_SERVICE); LinearLayout layout = (LinearLayout) lif. inflate (R. layout. cell, null); TextView TV = (TextView) layout. findViewById (R. id. TV); ImageView iv = (ImageView) layout. findViewById (R. id. iv); TV. setText (data [position]); iv. setBackgroundResource (image [position]); return layout ;}}

2) MainActivity. java

Package com. example. arrayadapter; import myAdapter. myAdapter; import android. app. activity; import android. OS. bundle; import android. widget. listView; public class MainActivity extends Activity {// declare the view ListView control private ListView lv; // declare the adapter Controller) MyAdapter private MyAdapter mAdapter; // declare and define the data source private String datas [] = {"Wireless & networks", "Call settings", "Sound", "Display", "Location & security ", "Applications", "Accounts & sync", "Privacy", "CD card & phone storage", "Search", "Language & keyboard", "Voice input & output ", "Accessibility", "Date & Time", "About phone"}; private int image [] = {R. drawable. ic_settings_wireless, R. drawable. ic_settings_call, R. drawable. ic_settings_sound, R. drawable. ic_settings_display, R. drawable. ic_settings_location, R. drawable. ic_settings_applications, R. drawable. ic_settings_sync, R. drawable. ic_bt_config, R. drawable. ic_settings_sim, R. drawable. ic_power_system, R. drawable. ic_settings_language, R. drawable. ic_settings_voice_cballs, R. drawable. ic_settings_accessibility, R. drawable. ic_settings_date_time, R. drawable. ic_settings_about}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lv = (ListView) findViewById (R. id. listView1); // implement the MyAdapter Adapter mAdapter = new MyAdapter (this, datas, image); // bind data to the view lv through the setAdapter method of the Adapter. setAdapter (mAdapter );}}

3) running effect:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/062H06312-3.jpg "title =" Capture 2.JPG"/>

4) add another question: the buffer of ListView.

Let's take a look at the MyAdapter. java code:

public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder;        if (convertView == null) {            convertView = LayoutInflater.from(context).                    inflate(R.layout.cell,null);            holder = new ViewHolder();            holder.tv = (TextView) convertView.findViewById(R.id.tv);            holder.iv = (ImageView) convertView.findViewById(R.id.iv);            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        holder.tv.setText(data[position]);        holder.iv.setBackgroundResource(image[position]);        return convertView;    }    private static class ViewHolder {        TextView tv;        ImageView iv;    }


This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1281143

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.