數(shù)組適配器(ArrayAdapter)
- 需要傳的參數(shù)類(lèi)型
-
實(shí)現(xiàn)效果:
效果圖 需要的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();//獲取系統(tǒng)資源對(duì)象
arrCity = resources.getStringArray(R.array.city);//通過(guò)資源對(duì)象獲取資源
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文件下創(chuàng)建一個(gè)存放數(shù)組類(lèi)型的xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="city">
<item>北京</item>
<item>廣州</item>
<item>成都</item>
</string-array>
</resources>
簡(jiǎn)單適配器(SimpleAdapter)
-
參數(shù)類(lèi)型
參數(shù)類(lèi)型 - 需要注意的幾點(diǎn)
- List<? extends Map<String,?>> data :泛型
HashMap繼承Map
?:代表任意類(lèi)型(object)
定義:
private List<HashMap<String,Object>> data =new ArrayList<>();- 第三個(gè)參數(shù),為L(zhǎng)ist的layout,需要在layout文件夾中添加對(duì)應(yīng)的layout
- 第四個(gè)參數(shù)列表和第五個(gè)參數(shù)列表應(yīng)一一對(duì)應(yīng)
-
實(shí)現(xiàn)效果
效果圖
- 需要的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<>();
//鍵值對(duì),Key:為String類(lèi)型,value:為任何類(lèi)型
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所對(duì)應(yīng)的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所對(duì)應(yīng)的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>



