代码
Main方法里的代码
package com.example.app3;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.DataFormatException;
public class MainActivity extends AppCompatActivity {
private ListView listViewid;
private List<Map<String,Object>> lists=new ArrayList<>();
private int [] imageId={R.mipmap.like,R.mipmap.like5,R.mipmap.like11,R.mipmap.like14,R.mipmap.like16,R.mipmap.like17,R.mipmap.like18,R.mipmap.like19,R.mipmap.like5,R.mipmap.like8};
private String[] names={"小花","小草","小爱","小黑","小白","小绿","小粉","小紫","小红","小兰"};
private String[] tels={"13218913357","13218913357","13218913357","13218913357","13218913357","13218913357","13218913357","13218913357","13218913357","13218913357"};
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
listViewid = (ListView) findViewById(R.id.listViewid);
for (int i = 0; i < 10; i++) {
Map<String,Object> map=new HashMap<>();
map.put("Image",imageId[i]);
map.put("Name",names[i]);
map.put("Tel",tels[i]);
lists.add(map);
}
myAdapter=new MyAdapter(this,lists);
listViewid.setAdapter(myAdapter);
listViewid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon((Integer) lists.get(position).get("Image"));
builder.setMessage("个人信息:姓名:"+lists.get(position).get("Name")+"电话:"+lists.get(position).get("Tel"));
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
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) {
Toast.makeText(MainActivity.this, "当前时间为:"+year+"/"+(month+1)+"/"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
});
listViewid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
if (position==2){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon((Integer) lists.get(position).get("Image"));
builder.setMessage("个人信息:姓名:"+lists.get(position).get("Name")+"电话:"+lists.get(position).get("Tel"));
builder.setTitle("友情提示:");
builder.setMessage("您确定要删除"+lists.get(position).get("Name")+"的信息吗");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定删除", Toast.LENGTH_SHORT).show();
lists.remove(position);
myAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消删除", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
return true;
}
});
}
}
MyAdapter 代码
package com.example.app3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Map<String,Object>> lists;
private LayoutInflater layoutInflater;
public MyAdapter(Context context, List<Map<String, Object>> lists) {
this.context = context;
this.lists = lists;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView==null){
holder=new ViewHolder();
convertView=layoutInflater.inflate(R.layout.item_layout,null);
holder.imageView=convertView.findViewById(R.id.layout_imageId);
holder.textView_name=convertView.findViewById(R.id.layout_text_name);
holder.textView_tel=convertView.findViewById(R.id.layout_text_telphone);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
holder.textView_tel.setText((String)lists.get(position).get("Tel"));
holder.textView_name.setText((String) lists.get(position).get("Name"));
Glide.with(context).load(lists.get(position).get("Image")).into(holder.imageView);
return convertView;
}
static class ViewHolder{
private ImageView imageView;
private TextView textView_name;
private TextView textView_tel;
}
}
布局
Main方法里的布局
<?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"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewid"></ListView>
</LinearLayout>
listView方法里的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_margin="10dp"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/layout_imageId"
android:src="@mipmap/ic_launcher"
android:layout_width="100dp"
android:layout_height="100dp">
</ImageView>
<TextView
android:id="@+id/layout_text_name"
android:layout_marginLeft="30dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:gravity="center"
android:text="姓名"></TextView>
<TextView
android:id="@+id/layout_text_telphone"
android:layout_marginLeft="30dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:gravity="center"
android:text="电话号码"></TextView>
</LinearLayout>