Listview中取数据

  1. package com.hgq.hepler;

  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;

  7. public class MyDatabaseHepler extends SQLiteOpenHelper
  8. {

  9. // 数据库名称
  10. private static final String DB_NAME = "notepad.db";
  11. // 数据表名称
  12. private static final String TABLE_NAME = "data";
  13. // 数据库版本
  14. private static final int DB_VERSION = 1;
  15. // 创建数据表SQL语句
  16. private static final String CREATE_TABLE = 
  17. "CREATE TABLE " + TABLE_NAME
  18. + "(" 
  19. + "_id INTEGER PRIMARY KEY ,"
  20. + "title VARCHAR(50) NOT NULL ," 
  21. + "body" 
  22. + ")";
  23. // 删除数据表SQL语句
  24. private static final String DROP_TABLE = "DROP IF TABLE EXISTS "
  25. + TABLE_NAME;
  26. // 定义SQLiteDatabase
  27. private SQLiteDatabase db;

  28. public MyDatabaseHepler(Context context)
  29. {
  30. super(context, DB_NAME, null, DB_VERSION);
  31. System.out.println("--------------------数据库创建成功!!");
  32. }

  33. @Override
  34. public void onCreate(SQLiteDatabase db)
  35. {
  36. // 创建数据表
  37. db.execSQL(CREATE_TABLE);
  38. System.out.println("------------------------数据表创建成功!!");
  39. }

  40. @Override
  41. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  42. {
  43. db.execSQL(DROP_TABLE);
  44. System.out.println("-------------------------数据表更新成功!!");
  45. onCreate(db);
  46. }

  47. /**
  48. * 插入数据

  49. * @param values
  50. */
  51. public void insert(ContentValues values)
  52. {
  53. SQLiteDatabase db = getWritableDatabase();

  54. db.insert(TABLE_NAME, null, values);

  55. db.close();
  56. }

  57. /**
  58. * 查询数据
  59. */
  60. public Cursor query()
  61. {
  62. SQLiteDatabase db = getWritableDatabase();

  63. Cursor cursor = db
  64. .query(TABLE_NAME, null, null, null, null, null, null);

  65. return cursor;
  66. }

  67. /**
  68. * 删除数据
  69. */
  70. public void delete(long id)
  71. {
  72. if (db == null)
  73. {
  74. db = getWritableDatabase();
  75. }
  76. db.delete(TABLE_NAME, "_id=?",new String[]{String.valueOf(id)});
  77. }

  78. /**
  79. * 更新数据
  80. */
  81. public void update(long id){
  82. db=getWritableDatabase();
  83. ContentValues values=new ContentValues();

  84. // db.update(TABLE_NAME, values, whereClause, whereArgs)
  85. }
  86. }

