[Android container component - AdapterView]

I. Overview:

      In Android application development, AdapterView is a common and very important component. Our common component that displays information in the form of a list is a subclass of AdapterView, called Listview ; the component that we often browse image thumbnails in a grid is also a subclass of AdapterView, called GridView ; it can be displayed in the form of a drop-down list. The options component is also a subclass of AdapterView, called Spinner ; and so on, they are all subclasses of AdapterView.

2. Introduce the programming mode of AdapterView:

     Use Android's ListView component to display all program information installed in the Android system in a list. ListView, as the name suggests, is to display information to users in the form of a list. Just like the list in your phone settings, it contains the icons of all your applications, the application name (similar to the picture below) and the class name of the entry Activity . When the displayed content exceeds the available area of ​​the physical screen, it can also scroll, just like the ScrollView effect we mentioned in the previous issue .

       As shown in the figure below: The part enclosed by the big yellow box is a ListView, and the part enclosed by the small dark blue box is a list item in a list. Therefore, in order to use ListView to display information in the program, some work must be done.

(1) Include a ListView component in the interface layout

(2) Layout the list items displayed in the list

(3) Design a class that implements the Adapter interface to provide the ListView component with the data that needs to be displayed.

Adapter:

     The list component ( ListView ), grid component ( GridView ) and drop-down list component ( Spinner ) just mentioned are all subclasses of Adapter. These components are only responsible for displaying data, and for the data to be displayed, they must be called It is managed by the interface of the Adapter. Taking the use of ListView to display data as an example, the relationship between AdapterView and Adapter interface is as follows:

 

Adapter common methods and their meanings:

method name meaning
int getCount() Returns the total number of data in the dataset to display
Object getItem(int position) Returns the data object at the specified position in the dataset
long getItemId(int position) Returns the ID of the data at the specified location in the dataset

View getView(int position,

View convertView,

ViewGroup parent)

Build the data at the specified location into a component that can be displayed in the AdapterView, and return to the AdapterView for display

ListView uses:

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:stretchColumns="2"
    >

    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
</RelativeLayout>

Select New→Layout resolution file to create

 item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:stretchColumns="2"
    >

    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

 myAdapater.java

package com.example.demo03_22;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class myAdapater extends BaseAdapter {
    private String[] books={"Java程序设计","Android应用开发","oracle数据库管理指南","JavaWeb程序设计","软件工程之系统工程师之路"};
    LayoutInflater inflater;
    int id_item;
    public myAdapater(Context context,int id_item){
        this.id_item=id_item;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return books.length;
    }

    @Override
    public Object getItem(int i) {
        return books[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView tv;
        tv=(TextView) inflater.inflate(id_item,viewGroup,false);
        tv.setText(books[i]);
        return tv;
    }
}

MainActivity.java

package com.example.demo03_22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv=(ListView) this.findViewById(R.id.lv1);
        myAdapater myAdapater=new myAdapater(this, R.layout.item);
        lv.setAdapter(myAdapater);
    }
}

Test Results:

 Improvement: add pictures

activity_main.xml  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:stretchColumns="2"
    >

    <ListView
        android:id="@+id/lv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

 item.xml

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


    <ImageView
        android:id="@+id/book_phone"
        android:layout_width="80dp"
        android:layout_height="80dp"

        />
    <TextView
        android:id="@+id/book_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        />



</LinearLayout>

 myAdapater.java

package com.example.demo03_22;

import android.annotation.SuppressLint;
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;


public class myAdapater extends BaseAdapter {
    private BookItem[] books={new BookItem("陈某人",R.drawable.dog),
            new BookItem("周某人",R.drawable.dog),
            new BookItem("钟某人", R.drawable.dog),
            new BookItem("林某人",R.drawable.dog),
            new BookItem("涛某人",R.drawable.dog)};

    LayoutInflater inflater;
    int id_item;
    public myAdapater(Context context,int id_item){
        this.id_item=id_item;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return books.length;
    }

    @Override
    public Object getItem(int position) {
        return books[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")@Override
    public View getView(int position, View view, ViewGroup parent) {
        LinearLayout LL=(LinearLayout)inflater.inflate(id_item,parent,false)
                ;
        ImageView iv=(ImageView)LL.findViewById(R.id.book_phone);
        iv.setImageResource(books[position].photo);

        TextView tv;
        tv=(TextView)LL.findViewById(R.id.book_name);
        tv.setText(books[position].name);

        return LL;
    }
/**
 *  定义一个图片类
  */
    private class BookItem{
        String name;
        int photo;

        public BookItem(String name,int photo){
            this.name=name;
            this.photo=photo;
        }
    }
}

MainActivity.java

package com.example.demo03_22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv=(ListView) this.findViewById(R.id.lv1);
        myAdapater myAdapater=new myAdapater(this, R.layout.item);
        lv.setAdapter(myAdapater);
        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView textView=(TextView)view.findViewById(R.id.book_name);
        String name=(String)textView.getText();
        String text="确定选择"+name+"今晚打火锅吗";
        Toast.makeText(this,text,Toast.LENGTH_LONG).show();
    }
}

Guess you like

Origin blog.csdn.net/m0_56233309/article/details/123657578