###

###

1. When the program runs, MainActivity will be entered to load PXDListView; 2.PXDListView encapsulates the ListView, which encapsulates the operations related to the Adapter (see the code below) 3. Create the DataManager class to manage data: To manage data: store parsed data, load data, save data, change data. 4. Create a FriendItem class to manage each view. 5. Create a FriendModle class to manage each data model. 6. Create the DataUtils class to manage data related tools

###

【 PXDListView. Java 】

package com.example.a18_listview_icon_name; import android.content.Context; import android.provider.ContactsContract; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; Public class LJRListView extends ListView {public LJRListView(Context) {public LJRListView(Context) { super(context); initData(); } public LJRListView(Context context, AttributeSet attrs) { super(context, attrs); initData(); } // Initialize private voidinitData(){// Set the adaptersetAdapter()
        setAdapter(new MyAdapter()); } private class MyAdapter extends BaseAdapter {@override public intgetCount() {// Get the number of datareturnDataManager.sharedManager.getDataSource().size(); } @override public Object getItem(int position) {// Obtain the data ObjectreturnDataManager.sharedManager.getDataSource().get(position); } @override public long getItemId(int position) {// Obtain the object IDreturnposition; } @Override public View getView(int position, View convertView, ViewGroup parent) {/ / to get this item corresponding to the data model of FriendModle modle = DataManager. SharedManager. GetDataSource () get (position); FriendItem = new FriendItem(getContext(),modle);returnitem.rootView; }}}Copy the code

【 DataManager. Java 】

package com.example.a18_listview_icon_name; import java.util.ArrayList; /** * Is used to manage data: store parsed data, load data, save data, change data * To ensure that this object has only one: singleton mode ** singleton mode: * 1. Public class DataManager {// Save all data private ArrayList<FriendModle> dataSource; // Disable the constructor privateDataManager(){// call loadData(); Public static final DataManager sharedManager = new DataManager(); / /setPublic ArrayList<FriendModle>getDataSource() {
        returndataSource; } /** * private voidloadDataDataSource = DataUtils. LoadCustomData (); // Call loadCustomData(). }}Copy the code

【 FriendItem. Java 】

package com.example.a18_listview_icon_name; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; /** * public class FriendItem {private FriendModle modle; Public View rootView; // Save Context private Context Context; Public FriendItem(Context Context,FriendModle modle) {this.modle = modle; this.context = context; initView(); } private voidinitViewViewGroup v = (ViewGroup) view.inflate (context, r.layout.layout_qq_friend,null); // Display data to the corresponding control of the view container // Get the image subview ImageView icon = v.findViewWithTag(context.getApplicationContext().getString(R.string.iconTag)); TextView name = v.finViewWithTag (context.getResources().getString(r.string.nameTag)); / / / / / / get data model FriendModle modle = DataManager. SharedManager. GetDataSource () get (position); // Display data icon.setimageresource (modle.iconid); name.setText(modle.name); rootView = v; }}Copy the code

【 FriendModle. Java 】

package com.example.a18_listview_icon_name; import android.graphics.Paint; Public class FriendModle {// avatar resource id public int iconID; // Friend nickname public String name; Public FriendModle(int iconID, String name) {this.iconID = iconID; this.name = name; }}Copy the code

【 DataUtils. Java 】

package com.example.a18_listview_icon_name; import java.util.ArrayList; /** * Public static method (); /** * public static method (); /** * Public static method (); Public class DataUtils {/** * public static ArrayList<FriendModle>loadCustomDataFriendlist <FriendModle> friends = new ArrayList<>();for(int i = 0; i < 30; I++){FriendModle modle = new FriendModle(r.rawable.icon,"Android combat"); // Add the array friends.add(modle); }returnfriends; } /** * local load (file) */ public static ObjectloadFromFile() {returnnull; } /** * public static ObjectloadFromSql() {returnnull; } /** * Public static ObjectloadFromServer() {returnnull; }}Copy the code

【 MainActivity. Java 】

package com.example.a18_listview_icon_name;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {

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

【 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"> <! <com.example. A18_listview_icon_name.LJRListView Android :id="@+id/lv_qqlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>
Copy the code

【 layout_qq_friend. XML 】

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

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="20dp"
        android:tag="@string/iconTag"
        android:scaleType="fitXY"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:gravity="center_vertical"
        android:textSize="30sp"
        android:textColor="# 000"
        android:tag="@string/nameTag"/>

</LinearLayout>
Copy the code