复制代码
这是MainActivity类
  1. package com.hgq.demo;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import android.annotation.SuppressLint;
  5. import android.app.AlertDialog;
  6. import android.app.ListActivity;
  7. import android.content.DialogInterface;
  8. import android.content.DialogInterface.OnClickListener;
  9. import android.content.Intent;
  10. import android.database.Cursor;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.os.Bundle;
  13. import android.view.ContextMenu;
  14. import android.view.ContextMenu.ContextMenuInfo;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.widget.AdapterView;
  19. import android.widget.AdapterView.AdapterContextMenuInfo;
  20. import android.widget.AdapterView.OnItemClickListener;
  21. import android.widget.ListView;
  22. import android.widget.SimpleCursorAdapter;

  23. import com.hgq.hepler.MyDatabaseHepler;

  24. public class MainActivity extends ListActivity
  25. {
  26. // 选项菜单
  27. private static final int DB_INSERT = Menu.FIRST + 1;

  28. // 上下文菜单
  29. private static final int DB_DELETE = Menu.FIRST + 2;

  30. // 定义帮助类
  31. private MyDatabaseHepler dbHepler;
  32. SQLiteDatabase db;

  33. @Override
  34. public void onCreate(Bundle savedInstanceState)
  35. {
  36. super.onCreate(savedInstanceState);
  37. init();
  38. }

  39. public void init()
  40. {
  41. // 注册上下文菜单
  42. registerForContextMenu(getListView());

  43. dbHepler = new MyDatabaseHepler(getApplicationContext());

  44. Cursor cursor = dbHepler.query();

  45. // 游标适配器
  46. SimpleCursorAdapter adapter = new SimpleCursorAdapter(
  47. MainActivity.this
  48. , R.layout.line
  49. , cursor
  50. , new String[]{ "_id", "title", "body" }
  51. , new int[]{ R.id.id, R.id.title, R.id.body }
  52. );

  53. ListView list = getListView();
  54. list.setAdapter(adapter);
  55. }

  56. /**
  57. * 删除
  58. */
  59. public void delete(long id)
  60. {
  61. final long temp=id;
  62. AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
  63. builder.setTitle("删除记录")
  64. .setMessage("确定删除吗?")
  65. .setIcon(R.drawable.icon_del)
  66. .setPositiveButton("确定", new DialogInterface.OnClickListener()
  67. {

  68. @Override
  69. public void onClick(DialogInterface dialog, int which)
  70. {
  71. dbHepler.delete(temp);
  72. init();
  73. }
  74. }).setNegativeButton("取消", null).create();
  75. builder.show();
  76. }

  77. /**
  78. * 更新
  79. */
  80. public void update(){
  81. Intent intent=getIntent();
  82. String title=intent.getStringExtra("title");
  83. String body=intent.getStringExtra("body");

  84. System.out.println("title"+title+",body"+body);
  85. }

  86. /**
  87. * 选项菜单
  88. */
  89. @Override
  90. public boolean onCreateOptionsMenu(Menu menu)
  91. {
  92. menu.add(0, DB_INSERT, 0, "新建记事本").setIcon(R.drawable.icon_add);
  93. return super.onCreateOptionsMenu(menu);
  94. }

  95. /**
  96. * 选项菜单监听
  97. */
  98. @Override
  99. public boolean onOptionsItemSelected(MenuItem item)
  100. {
  101. switch (item.getItemId())
  102. {
  103. case DB_INSERT:
  104. Intent intent = new Intent(MainActivity.this, AddNotepad.class);
  105. startActivityForResult(intent, 0);
  106. break;
  107. }

  108. return super.onOptionsItemSelected(item);
  109. }

  110. /**
  111. * 上下文菜单
  112. */
  113. @Override
  114. public void onCreateContextMenu(ContextMenu menu, View v,
  115. ContextMenuInfo menuInfo)
  116. {
  117. menu.setHeaderTitle("信息操作");
  118. menu.add(0, DB_DELETE, 0, "删除");

  119. super.onCreateContextMenu(menu, v, menuInfo);
  120. }

  121. /**
  122. * 上下文菜单监听
  123. */
  124. @Override
  125. public boolean onContextItemSelected(MenuItem item)
  126. {
  127. switch (item.getItemId())
  128. {
  129. case DB_DELETE:
  130. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
  131. .getMenuInfo();
  132. delete(info.id);
  133. break;
  134. }

  135. return super.onContextItemSelected(item);
  136. }

  137. }
复制代码
这是AddActivity类
  1. package com.hgq.demo;

  2. import android.app.Activity;
  3. import android.content.ContentValues;
  4. import android.content.Intent;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;

  12. import com.hgq.hepler.MyDatabaseHepler;

  13. public class AddNotepad extends Activity
  14. {
  15. // 定于组件
  16. private EditText editTitle, editBody;
  17. private Button butAdd;

  18. // 定义帮助类
  19. private MyDatabaseHepler dbHepler;
  20. // 定义数据类
  21. private SQLiteDatabase db;
  22. // 数据表名称
  23. private static final String TABLE_NAME = "data";

  24. private String title;
  25. private String body;

  26. @Override
  27. protected void onCreate(Bundle savedInstanceState)
  28. {
  29. super.onCreate(savedInstanceState);
  30. super.setContentView(R.layout.add);

  31. init();
  32. }

  33. public void init()
  34. {
  35. // 实例化帮助类
  36. dbHepler = new MyDatabaseHepler(getApplicationContext());
  37. dbHepler.getWritableDatabase();
  38. // 取出组件
  39. editTitle = (EditText) findViewById(R.id.editTitle);
  40. editBody = (EditText) findViewById(R.id.editBody);
  41. butAdd = (Button) findViewById(R.id.butAdd);

  42. // 设置添加按钮监听
  43. butAdd.setOnClickListener(new OnClickListener()
  44. {

  45. @Override
  46. public void onClick(View v)
  47. {
  48. title = editTitle.getText().toString();
  49. body = editBody.getText().toString();

  50. ContentValues cValues = new ContentValues();
  51. cValues.put("title", title);
  52. cValues.put("body", body);

  53. dbHepler.insert(cValues);

  54. // 提示保存成功
  55. Toast.makeText(AddNotepad.this, "保存成功", 8000).show();

  56. // 定义 Intent
  57. Intent intent = new Intent(AddNotepad.this, MainActivity.class);
  58. intent.putExtra("title", title);
  59. intent.putExtra("body", body);

  60. // 启动Activity
  61. startActivityForResult(intent, 0);
  62. }
  63. });

  64. }

  65. public void update(long id){

  66. }

  67. }

你可能感兴趣的:(Listview中取数据)