黑名单增删改

黑名单增删改_第1张图片

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical" 
        android:layout_weight="1"
        android:gravity="center">

        <ListView
            android:id="@+id/lv_main_blacks"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:visibility="gone">
        </ListView>

    
         <TextView
            android:gravity="center"
	        android:id="@+id/tv_main_empty"
	        android:layout_width="fill_parent"
	        android:layout_height="match_parent"
	        android:text="还没有一个黑名单" 
	        android:textSize="20dp"
	        android:visibility="visible"/>
        
    </LinearLayout>

   
    <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal" 
             android:background="#999999">
        <EditText
            android:id="@+id/et_main_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        </EditText>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添 加" 
            android:onClick="addBlack"/>
  </LinearLayout>
    

</LinearLayout>

Black.java(只列出属性即可,其他省略)

private int id;
private String number;


DBHelper.java

package com.example.heimingdan;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	/**
	 * 数据库名字--数据库表名字
	 */
	private static String DBNAME = "wyf.db";
	public static String TABLE_NAME = "black";

	/**
	 * 初始化创建数据库
	 */
	public DBHelper(Context context) {
		super(context, DBNAME, null, 1);
	}

	/**
	 * 创建数据库表
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table black(_id integer primary key autoincrement,number)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
}

Dao.java

package com.example.heimingdan;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Dao {
	private DBHelper dbHelper;
	/*
	 *dao一创建就初始化dbhelper 
	 */
	public Dao(Context context){
		dbHelper = new DBHelper(context);
	}
	/*
	 * 添加对象
	 */
	public void add(Black black){
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		//准备数据
		ContentValues values = new ContentValues();
		values.put("number", black.getNumber());
		//执行插入操作
		long id = database.insert(DBHelper.TABLE_NAME, null, values);
		//关闭资源
		database.close();
	}
	
	/*
	 * 删除
	 */
	public void deleteById(String id) {
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		int deleteCount = database.delete(DBHelper.TABLE_NAME, "_id=?", new String[]{id});
		database.close();
	}
	
	/*
	 * 更新
	 */
	public void update(Black black) {
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		//准备数据
		ContentValues values = new ContentValues();
		values.put("number", black.getNumber());
		
		int updateCount = database.update(DBHelper.TABLE_NAME, values, "_id=?", new String[]{black.getId()+""});
		
		database.close();
	}
	
	/*
	 * 查询
	 */
	public List<Black> getAll() {
		List<Black> list = new ArrayList<Black>();
		SQLiteDatabase database = dbHelper.getReadableDatabase();

		Cursor cursor = database.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);
		
		while(cursor.moveToNext()) {
			int id = cursor.getInt(0);
			String number = cursor.getString(1);
			Black blackNumber = new Black(id, number);
			list.add(blackNumber);
		}
		
		cursor.close();
		database.close();
		return list;
	}
}

MainActivity.java

package com.example.heimingdan;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ListView lv_main_blacks;
	private TextView tv_main_empty;

	private EditText et_main_number;

	// 用来对黑名单的增删改以及获取 黑名单集合
	private Dao dao;
	private List<Black> data;
	// 设置存放黑名单的适配器
	private Myadapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 获取number视图
		et_main_number = (EditText) findViewById(R.id.et_main_number);
		// 获取listview视图
		lv_main_blacks = (ListView) findViewById(R.id.lv_main_blacks);
		// 获取textview视图
		tv_main_empty = (TextView) findViewById(R.id.tv_main_empty);
		dao = new Dao(this);
		// 获取所有的黑名单条目
		data = dao.getAll();
		// 界面加载时候,如果有黑名单就显示出来
		if (data != null && data.size() > 0) {
			// 隐藏textview
			tv_main_empty.setVisibility(View.GONE);
			// 显示列表
			lv_main_blacks.setVisibility(View.VISIBLE);
			adapter = new Myadapter();
			// listview设置适配器
			lv_main_blacks.setAdapter(adapter);
		}

		// 添加listview上下菜单的监听--本类已经实现了接口,需要重写某方法
		lv_main_blacks.setOnCreateContextMenuListener(this);
	}

	// 产生上下文列表
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.add(0, 1, 0, "更新");
		menu.add(0, 2, 0, "删除");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	// 对上下文列表item设置方法
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// ContextMenuInfo是一个接口,需要实现类
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
				.getMenuInfo();
		// 选择的view的下标
		int location = menuInfo.position;
		switch (item.getItemId()) {
		case 1:
			showUpdateDialog(location);
			break;
		case 2:
			Black black = data.get(location);
			// 删除表中的数据
			dao.deleteById(black.getId() + "");
			// 删除list中的数据
			data.remove(location);
			// 更新界面
			adapter.notifyDataSetChanged();
			break;

		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	/**
	 * 更新黑名单号码的方法
	 */
	private void showUpdateDialog(int location) {
		final Black black = data.get(location);
		// 需要更新的条目的number
		String number = black.getNumber();
		final EditText editText = new EditText(this);
		new AlertDialog.Builder(this).setTitle("更新:" + number)
				.setView(editText)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						String newNumber = editText.getText().toString();

						black.setNumber(newNumber);

						dao.update(black);
						// 更新界面
						adapter.notifyDataSetChanged();
					}
				}).setNegativeButton("取消", null).show();
	}

	// 添加黑名单
	public void addBlack(View view) {
		// 读取数据
		String number = et_main_number.getText().toString();
		// 准备对象
		Black black = new Black();
		black.setNumber(number);
		// 保存对象
		dao.add(black);
		Toast.makeText(this, "保存成功", 0).show();
		// 更新黑名单列表
		data = dao.getAll();
		// 通知更新
		adapter.notifyDataSetChanged();
	}

	// 自定义适配器
	private 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 position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				// 动态加载布局
				convertView = View.inflate(MainActivity.this,
						android.R.layout.simple_list_item_1, null);
			}
			// 得到当前的记录
			Black black = data.get(position);
			// 得到布局文件的视图对象
			TextView tv = (TextView) convertView
					.findViewById(android.R.id.text1);
			// 将记录设置到对应的位置上去
			tv.setText(black.getNumber());

			return convertView;
		}

	}

}




你可能感兴趣的:(黑名单增删改)