Android Talking about the Usage of ArrayAdapter and SimpleAdapter in ListView

ArrayAdapter

I use a code to explain the general usage of ArrayAdapter. I won't talk about the xml layout. It is definitely necessary to create a ListView control and put an id on the column so that it can be called in the Java file! In fact, the main problem is to use the function of Array Adapter to realize the view we want in the Java file. It can be simply divided into four steps. The first step must be to first obtain the property of the object List View in the Java file, that is, the id. Yes, the second step is to set the content resources you want to display, such as String[]...text content or int[]...photo content, etc. (your dramble) The third step is to configure the adapter you need Now, there are many kinds of adapters, choose according to what you want to use. For example, we are talking about ArrayAdapter in this section. You must choose rrayAdapter as adapter code, such as ArrayAdapter<String>adapter=new ArrayAdapter<String(this,android.R .layout.simple_list_item_1,data); The three parameters in parentheses correspond to: the context is your MainActivity, and the second parameter is some iten template, you can set it yourself or use the one that comes with Android directly, I use this The third parameter is your resource, that is, the parameter of the warrior content you use. The fourth step is to establish a connection between the adaptation and your List View, so as to realize the display of functions!

code show as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:gravity="center"
    android:text="世界百科"
    />
    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

MainActivity.java

package com.example.listview_arrayadapter;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //第一步:先获取ListView的控件资源
        ListView listview=this.findViewById(R.id.lv);
        //第二步准备数据库(Array)
        String [] data={"中国","俄罗斯","美国","英国","法国","印度","韩国","朝鲜","意大利","土耳其","阿达里亚","加拿大",
               "缅甸","日本","老挝","蒙古","乌克兰","泰国"};

        //第三步:配置适配器(ArrayAdapter)3个参数,上下文,xml布局资源,数据库源
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);

        //第四步:将适配器与ListView经行关联
        listview.setAdapter(adapter);
    }
}

final renderings

 SimpleAdapter

I also use a code to show and explain this usage. Both simple adapter and arraryadapter inherit BaseAdapter, which means that they both have the properties of BaseAdapter! If you want to tell the difference between the two, in terms of difficulty, the simple adapter is relatively difficult, because it uses a lot of parameters, and you will be dizzy if you are not careful! The method is basically the same. I want to display the image here. I need to introduce an xml here because here you can customize the layout of your unified style for each image, and inject the id below. Focus on the Java file function implemented. The first step is to get the object of LiatView, and the second step is to set the content (picture or text)

List<Map<String,Object>> data=new ArrayList<Map<String,Object>>(); 
Map<String,Object> map= new HashMap<String,Object>(); 
map.put ("icon", R.drawable.yao); 
data.add(map) 
Map<String, Object> defines a Map collection variable, and then List<Map<String, Object>> defines a List collection variable, which is a map collection; map is one of the values ​​of that list.

List<Map<String,Object> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String,Object>();
  
data.add(map);//map是data中的其中一个值。

In this way, you will get one of the many image resources of the ListView. If you want to get another image and display it, then

map= new HashMap<String,Object>();
map.put ("icon",R.drawable.diaochan);
data.add(map);类推

Notice

When configuring the adapter (5 parameters in brackets are: context, data parameters, item's xml layout file, and name and id)

If you want to display more things, such as adding a title (character name) and message (character introduction) to the side of the photo, you can set two textviews in the xml file you just created, and at the same time in the List< Map<String,Object>> data=new ArrayList<Map<String,Object>>(); Map<String,Object> map= new HashMap<String,Object>();Add map.put ("title," title content "); and map.put ("message", "messgae content");

code show as below:

MainActivity.java

package com.example.listview_simpleadapter;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取对象
        ListView listview =this.findViewById(R.id.lv);
        //设置数据库(你的展示内容)
        List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();//定义了一个List集合变量
        Map<String,Object> map= new HashMap<String,Object>();//定义了一个Map变量
        map.put ("icon",R.drawable.yao);//将其yaotp放入map集合
        data.add(map);//将其map添加到data集合变量中去,map是data中的其中一个值。

        map= new HashMap<String,Object>();
        map.put ("icon",R.drawable.libai);
        data.add(map);

        map= new HashMap<String,Object>();
        map.put ("icon",R.drawable.diaochan);
        data.add(map);
        //配置适配器(以上用到的为SimpleAdapter方法则应选用SimpleAdapter适配器)(括号5个参数分别为:上下文,数据参数,item的xml布局文件,以及名称和id)
        SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.layout_item,new String []{"icon"},new int[]{R.id.icon});

      //将适配器与ListView的控件建立起联系
        listview.setAdapter(adapter);
    }
}

renderings

 Okay, I will explain the difference and efficiency of List View and RecycleView in the next issue!

Guess you like

Origin blog.csdn.net/Abtxr/article/details/123948786