📜  Android中的CustomArrayAdapter示例

📅  最后修改于: 2021-05-09 18:23:52             🧑  作者: Mango

在Android中,ArrayAdapters用于填充和控制ListView和Spinners。默认情况下,ArrayAdapter提供的ListItems仅包含单个信息或单个TextView。为了拥有更复杂的布局,在单个ListItem中包含多个信息,例如图像,文本等,我们使用CustomArrayAdapter 。 Instagram,WhatsApp等应用程序正使用这种复杂的布局。

例子

在此示例中,我们将为具有不同版本的Android OS的应用程序制作一个应用程序,该应用程序具有一个ImageView和一个显示版本名称的TextView。下面给出了一个示例GIF,以使我们对本文中要做的事情有一个了解。注意,我们将使用Java语言实现该项目。

Android中的CustomArrayAdapter

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。 。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

在这一步中,我们将一个ListView添加到我们的activity_main.xml文件中,该文件用于显示listItems的数据。转到应用程序> res>布局> activity_main.xml和以下代码段。

XML


  
    
  


XML
 
    #0F9D58 
    #16E37F 
    #03DAC5 


XML


  
    
    
  
    
  
        
  
        
    
  


Java
public class Item {
  
    private int androidVersionImage;
    private String androidVersionName;
    private String androidVersionNumber;
  
    // Constructor
    public Item(int androidVersionImage, String androidVersionName, String androidVersionNumber) {
        this.androidVersionImage = androidVersionImage;
        this.androidVersionName = androidVersionName;
        this.androidVersionNumber = androidVersionNumber;
  
    }
  
    // Getters and Setters method
    public int getAndroidVersionImage() {
        return androidVersionImage;
    }
  
    public void setAndroidVersionImage(int androidVersionImage) {
        this.androidVersionImage = androidVersionImage;
    }
  
    public String getAndroidVersionName() {
        return androidVersionName;
    }
  
    public void setAndroidVersionName(String androidVersionName) {
        this.androidVersionName = androidVersionName;
    }
  
    public String getAndroidVersionNumber() {
        return androidVersionNumber;
    }
  
    public void setAndroidVersionNumber(String androidVersionNumber) {
        this.androidVersionNumber = androidVersionNumber;
    }
}


Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
  
public class Adapter extends ArrayAdapter  {
  
    ImageView imageView;
    TextView textView1, textView2;
    ArrayList  androidVersionList = new ArrayList <>();
  
    public Adapter(Context context, int textViewResourceId, ArrayList  objects) {
        super(context, textViewResourceId, objects);
        androidVersionList = objects;
    }
  
    // Returns total number of items to be displayed in the list.
    // It counts the value from the arraylist size
    @Override
    public int getCount() {
        return super.getCount();
    }
  
    // This function implicitly gets called when the listItem view is ready
    // to be displayed. Here we set the layout and add data to the views
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View view = convertView;
          
          // Setting layout
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_item, null);
        imageView = (ImageView) view.findViewById(R.id.androidVersionImage);
        textView1 = (TextView) view.findViewById(R.id.androidVersionName);
        textView2 = (TextView) view.findViewById(R.id.androidVersionNumber);
          
           // Adding data to the Views
        imageView.setImageResource(androidVersionList.get(position).getAndroidVersionImage());
        textView1.setText(androidVersionList.get(position).getAndroidVersionName());
        textView2.setText(androidVersionList.get(position).getAndroidVersionNumber());
        return view;
    }
}


Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
  
public class MainActivity extends AppCompatActivity {
  
    ListView listView;
    ArrayList  androidVersionList = new ArrayList <>();
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
          // Getting the reference of listView
        listView = (ListView) findViewById(R.id.listView);
          
          // Adding image and texts to list
        androidVersionList.add(new Item(R.drawable.donut, "Donut", "1.6"));
        androidVersionList.add(new Item(R.drawable.eclair, "Eclair", "2.0 - 2.1"));
        androidVersionList.add(new Item(R.drawable.froyo, "Froyo", "2.2 - 2.2.3"));
        androidVersionList.add(new Item(R.drawable.gingerbread, "GingerBreak", "2.3 - 2.3.7"));
        androidVersionList.add(new Item(R.drawable.honeycomb, "HoneyComb", "3.0 - 3.2.6"));
        androidVersionList.add(new Item(R.drawable.icecream, "IceCream", "4.0 - 4.0.4"));
        androidVersionList.add(new Item(R.drawable.jellybean, "JellyBean", "4.1 - 4.3.1"));
        androidVersionList.add(new Item(R.drawable.kitkat, "KitKat", "4.4 - 4.4.4"));
        androidVersionList.add(new Item(R.drawable.lollipop, "Lollipop", "5.0 - 5.1.1"));
        androidVersionList.add(new Item(R.drawable.marshmallow, "Marshmallow", "6.0 - 6.0.1"));
  
        Adapter adapter = new Adapter(this, R.layout.list_item, androidVersionList);
          
          // Setting the adapter to list
        listView.setAdapter(adapter);
    }
}


在继续之前,让我们添加一些颜色属性以增强应用程序栏。转到应用程序> res>值> colors.xml并添加以下颜色属性。

XML格式

 
    #0F9D58 
    #16E37F 
    #03DAC5 
 

