黑名单
Posted 1011042043
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了黑名单相关的知识,希望对你有一定的参考价值。
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; } } }
package com.example.wang.blacklist.com.hanqi.blacklist.orm; //黑名单表的实体类 public class BlackList { 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 BlackList(long id, String phoneNumber) { this.id = id; this.phoneNumber = phoneNumber; } public BlackList(String phoneNumber) { this.phoneNumber = phoneNumber; } }
package com.example.wang.blacklist.com.hanqi.blacklist.orm; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by wang 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))"; db.execSQL(sql); Log.e("TAG", "表创建成功"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.example.wang.blacklist.com.hanqi.blacklist.orm; 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; /** * Created by wang on 2016/6/6. */ //数据库操作类 public class BlackListDAO { private DBHelper dh; private final String TABLENAME="t_blacklist"; public BlackListDAO(Context context) { // this.context=context; dh=new DBHelper(context); } //增 //传入参数:实体类的实例 public long insert(BlackList blackList) { long rtn=0; //连接数据库 SQLiteDatabase sd=dh.getWritableDatabase(); //执行insert语句 //insert into t_blacklist(phone_number) values () ContentValues cv=new ContentValues(); cv.put("phone_number",blackList.getPhoneNumber()); rtn=sd.insert(TABLENAME,null,cv); sd.close(); return rtn; } //删 public int delete(long id) { int rtn=0; //delete from t_blacklist where _id=? SQLiteDatabase sd=dh.getWritableDatabase(); rtn=sd.delete(TABLENAME,"_id=?",new String[]{id+""}); sd.close(); return rtn; } //改 public int updata(BlackList blackList) { int rtn=0; //updata t_blacklist set phone_number=? where _id=? SQLiteDatabase sd=dh.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("phone_number",blackList.getPhoneNumber()); rtn=sd.update(TABLENAME,cv,"_id=?",new String[]{blackList.getId()+""}); Log.e("TAG", "jieguo= + xiugau"+blackList.getPhoneNumber()); sd.close(); return rtn; } //查 //返回查询结果 public ArrayList<BlackList> getAll() { ArrayList<BlackList> blackLists=new ArrayList<>(); //连接数据库 SQLiteDatabase sd=dh.getWritableDatabase(); //select * from t_blacklist //查询之后得到游标结果集 Cursor cursor=sd.query(TABLENAME, null, null, null, null, null, "_id desc"); //遍历结果集 while (cursor.moveToNext()) { //1.把数据转成实体类的实例 BlackList bl=new BlackList(cursor.getLong(0),cursor.getString(1)); //2.把实例放在集合里 blackLists.add(bl); // Log.e("TAG", "blackL" + cursor.getString(1)); } cursor.close(); sd.close(); //返回这个集合 return blackLists; } }
<?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.example.wang.blacklist.MainActivity" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/lv_1"></ListView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加黑名单" android:onClick="add_OnClick"/> </LinearLayout>
以上是关于黑名单的主要内容,如果未能解决你的问题,请参考以下文章