activity_mian.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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="com.hanqi.blacklist.MianActivity" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@android:id/list" > </ListView> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/empty" android:layout_weight="1" android:gravity="center" android:text="还没有一个黑名单"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加" android:onClick="bt1_onclick"/> </LinearLayout>
DBHelper.java
package com.hanqi.blacklist; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by Administrator on 2016/4/15. */ public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { super(context,"blacklist.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table black_number (_id integer primary key autoincrement, phone_nubmer varchar)"); Log.e("TAG","onCreate"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
BlackNumber.java
package com.hanqi.blacklist; /** * Created by Administrator on 2016/4/15. */ public class BlackNumber { private long id; private String phonenumber; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } public BlackNumber() { } public BlackNumber(long id, String phonenumber) { this.id = id; this.phonenumber = phonenumber; } public BlackNumber(String phonenumber) { this.phonenumber = phonenumber; } @Override public String toString() { return "BlackNumber{" + "id=" + id + ", phonenumber=‘" + phonenumber + ‘\‘‘ + ‘}‘; } }
BlackNumberDAO.java
package com.hanqi.blacklist; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016/4/15. */ public class BlackNumberDAO { private DBHelper dbHelper; //初始化工具类 public BlackNumberDAO(Context context) { dbHelper = new DBHelper(context); } //增 public void insert(BlackNumber blackNumber) { //获取数据库连接 SQLiteDatabase sql = dbHelper.getWritableDatabase(); //要添加数据的键值对集合 ContentValues contentValues =new ContentValues(); contentValues.put("phone_number",blackNumber.getPhonenumber()); //数据添加 sql.insert("black_number",null,contentValues); //关闭数据库 sql.close(); Log.e("TAG","添加数据成功"); } //删除 public void delete(long id) { //获取数据库连接 SQLiteDatabase sql = dbHelper.getWritableDatabase(); //数据删除 sql.delete("black_number","_id=?",new String[]{String.valueOf(id)}); //关闭数据库 sql.close(); Log.e("TAG","删除数据成功"); } //改 public void update(BlackNumber blackNumber) { //获取数据库连接 SQLiteDatabase sql = dbHelper.getWritableDatabase(); //要添加数据的键值对集合 ContentValues contentValues =new ContentValues(); contentValues.put("phone_number", blackNumber.getPhonenumber()); //数据修改 sql.update("black_number",contentValues,"_id=?",new String[]{String.valueOf(blackNumber.getId())}); //关闭数据库 sql.close(); Log.e("TAG","修改数据成功"); } //查 //用list<实体类的实例>返回数据 public List<BlackNumber> getAll() { List<BlackNumber> rtn = new ArrayList<BlackNumber>(); //查询数据 //获取数据库连接 SQLiteDatabase sql = dbHelper.getWritableDatabase(); //返回游标 Cursor cursor = sql.query("black_number", null, null, null, null, null, "_id desc"); //移动游标,获取数据 while (cursor.moveToNext()) { //构造实体类的实例,放入List rtn.add(new BlackNumber(cursor.getLong(0),cursor.getString(1))); } Log.e("TAg","记录条数 = "+cursor.getCount()); sql.close(); return rtn; } }
MianActivity.java
package com.hanqi.blacklist; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.os.Bundle; 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 java.util.List; public class MianActivity extends ListActivity { private ListView lv1; private List<BlackNumber> data; private BlackListAdapter dla; private int position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mian); //lv1 = (ListView)findViewById(R.id.lv_1); lv1 = getListView(); //获取数据集合 BlackNumberDAO bd = new BlackNumberDAO(this); data = bd.getAll(); dla = new BlackListAdapter(); //设置适配器 lv1.setAdapter(dla); //设置上下文菜单的监听 lv1.setOnCreateContextMenuListener(this); } //上下文菜单被点击 @Override public boolean onContextItemSelected(MenuItem item) { final BlackNumber bn =data.get(position); switch (item.getItemId()) { case 0://修改 final EditText et = new EditText(this); et.setInputType(InputType.TYPE_CLASS_PHONE); et.setText(bn.getPhonenumber()); //弹出对话框 new AlertDialog.Builder(this) .setTitle("修改黑名单") .setView(et) .setNeutralButton("取消",null) .setPositiveButton("保存", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //获取修改后的值 bn.setPhonenumber(et.getText().toString()); //执行数据库修改操作 BlackNumberDAO bnd = new BlackNumberDAO(MianActivity.this); bnd.update(bn); //更新集合 //bn和data里面的对象,指向同一个内存地址 //如果bn 被修改了, data里面的对象也同时 //这就是引用类型的特点 //通知刷新列表 dla.notifyDataSetChanged(); Toast.makeText(MianActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); } }) .show(); break; case 1://删除 BlackNumberDAO bd = new BlackNumberDAO(this); //需要id 数据库删除了 bd.delete(bn.getId()); //刷新界面 data.remove(position); //通知刷新 dla.notifyDataSetChanged(); break; } return super.onContextItemSelected(item); } //创建上下文菜单 public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, 0, 0, "修改"); menu.add(0,1,1,"修改"); //获取长按的适配器信息 AdapterView.AdapterContextMenuInfo acm = (AdapterView.AdapterContextMenuInfo)menuInfo; position=acm.position; } //列表的适配器 class BlackListAdapter extends BaseAdapter { @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return data.get(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { //判断可复用的视图是否为空 if(convertView == null) { //获取视图 convertView = View.inflate(MianActivity.this,android.R.layout.simple_list_item_1,null); } //给视图绑定视图 TextView tv = (TextView)convertView.findViewById(android.R.id.text1); tv.setText(data.get(position).getPhonenumber()); return convertView; } } public void bt1_onclick(View v) { final EditText editText = new EditText(this); editText.setHint("输入电话号码"); //editText.setLayoutParams(new ActionBar.LayoutParams()); editText.setInputType(InputType.TYPE_CLASS_PHONE); //构造在定义对话框 new AlertDialog.Builder(this) .setTitle("添加黑名单") .setView(editText) .setNeutralButton("取消",null) .setPositiveButton("保存", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //构造实体类 BlackNumber blackNumber = new BlackNumber(editText.getText().toString()); //调用DAO类,保存数据 BlackNumberDAO blackNumberDAO = new BlackNumberDAO(MianActivity.this); blackNumberDAO.insert(blackNumber); //添加数据到集合数据集合的顶部 data.add(0,blackNumber); //通知适配器刷新ListView dla.notifyDataSetChanged(); Toast.makeText(MianActivity.this, "数据保存成功", Toast.LENGTH_SHORT).show(); } }) .show(); } }
时间: 2024-10-13 03:11:01