<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
ListView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="add"
/>
LinearLayout>
package com.itzheng.app04_sqlite;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends Activity {
//初始化ListView
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = (ListView) findViewById(R.id.lv_main);
}
public void add(View v){
}
}
package com.itzheng.app04_sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/*
* 数据库操作的帮助类
*/
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context ) {
super(context, "itzheng.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("TAG", "DBHelper onCreate()");
//创建表
db.execSQL("create table black_number(_id integer primary key autoincrement,number varchar )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
package com.itzheng.app04_sqlite;
/*
* 某一个表black_number表对应的实现类
*/
public class BlackNumber {
private int id;
private String number;
public BlackNumber() {
super();
// TODO Auto-generated constructor stub
}
public BlackNumber(int id, String number) {
super();
this.id = id;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "BlackNumber [id=" + id + ", number=" + number + "]";
}
}
package com.itzheng.app04_sqlite;
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;
import android.util.Log;
import android.widget.ArrayAdapter;
/*
* 操作black_number表的DAO类
*/
public class BlackNumberDao {
private DBHelper dbHelper;
public BlackNumberDao(Context context) {
dbHelper = new DBHelper(context);
}
/*
* 添加一条记录
*/
public void add(BlackNumber blackNumber) {
// 1、得到连接
SQLiteDatabase database = dbHelper.getReadableDatabase();
// 2、执行insert insert into balck_number ( number ) values(xxx)
ContentValues values = new ContentValues();
values.put("number", blackNumber.getNumber());
long id = database.insert("balck_number", null, values);
Log.i("TAG", "id=" + id);
// 3、关闭
database.close();
}
/*
* 根据ID删除一条记录
*/
public void deleteById(int id) {
// 1、得到连接
SQLiteDatabase database = dbHelper.getReadableDatabase();
// 2、执行delete delete from black_number where _id = id
int deleteCount = database.delete("black_number", "_id=?",
new String[] {
id + "" });
Log.i("TAG", "deleteCount" + deleteCount);
// 3、关闭
database.close();
}
/*
* 更新记录
*/
public void update(BlackNumber blackNumber) {
// 1、得到连接
SQLiteDatabase database = dbHelper.getReadableDatabase();
// 2、执行update update balck_number set number = xxx where _id = id
ContentValues values = new ContentValues();
values.put("number", blackNumber.getNumber());
int updateCount = database.update("black_number", values, "_id="
+ blackNumber.getId(), null);
Log.i("TAG", "updateCount" + updateCount);
// 3、关闭
database.close();
}
/*
* 查询所有记录,分装为List
*/
public List<BlackNumber> getAll() {
List<BlackNumber> list = new ArrayList<BlackNumber>();
// 1、得到连接
SQLiteDatabase database = dbHelper.getReadableDatabase();
// 2、执行query select * from black_number
Cursor cursor = database.query("black_number", null, null, null, null,
null, null);
while (cursor.moveToNext()) {
// id
int id = cursor.getInt(0);
// number
String number = cursor.getString(1);
list.add(new BlackNumber(id, number));
}
// 从cursor当中取出所有数据,并封装到List当中
cursor.close();
// 3、关闭
database.close();
return list;
}
}
package com.itzheng.app04_sqlite;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
//初始化ListView
private ListView lv_main;
private BlackNumberAdapter adapter;
private BlackNumberDao dao;
private List<BlackNumber> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = (ListView) findViewById(R.id.lv_main);
adapter = new BlackNumberAdapter();
dao = new BlackNumberDao(this);
data = dao.getAll();
lv_main.setAdapter(adapter);
}
public void add(View v){
}
class BlackNumberAdapter 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 0;
}
@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);
}
BlackNumber blackNumber = data.get(position);
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
textView.setText(blackNumber.getNumber());
return convertView;
}
}
}
1、显示添加的dialog(带输入框)
2、在确定的回调方法当中实现:
保存数据到数据库当中
保存数据到内存集合当中
通知更新列表
public void add(View v){
final EditText editText = new EditText(this);
editText.setHint("输入黑名单号");
new AlertDialog.Builder(this)//在当前页面建立AlertDialog(弹出框)
.setTitle("添加黑名单")
.setView(editText)
.setPositiveButton("添加", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//保存数据到数据库当中
String number = editText.getText().toString();//获取页面上的数据
BlackNumber blackNumber = new BlackNumber(-1,number);
dao.add(blackNumber);//将数据保存到数据库当中
//保存数据到内存集合当中
data.add(0,blackNumber);//在以有的数据当中添加数据(页面上已有的数据)已经有ID
//通知更新列表
adapter.notifyDataSetChanged();
}
} )
.setNegativeButton("取消", null)
.show();
}
/*
* 查询所有记录,分装为List
*/
public List<BlackNumber> getAll() {
List<BlackNumber> list = new ArrayList<BlackNumber>();
// 1、得到连接
SQLiteDatabase database = dbHelper.getReadableDatabase();
// 2、执行query select * from black_number
Cursor cursor = database.query("black_number", null, null, null, null, null, "_id desc");
while (cursor.moveToNext()) {
// id
int id = cursor.getInt(0);
// number
String number = cursor.getString(1);
list.add(new BlackNumber(id, number));
}
// 从cursor当中取出所有数据,并封装到List当中
cursor.close();
// 3、关闭
database.close();
return list;
}
1、显示ContextMenu
2、响应item的选择
1)删除数据表当中对应的数据
2)删除List当中的对应的数据
3)通知更新列表
给listView设置创建contextMenu的监听
lv_main.setOnCreateContextMenuListener(this);
完善MainActivity当中的onCreateContextMenu方法和onContextItemSelected
//触发长按按钮
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//在长按弹出的菜单当中添加2个item
menu.add(0,1,0,"更新");
menu.add(0,2,0,"删除");
//得到长按的position
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;//得到所有的列表
position = info.position;//得到当前点击的列表下标
}
@Override
public boolean onContextItemSelected(MenuItem item) {
//得到对应的BlackNumber对象
final BlackNumber blackNumber = data.get(position);//通过下标得到对应的BlackNumber对象
switch (item.getItemId()) {
case 1://更新
break;
case 2://删除
//1)删除数据表当中对应的数据
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告");
builder.setMessage("是否确认删除:"+blackNumber.getNumber());
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dao.deleteById(blackNumber.getId());//删除数据库当中的数据
//2)删除List当中的对应的数据
data.remove(position);//删除列表当中对应的数据,需要得到长按的position?
//3)通知更新列表
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//一样要show
builder.show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
1、显示更新的Dialog
2、点击确定的响应
1)更新数据表对应的数据
2)更新List表当中的数据
3)通知更新列表
/*
* 1、显示更新的Dialog
*/
private void showUpdateDialog(final BlackNumber blackNumber) {
final EditText editText = new EditText(this);
editText.setHint(blackNumber.getNumber());
new AlertDialog.Builder(this)//在当前页面建立AlertDialog(弹出框)
.setTitle("更新黑名单")
.setView(editText)
.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//1)更新List表当中的数据
String newNumber = editText.getText().toString();
blackNumber.setNumber(newNumber);
//2)更新数据表对应的数据
dao.update(blackNumber);
//3)通知更新列表
adapter.notifyDataSetChanged();
}
} )
.setNegativeButton("取消", null)
.show();
}
1、内存的操作(集合)
2、存储的操作(sp/数据库/文件)
3、界面的操作(列表)
拆卸重新安装对应的应用后,其中的表对应订单数据都消失了
启动应用后显示空白
需要在屏幕中间设置对应的提示文字
使用ListActivity优化功能
lv_main = (ListView)getListView();
的id必须是系统定义的id:list
(id必须为empty) <TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="还没有一个黑名单"
android:gravity="center"
/>