package cn.wuxiaocheng.contentprovider;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private MyAdapter myAdapter;
private List<ContactBean> contacts;
private FloatingActionButton fab;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv = (ListView) findViewById(R.id.lv);
contacts = new ArrayList<ContactBean>();
myAdapter = new MyAdapter(this, contacts);
lv.setAdapter(myAdapter);
setContactsData();
add();
//点击
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ContactBean contact = myAdapter.getItem(position);
showCheckChoose(contact);
}
});
//长按
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ContactBean contact = myAdapter.getItem(position);
deleteContact(contact);
return true;
}
});
}
private void deleteContact(final ContactBean contact) {
new AlertDialog.Builder(this)
.setTitle("删除该联系人")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContactManager.deleteContact(MainActivity.this, contact);
setContactsData();
}
})
.setNegativeButton("取消", null)
.show();
}
private void showCheckChoose(final ContactBean contact) {
new AlertDialog.Builder(this)
.setItems(new String[]{"拨打电话", "发送短信", "修改联系人"},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intentCall = new Intent();
intentCall.setAction(Intent.ACTION_CALL);
intentCall.setData(Uri.parse("tel:" + contact.getNum()));
startActivity(intentCall);
break;
case 1:
Intent intentSend = new Intent();
intentSend.setAction(Intent.ACTION_SENDTO);
intentSend.setData(Uri.parse("smsto:" + contact.getNum()));
startActivity(intentSend);
break;
case 2:
updateContact(contact);
setContactsData();
break;
default:
break;
}
}
})
.show();
}
//悬浮按钮
private void add() {
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addContext();
}
});
}
//添加联系人对话框
private void addContext() {
//拿到自定义布局
final LayoutInflater inflate = getLayoutInflater();
View layout = inflate.inflate(R.layout.add_contact, null);
final EditText ed_Name = (EditText) layout.findViewById(R.id.ed_Name);
final EditText ed_Num = (EditText) layout.findViewById(R.id.ed_Num);
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(layout);
//添加联系人
DialogInterface.OnClickListener ls = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContactBean contact = new ContactBean();
contact.setName(ed_Name.getText().toString());
contact.setNum(ed_Num.getText().toString());
ContactManager.addContact(MainActivity.this, contact);
setContactsData();
}
};
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", ls);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", cancel);
dialog.show();
}
//更新联系人对话框
private void updateContact(final ContactBean oldContact) {
View view = View.inflate(this, R.layout.custom, null);
final EditText ed_Name = (EditText) view.findViewById(R.id.ed_Name);
final EditText ed_Num = (EditText) view.findViewById(R.id.ed_Num);
ed_Name.setText(oldContact.getName());
ed_Num.setText(oldContact.getNum());
new AlertDialog.Builder(this)
.setTitle("修改联系人")
.setView(view)
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContactBean contact = new ContactBean();
contact.setRawContactId(oldContact.getRawContactId());
contact.setName(ed_Name.getText() + "");
contact.setNum(ed_Num.getText() + "");
ContactManager.updateContact(MainActivity.this, contact);
setContactsData();
}
})
.setNegativeButton("取消", null)
.show();
}
//加载联系人
private void setContactsData() {
List<ContactBean> contactData = ContactManager.getContacts(this);
contacts.clear();
contacts.addAll(contactData);
myAdapter.notifyDataSetChanged();
}
//取消动作
DialogInterface.OnClickListener cancel = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
};
}