JAVA代码
package com.example.dell.heimingdanfu; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by DELL on 2016/4/16. */ public class DBhelper extends SQLiteOpenHelper { public DBhelper(Context context) { super(context, "user", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table user(_id integer primary key autoincrement,number varchar )"); Log.e("TAG","创建数据表成功"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.example.dell.heimingdanfu; /** * Created by DELL on 2016/4/16. */ public class HeiMingdanfu { private long _id; private String number; public HeiMingdanfu(long _id, String number) { this._id = _id; this.number = number; } public long get_id() { return _id; } public void set_id(long _id) { this._id = _id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public HeiMingdanfu(String number) { this.number = number; } public HeiMingdanfu() { } }
package com.example.dell.heimingdanfu; 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 DELL on 2016/4/16. */ public class HeiMingDanFuDAO { private DBhelper dBhelper; public HeiMingDanFuDAO(Context context) { dBhelper = new DBhelper(context); } //増 public void insert(HeiMingdanfu heiMingdanfu) { SQLiteDatabase sd = dBhelper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("number",heiMingdanfu.getNumber()); sd.insert("user", null, contentValues); sd.close(); Log.e("TAG","添加成功"); } //删 public void delete(long _id) { SQLiteDatabase sd = dBhelper.getWritableDatabase(); sd.delete("user","_id=?",new String[]{String.valueOf(_id)}); sd.close(); } //改 public void update(HeiMingdanfu heiMingdanfu) { SQLiteDatabase sd = dBhelper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("number",heiMingdanfu.getNumber()); sd.update("user", contentValues, "_id = ?", new String[]{String.valueOf(heiMingdanfu.get_id())}); sd.close(); } //查 public Listgetall() { List rtn= new ArrayList ();; SQLiteDatabase sd = dBhelper.getWritableDatabase(); Cursor cursor = sd.query("user", null, null, null, null, null, null); while (cursor.moveToNext()) { rtn.add(new HeiMingdanfu(cursor.getLong(0),cursor.getString(1))); } return rtn; } }
package com.example.dell.heimingdanfu; 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 java.util.List; public class heimingdanfuActivity extends AppCompatActivity { private ListView lv_1; private Listdata; private MyAdapter ma; private int position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_heimingdanfu); lv_1 = (ListView)findViewById(R.id.lv_1); HeiMingDanFuDAO heiMingDanFuDAO = new HeiMingDanFuDAO(this); data = heiMingDanFuDAO.getall(); ma = new MyAdapter(); lv_1.setAdapter(ma); lv_1.setOnCreateContextMenuListener(this); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(0, 0, 0, "修改"); menu.add(0, 1, 1, "删除"); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; position = info.position; super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { //修改 case 0: final EditText editText = new EditText(this); editText.setText(data.get(position).getNumber()); 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) { HeiMingDanFuDAO heiMingDanFuDAO = new HeiMingDanFuDAO(heimingdanfuActivity.this); data.get(position).setNumber(editText.getText().toString()); heiMingDanFuDAO.update(data.get(position)); ma.notifyDataSetChanged(); } }) .show(); break; //删除 case 1: new AlertDialog.Builder(this) .setTitle("确定删除吗?") .setNeutralButton("取消",null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { HeiMingDanFuDAO heiMingDanFuDAO = new HeiMingDanFuDAO(heimingdanfuActivity.this); heiMingDanFuDAO.delete(data.get(position).get_id()); data.remove(position); ma.notifyDataSetChanged(); } }) .show(); break; } return super.onContextItemSelected(item); } class MyAdapter 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).get_id(); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(heimingdanfuActivity.this,android.R.layout.simple_list_item_1,null); } TextView textView = (TextView)convertView.findViewById(android.R.id.text1); textView.setText(data.get(position).getNumber()); return convertView; } } public void tianjia(View view) { final EditText editText = new EditText(this); editText.setHint("请输入号码"); 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) { //data.add(); HeiMingDanFuDAO heiMingDanFuDAO = new HeiMingDanFuDAO(heimingdanfuActivity.this); HeiMingdanfu heiMingdanfu = new HeiMingdanfu(editText.getText().toString()); heiMingDanFuDAO.insert(heiMingdanfu); //添加数据到集合的顶部 data.add(0,heiMingdanfu ); ma.notifyDataSetChanged(); } }) .show(); } }
XML代码
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.example.dell.heimingdanfu.heimingdanfuActivity" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/lv_1"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加" android:onClick="tianjia"/>