DBHelpter
package com.example.lenovo.heimingdan.com.hanqi.heimingdan.com;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;/** * Created by lenovo on 2016/6/7. */public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { super(context,"blacklist.db", null,1); } @Override public void onCreate(SQLiteDatabase db) { String sql="CREATE TABLE t_blacklist ( " + "_id INTEGER PRIMARY KEY " + "AUTOINCREMENT NOT NULL," + "phone_number VARCHAR(20) null)"; db.execSQL(sql);//建表 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}
XunwuQS
package com.example.lenovo.xunwuqs.com.hanqi.xunwuqs;/** * Created by lenovo on 2016/6/16. */public class XunwuQS { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public XunwuQS(long id, String name) { this.id = id; this.name = name; } public XunwuQS(String name) { this.name = name; }}
XunwuQSDAO
package com.example.lenovo.xunwuqs.com.hanqi.xunwuqs;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import java.util.ArrayList;/** * Created by lenovo on 2016/6/16. */public class XunwuQsDAO { private DBHelper dbHelper; private String TABLE_NAME="main.NewTable"; public XunwuQsDAO(Context context) { dbHelper=new DBHelper(context); } //增 public long insert(XunwuQS xwqs) { long rtn=0; //连接数据库 SQLiteDatabase sd=dbHelper.getReadableDatabase(); //执行insert ContentValues cv=new ContentValues(); cv.put("name",xwqs.getName()); rtn=sd.insert(TABLE_NAME,null,cv); sd.close(); return rtn; } //删 public int delete(long id) { int rtn=0; SQLiteDatabase sd=dbHelper.getReadableDatabase(); rtn=sd.delete(TABLE_NAME, "_id=?", new String[]{id + ""}); sd.close(); return rtn; } //改 public int updete(XunwuQS xunwuQS) { int rtn=0; SQLiteDatabase sd=dbHelper.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("name",xunwuQS.getName()); rtn=sd.update(TABLE_NAME, cv, "_id=?", new String[]{xunwuQS.getId() + ""}); sd.close(); return rtn; } //查 public ArrayListgetAll() { ArrayList xunwuQSes=new ArrayList<>(); SQLiteDatabase sd=dbHelper.getReadableDatabase(); Cursor cursor=sd.query(TABLE_NAME,null,null,null,null,null,"_id desc"); while (cursor.moveToNext()) { XunwuQS xunwuQS=new XunwuQS(cursor.getLong(0),cursor.getString(1)); xunwuQSes.add(xunwuQS); } cursor.close(); sd.close(); return xunwuQSes; }}
Mainactivity
package com.example.lenovo.heimingdan;import android.app.AlertDialog;import android.content.DialogInterface;import android.renderscript.ScriptGroup;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.InputType;import android.view.ContextMenu;import android.view.LayoutInflater;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.example.lenovo.heimingdan.com.hanqi.heimingdan.com.BlackListDAO;import com.example.lenovo.heimingdan.com.hanqi.heimingdan.com.Heimingdan;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { ListView lv_1; //数据访问对象 BlackListDAO bld=new BlackListDAO(this); //数据集合 ArrayListalb; 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); //增加上下文菜单,设置创建菜单的监听 lv_1.setOnCreateContextMenuListener(this); //获取数据 alb=bld.getAll(); //显示数据 //实例化adapter lv_1.setAdapter(new BLAdapter()); bla=new BLAdapter(); } //创建上下文菜单的重写方法 @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-得到数据的所在位置。acmi.position; index=acmi.position; } //相应菜单点击的回调方法 @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: //修改 final EditText editText = new EditText(this); editText.setText(alb.get(index).getPhonenumber()); new AlertDialog.Builder(this) .setTitle("修改黑名单") .setView(editText) .setNegativeButton("取消",null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //1-得到新数据的实体类 //1)赋值;复制新的值再传递。值类型 // 2)传值;传递的是内存地址,指向同一对象。引用类型 Heimingdan bl = alb.get(index); bl.setPhonenumber(editText.getText().toString()); //2-调用DAO的update() if (bld.update(bl) > 0) { Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); //更新List //刷新列表 bla.notifyDataSetChanged(); } else { Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show(); } } }) .show(); break; case 2: //添加一个删除-确定取消对话框 //删除 if (bld.delete(alb.get(index).getId())>0) { new AlertDialog.Builder(this) .setTitle("确定删除?") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "取消删除", Toast.LENGTH_SHORT).show(); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alb.remove(index); bla.notifyDataSetChanged(); Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); } }) .show(); } else { Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show(); } break; } return super.onContextItemSelected(item); } //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) { //得到数据 Heimingdan bl= alb.get(position); //得到视图 if (convertView==null) { //构建视图 convertView=new TextView(MainActivity.this); } //视图和数据显示匹配 TextView textView=(TextView)convertView; textView.setTextSize(20); textView.setHeight(80); textView.setText(bl.getPhonenumber()); return textView; } } //添加按钮的监听器 public void add_1(View v) { //添加 final EditText editText = new EditText(this); editText.setHint("输入电话号码"); editText.setInputType(InputType.TYPE_CLASS_PHONE); new AlertDialog.Builder(this) .setTitle("填加黑名单") .setView(editText) .setCancelable(false) .setNegativeButton("取消", null) .setPositiveButton("保存", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //向数据库保存 //1-定义实体类 Heimingdan heimingdan = new Heimingdan(editText.getText().toString()); //2-通过DAO插入数据。调用insert Long l =bld.insert(heimingdan); if (l > 0) { Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); //更新List //alb=bld.getAll(); heimingdan.setId(l); alb.add(0,heimingdan); //刷新列表 bla.notifyDataSetChanged(); } else { Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show(); } } }) .show(); }}