package com.example.liebiaoshitu;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int []imageid=new int[]{
R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d
,R.drawable.h,R.drawable.g,R.drawable.f,R.drawable.e
,R.drawable.i
};
String []title=new String[]{
"张三","李四","王五","赵六","孙琦","刘八","陈九","陈1","哈哈"
};
List<Map<String,Object>>listitem=new ArrayList<Map<String,Object>>();
for(int i=0;i<imageid.length;i++){
Map<String,Object>map=new HashMap<String,Object>();
map.put("image",imageid[i]);
map.put("name",title[i]);
listitem.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(this,listitem,R.layout.main,new String[]{
"name","image"},new int[]{R.id.title,R.id.image}
);
ListView listView=findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String,Object> map=(Map<String,Object>) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, map.get("name").toString(), Toast.LENGTH_LONG).show();
}
});
}
}
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="370dp"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:maxHeight="72dp"
android:maxWidth="72dp"
android:adjustViewBounds="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/title"
android:layout_gravity="center"
/>
</LinearLayout>
今天上课挺累的,忙了一天啦,所以决定,不学太多内容啦