数组适配器(ArrayAdapter)
- 需要传的参数类型
-
实现效果:
效果图 需要的java代码:
package com.example.demo2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView lv_city;
private String[] arrCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources resources = getResources();//获取系统资源对象
arrCity = resources.getStringArray(R.array.city);//通过资源对象获取资源
lv_city= findViewById(R.id.lv_city);
//定义适配器
ArrayAdapter<String> adapter=new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
arrCity
);
lv_city.setAdapter(adapter);
lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, arrCity[i], Toast.LENGTH_LONG).show();
}
});
}
- 需要的xml代码
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv_city"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
- 在value文件下创建一个存放数组类型的xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="city">
<item>北京</item>
<item>广州</item>
<item>成都</item>
</string-array>
</resources>
简单适配器(SimpleAdapter)
-
参数类型
参数类型 - 需要注意的几点
- List<? extends Map<String,?>> data :泛型
HashMap继承Map
?:代表任意类型(object)
定义:
private List<HashMap<String,Object>> data =new ArrayList<>();- 第三个参数,为List的layout,需要在layout文件夹中添加对应的layout
- 第四个参数列表和第五个参数列表应一一对应
-
实现效果
效果图
- 需要的java代码
package com.example.demo2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FirstActivity extends AppCompatActivity {
private ListView lv_person;
private List<HashMap<String,Object>> data =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
for (int i=0;i<5;i++){
HashMap<String,Object> map=new HashMap<>();
//键值对,Key:为String类型,value:为任何类型
map.put("head",R.mipmap.ic_launcher);
map.put("name","冯"+i);
map.put("info","哈哈哈哈"+i);
map.put("time","20:0"+i);
data.add(map);
}
lv_person=findViewById(R.id.lv_person);
SimpleAdapter adapter=new SimpleAdapter(
this,
data,
R.layout.item,
new String[]{"head","name","info","time"},
new int[]{R.id.iv_head,R.id.tv_name,R.id.tv_info,R.id.tv_time}
);
lv_person.setAdapter(adapter);
}
}
- 需要的java所对应的xml代码
<?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"
android:orientation="vertical"
tools:context=".FirstActivity">
<ListView
android:id="@+id/lv_person"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
- 需要的Hashmap所对应的xml代码
<?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="wrap_content"
android:padding="5dp">
<ImageView
android:id="@+id/iv_head"
android:src="@mipmap/ic_launcher"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/iv_head"
android:layout_marginLeft="20dp"
android:id="@+id/tv_name"
android:text="掌声"
android:textSize="20sp"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/iv_head"
android:layout_alignBottom="@id/iv_head"
android:layout_marginLeft="20dp"
android:id="@+id/tv_info"
android:text="哈哈哈哈哈"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="14sp"
android:text="20:00"
android:id="@+id/tv_time"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>



