代码块:
public class MainActivity extends AppCompatActivity {
int[] imgs={R.mipmap.img001,R.mipmap.img002,R.mipmap.img003,R.mipmap.img004,R.mipmap.img005,
R.mipmap.img001,R.mipmap.img002,R.mipmap.img003,R.mipmap.img004,R.mipmap.img005};
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv);
final List<Map<String,Object>> list=new ArrayList<>();
for (int i=0;i<10;i++){
Map<String,Object> map= new HashMap<>();
map.put("name","我是"+i);
map.put("img",imgs[i]);
map.put("phone","111222"+i);
list.add(map);
}
MyAdapter adapter=new MyAdapter(this,list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){//第一个同学
new AlertDialog.Builder(MainActivity.this).setMessage(list.get(position).get("name").toString()).show();
}else if(position==1){//第二个同学
Calendar calendar=Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}else if(position==2){//第三个同学
View inflate = View.inflate(MainActivity.this, R.layout.list_item, null);
ImageView iv=(ImageView) inflate.findViewById(R.id.tv_iv);
TextView tv_name=(TextView)inflate.findViewById(R.id.tv_name);
TextView tv_phone=(TextView)inflate.findViewById(R.id.tv_phone);
iv.setImageResource((int)list.get(position).get("img"));
tv_name.setText(list.get(position).get("name").toString());
tv_phone.setText(list.get(position).get("phone").toString());
new AlertDialog.Builder(MainActivity.this).setView(inflate).show();
}
}
});
}
}
适配器:
public class MyAdapter extends BaseAdapter {
Context context;
List<Map<String,Object>> list;
public MyAdapter(Context context, List<Map<String,Object>> list){
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Viewholder vh;
if(convertView==null){
convertView=View.inflate(context,R.layout.list_item,null);
vh=new Viewholder();
vh.tv_name=(TextView) convertView.findViewById(R.id.tv_name);
vh.tv_phone=(TextView)convertView.findViewById(R.id.tv_phone);
vh.tv_iv=(ImageView)convertView.findViewById(R.id.tv_iv);
convertView.setTag(vh);
}else {
vh=(Viewholder)convertView.getTag();
}
vh.tv_name.setText(list.get(position).get("name").toString());
vh.tv_phone.setText(list.get(position).get("phone").toString());
vh.tv_iv.setImageResource((int)list.get(position).get("img"));
return convertView;
}
class Viewholder{
ImageView tv_iv;
TextView tv_name;
TextView tv_phone;
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.frxm.day02_menu.MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
item布局:
<?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="match_parent">
<ImageView
android:id="@+id/tv_iv"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="@id/tv_iv"
android:text="姓名"
android:gravity="center_vertical"/>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="@id/tv_iv"
android:layout_below="@id/tv_name"
android:text="手机号"
android:gravity="center_vertical"/>
</RelativeLayout>