步骤3:创建新的布局文件list_item.xml

在此步骤中,我们将为单个列表项视图创建一个布局文件。转到应用程序> res>布局>右键单击>新建>布局资源文件,并将其命名为list_itemlist_item.xml包含一个ImageView和一个TextView,用于填充ListView。

XML格式



  
    
    
  
    
  
        
  
        
    
  

步骤4:创建一个新的Java类Item。Java

我们将创建一个新的Java类,并将其命名为Item 。物品。 Java包含三个私有成员变量androidVersionImage,androidVersionName,androidVersionNumber。稍后,我们将创建此Item类型的ArrayList。转到应用程序> Java >程序包>右键单击>创建新的Java类

Java

public class Item {
  
    private int androidVersionImage;
    private String androidVersionName;
    private String androidVersionNumber;
  
    // Constructor
    public Item(int androidVersionImage, String androidVersionName, String androidVersionNumber) {
        this.androidVersionImage = androidVersionImage;
        this.androidVersionName = androidVersionName;
        this.androidVersionNumber = androidVersionNumber;
  
    }
  
    // Getters and Setters method
    public int getAndroidVersionImage() {
        return androidVersionImage;
    }
  
    public void setAndroidVersionImage(int androidVersionImage) {
        this.androidVersionImage = androidVersionImage;
    }
  
    public String getAndroidVersionName() {
        return androidVersionName;
    }
  
    public void setAndroidVersionName(String androidVersionName) {
        this.androidVersionName = androidVersionName;
    }
  
    public String getAndroidVersionNumber() {
        return androidVersionNumber;
    }
  
    public void setAndroidVersionNumber(String androidVersionNumber) {
        this.androidVersionNumber = androidVersionNumber;
    }
}

步骤5:创建适配器类

现在,我们将创建一个Adapter类,该类充当UI组件和数据源.ie,androidVersionImage,androidVersionName,androidVersionNumber和ListView之间的桥梁。转到应用程序> Java >程序包>右键单击并创建一个新的Java类,并将其命名为Adapter。下面是为此提供的代码段。

Java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
  
public class Adapter extends ArrayAdapter  {
  
    ImageView imageView;
    TextView textView1, textView2;
    ArrayList  androidVersionList = new ArrayList <>();
  
    public Adapter(Context context, int textViewResourceId, ArrayList  objects) {
        super(context, textViewResourceId, objects);
        androidVersionList = objects;
    }
  
    // Returns total number of items to be displayed in the list.
    // It counts the value from the arraylist size
    @Override
    public int getCount() {
        return super.getCount();
    }
  
    // This function implicitly gets called when the listItem view is ready
    // to be displayed. Here we set the layout and add data to the views
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View view = convertView;
          
          // Setting layout
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_item, null);
        imageView = (ImageView) view.findViewById(R.id.androidVersionImage);
        textView1 = (TextView) view.findViewById(R.id.androidVersionName);
        textView2 = (TextView) view.findViewById(R.id.androidVersionNumber);
          
           // Adding data to the Views
        imageView.setImageResource(androidVersionList.get(position).getAndroidVersionImage());
        textView1.setText(androidVersionList.get(position).getAndroidVersionName());
        textView2.setText(androidVersionList.get(position).getAndroidVersionNumber());
        return view;
    }
}

步骤6:使用MainActivity。 Java文件

在MainActivity中。在Java类中,我们创建了一个ArrayList来存储图像和文本。这些图像放置在drawable文件夹中( app> res> drawable )。您可以使用任何图像代替它。我们获取listView的引用,并在listView上设置适配器。

Java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
  
public class MainActivity extends AppCompatActivity {
  
    ListView listView;
    ArrayList  androidVersionList = new ArrayList <>();
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
          // Getting the reference of listView
        listView = (ListView) findViewById(R.id.listView);
          
          // Adding image and texts to list
        androidVersionList.add(new Item(R.drawable.donut, "Donut", "1.6"));
        androidVersionList.add(new Item(R.drawable.eclair, "Eclair", "2.0 - 2.1"));
        androidVersionList.add(new Item(R.drawable.froyo, "Froyo", "2.2 - 2.2.3"));
        androidVersionList.add(new Item(R.drawable.gingerbread, "GingerBreak", "2.3 - 2.3.7"));
        androidVersionList.add(new Item(R.drawable.honeycomb, "HoneyComb", "3.0 - 3.2.6"));
        androidVersionList.add(new Item(R.drawable.icecream, "IceCream", "4.0 - 4.0.4"));
        androidVersionList.add(new Item(R.drawable.jellybean, "JellyBean", "4.1 - 4.3.1"));
        androidVersionList.add(new Item(R.drawable.kitkat, "KitKat", "4.4 - 4.4.4"));
        androidVersionList.add(new Item(R.drawable.lollipop, "Lollipop", "5.0 - 5.1.1"));
        androidVersionList.add(new Item(R.drawable.marshmallow, "Marshmallow", "6.0 - 6.0.1"));
  
        Adapter adapter = new Adapter(this, R.layout.list_item, androidVersionList);
          
          // Setting the adapter to list
        listView.setAdapter(adapter);
    }
}

输出:在模拟器上运行

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!