package com.example.wang.blacklist;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.wang.blacklist.com.hanqi.blacklist.orm.BlackList;
import com.example.wang.blacklist.com.hanqi.blacklist.orm.BlackListDAO;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView lv_1;
//数据访问对象
BlackListDAO bld=new BlackListDAO(this);
//数据集合
ArrayList<BlackList> alb;
BLAdapter bla;
//长按的数据的索引
int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_1=(ListView)findViewById(R.id.lv_1);
//获取数据集合
alb=bld.getAll();
//显示数据
//adapter
bla=new BLAdapter();
lv_1.setAdapter(bla);
//增加上下文菜单,设置创建菜单的监听器
lv_1.setOnCreateContextMenuListener(this);
}
//重写创建上下文菜单的方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 1, 1, "修改");
menu.add(0, 2, 2, "删除");
//获取长按的数据信息
//1 得到菜单信息
AdapterView.AdapterContextMenuInfo acmi =(AdapterView.AdapterContextMenuInfo)menuInfo;
//2 得到数据在集合中的索引
index=acmi.position;
}
//响应菜单点击的回调方法
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId())
{
case 1:
//修改
final EditText et=new EditText(this);
et.setText(alb.get(index).getPhoneNumber());
AlertDialog a=new AlertDialog.Builder(this)
.setTitle("修改黑名单")
.setView(et)
.setNegativeButton("取消", null)
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//1.得到新数据的实体类
//赋值 1)传值; 复制新的值再传递,应用于值类型
// 2)传址 传递的是内存地址,指向同一个对象,应用于引用类型
//1.1方法1
//BlackList blackList = new BlackList(alb.get(index).getId(),
// et.getText().toString());
//1.2 方法2
BlackList blackList = alb.get(index);
blackList.setPhoneNumber(et.getText().toString());
//2-调用DAO的update方法
if (bld.updata(blackList) > 0) {
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
}
//3-刷新ListView
//方法1
// alb=bld.getAll();
//方法2
// alb.remove(index);
// alb.add(index, blackList);
//刷新列表
bla.notifyDataSetChanged();
}
})
.show();
break;
case 2:
//删除确认对话框
new AlertDialog.Builder(this)
.setTitle("确定要删除?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//删除
if ( bld.delete(alb.get(index).getId())>0)
{
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
//更新List
//方法1
//alb=bld.getAll();
//方法2
alb.remove(index);
//刷新列表
bla.notifyDataSetChanged();
}
else
{
Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("取消",null)
.show();
break;
}
return super.onContextItemSelected(item);
}
//添加按钮的监听器
public void add_OnClick(View v)
{
//自定义对话框
final EditText editText=new EditText(this);
editText.setHint("输入电话号码");
editText.setInputType(InputType.TYPE_CLASS_PHONE);
//构建对话框
AlertDialog al=new AlertDialog.Builder(this)
.setTitle("添加黑名单")
.setView(editText)
.setCancelable(false)
.setNegativeButton("取消", null)
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//向数据库保存
//1-定义实体类
BlackList blackList = new BlackList(editText.getText().toString());
//2-通过DAO插入数据
Long l=bld.insert(blackList);
if (l > 0) {
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
//更新List
//方法1
//alb=bld.getAll();
//方法2
blackList.setId(l);
alb.add(0,blackList);
//刷新列表
bla.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
})
.show();
}
//BaseAdapter 的实现类
class BLAdapter extends BaseAdapter
{
@Override
public int getCount() {
return alb.size();
}
@Override
public Object getItem(int position) {
return alb.get(position);
}
@Override
public long getItemId(int position) {
return alb.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//得到数据
BlackList blackList=alb.get(position);
//得到视图
if (convertView==null)
{
//构建视图
convertView=new TextView(MainActivity.this);
}
//视图和数据做显示匹配
TextView textView=(TextView)convertView;
textView.setText(blackList.getPhoneNumber());
textView.setTextSize(20);
textView.setHeight(80);
return convertView;
}
}
}