工程目录:
MainActivity
package com.example.demo_four;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ListView listView;
Button btn1,btn2,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listView);
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
btn3=findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int se=v.getId();
switch(se){
case R.id.btn1:
List<String> data1=new ArrayList<String>();
data1.add("北京");
data1.add("上海");
data1.add("武汉");
final ArrayAdapter<String> adapter1=new ArrayAdapter<String>(
MainActivity.this,
R.layout.item_layout1,
R.id.tv,
data1
);
listView.setAdapter(adapter1);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String mess=adapter1.getItem(position)+"";
Toast.makeText(MainActivity.this, mess, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn2:
List<Map<String,Object>> data2=new ArrayList<Map<String,Object>>();
Map<String,Object> listItem1=new HashMap<String,Object>();
listItem1.put("id",1);
listItem1.put("name","张三");
listItem1.put("salary",5000);
data2.add(listItem1);
Map<String,Object> listItem2=new HashMap<String,Object>();
listItem2.put("id",2);
listItem2.put("name","李四");
listItem2.put("salary",5800);
data2.add(listItem2);
Map<String,Object> listItem3=new HashMap<String,Object>();
listItem3.put("id",3);
listItem3.put("name","王五");
listItem3.put("salary",5500);
data2.add(listItem3);
final SimpleAdapter adapter2=new SimpleAdapter(
MainActivity.this,
data2,
R.layout.item_layout2,
new String[]{
"id",
"name",
"salary"
},
new int[]{
R.id.tv1,
R.id.tv2,
R.id.tv3
}
);
listView.setAdapter(adapter2);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String,Object> rec=(Map<String,Object>)adapter2.getItem(position);
String result=" "+position+" "+rec.get("id")+" "+rec.get("name")+" "+rec.get("salary");
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn3:
String[] names=new String[]{
"弄玉",
"李清照",
"李白"
};
String[] descs=new String[]{
"一个擅长音乐的女孩",
"一个擅长文学的女性",
"浪漫主义诗人"
};
int[] imageIds=new int[]{
R.drawable.nongyu,
R.drawable.liqingzhao,
R.drawable.libai
};
final List<Map<String,Object>> data3=new ArrayList<Map<String,Object>>();
for(int i=0;i<names.length;i++){
Map<String,Object> listItem=new HashMap<String,Object>();
listItem.put("header",imageIds[i]);
listItem.put("personName",names[i]);
listItem.put("desc",descs[i]);
data3.add(listItem);
}
final BaseAdapter adapter3=new BaseAdapter(){
@Override
public int getCount() {
return data3.size();
}
@Override
public Object getItem(int position) {
return data3.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item=View.inflate(getApplicationContext(),R.layout.item_layout3,null);
ImageView header=item.findViewById(R.id.header);
TextView name=item.findViewById(R.id.name);
TextView desc=item.findViewById(R.id.desc);
Map<String,Object> p=data3.get(position);
header.setImageResource((int)p.get("header"));
name.setText(p.get("personName")+"");
desc.setText(p.get("desc")+"");
return item;
}
};
listView.setAdapter(adapter3);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String,Object> p=(Map<String,Object>)adapter3.getItem(position);
String result=p.get("personName")+"";
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="ArrayAdapter" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="SimpleAdapter" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="BaseAdapter" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
item_layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
item_layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
item_layout3.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="80sp"
android:maxHeight="80sp"
tools:srcCompat="@tools:sample/avatars" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
演示: