A Brief Introduction to ListView and Adapter data adapters, listviewadapter

Source: Internet
Author: User

A Brief Introduction to ListView and Adapter data adapters, listviewadapter

 

ListView

Display a large number of data in the same format

Common attributes:

The Drawable of listSelector listView in different states, such as selection and pressing.

Divider ListView interval Drawable between each item

DividerHeight the interval height between each item in ListView

    

 

Common Methods:

SetAdapter () set the data adapter

SetOnItemClickListener () sets the listener for each click event

AddHeaderView () Add a header View

AddFooterView () Add a foot view

SetEmptyView (): Empty data view when the data item is set to 0

Listen to events

The example of this listener is to set a subscript change for a header view item so get the correct object with the subscript position of the position-listview.getHeaderViewsCount () calculated item

      

 

 

 

 

The Adapter data Adapter binds various types of data to controls, such as listview, gridview, and spinner, in an appropriate manner and uses the Adapter to bind data.

Three adapters

ArrayAdapter: Supports generic operations. The simplest Adapter is to display only one line of text.

SimpleAdapter: An Adapter with good scalability, which can be customized for multiple effects

BaseAdapter: Abstract class. In actual development, we will inherit this class and rewrite related methods. The most used Adapter

 

First, a brief introduction

1. ArrayAdapter

  

Parameter 1: current context

Parameter 2: android. R. layout. simple_list_item_1

It is a simple layout that comes with android. It only contains one TextView with the id of text1, that is, parameter 3. ArrayAdapter can only display one line of data to display text information. Use the layout provided by this system.

The android system also provides us with some other useful templates.

Simple_list_item_1: Text box with a single line

Simple_list_item_2: Composed of two text boxes

Simple_list_item_checked: Each item is composed of an selected list item.

Imple_list_item_multiple_choice: All contain a check box

Simple_list_item_single_choice: All have a single-choice button

Parameter 4: The Bound data is used here as a string array. As mentioned above, ArrayAdapter supports generics and can also be bound to a list.

The display effect is bound to the listview.

 

 

2. SimpleAdapter

SimpleAdapter has the best scalability. It can define various la S, put ImageView (image), Button, CheckBox, and so on.

  

Display result

  

The layout referenced here is the system defaultSimple_list_item_2

The display effect of the listView item is displayed based on your layout. You can write a lot of cool information.

 

3. BaseAdapter

BaseAdapter is the most commonly used adapter ArrayAdapter in development. SimpleAdapter inherits from BaseAdapter. The BaseAdapter can complete its own defined Adapter, and display any complicated combination of data and resources in any way you want.

After inheriting the BaseAdapter, you must override the following four methods: getCount, getItem, getItemId, and getView.

Before creating a ListView, the system calls the getCount method to obtain the number of items. Each time an Item is drawn, the getView method is called to reference the previously defined layout in getView to determine the Display Effect and return a View object as an Item for display.

These two methods are the most important in customizing the Display Effect of ListView. At the same time, as long as the two methods are overwritten, ListView can be fully displayed as required by developers. The getItem and getItemId methods will be called when the ListView response method is called.

 

Custom Layout file (Display Effect of listview items)

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/image_photo"        android:layout_width="70dp"        android:layout_height="70dp"        android:padding="10dp"/>    <TextView        android:id="@+id/name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:layout_toRightOf="@id/image_photo"/>    <TextView        android:id="@+id/age"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:layout_toRightOf="@id/image_photo"        android:layout_below="@id/name"        android:layout_marginTop="10dp"/></RelativeLayout>

 

Student students

public class Student {    private String name;    private int age;    private int photo;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public int getPhoto() {        return photo;    }    public void setPhoto(int photo) {        this.photo = photo;    }}

 

MainActivity

Public class MainActivity extends AppCompatActivity {private ListView listView; // custom BaseAdapter private MyAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. listview); List <Student> stuList = new ArrayList <> (); for (int I = 0; I <10; I ++) {Student stu = new Student (); stu. setAge (10 + I); stu. setName ("name" + I); stu. setPhoto (R. mipmap. ic_launcher); stuList. add (stu);} adapter = new MyAdapter (stuList, MainActivity. this); listView. setAdapter (adapter );}

 

MyAdapter

Public class MyAdapter extends BaseAdapter {private List <Student> stuList; private LayoutInflater inflater; public MyAdapter () {} public MyAdapter (List <Student> stuList, Context context) {this. stuList = stuList; this. inflater = LayoutInflater. from (context) ;}@ Override public int getCount () {return stuList = null? 0: stuList. size () ;}@ Override public Student getItem (int position) {return stuList. get (position) ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {// load the layout into a View view = inflater. inflate (R. layout. layout_student_item, null); Student student = getItem (position); // In the view, find the ImageView image_photo = (ImageView) view control with the id of image_photo. findViewById (R. id. image_photo); TextView TV _name = (TextView) view. findViewById (R. id. name); TextView TV _age = (TextView) view. findViewById (R. id. age); image_photo.setImageResource (student. getPhoto (); TV _name.setText (student. getName (); TV _age.setText (String. valueOf (student. getAge (); return view ;}

 

Display result

 

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.