android

    xiaoxiao2025-12-30  8

    >>>实现功能

    主要是对对SQLite编辑和修改 分析代码结构,需要NotePadList类用于展示笔记,NotePadAdd类用于实现笔记的添加,需要NotePadDetil实现修改笔记,需要NotePadProvieder实现对数据库操作。 ------------------------------------------------------- 第一步,编写NotePadList xml部分 <RelativeLayout 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=".NotePadList" > <ListView android:id="@+id/id_listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> java部分 public class NotePadList extends Activity { private NotePadProvider nProvider = new NotePadProvider(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setNoteListView(); } SimpleCursorAdapter adapter; private void setNoteListView() { c = qurydata(); //lsitviw ListView lsv = (ListView) findViewById(R.id.id_listview); //simpleadapter adapter=new SimpleCursorAdapter(this, R.layout.notepadlist, c, new String[]{"content","created"}, new int[]{R.id.lsit_textview,R.id.lsit_textview2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lsv.setAdapter(adapter); lsv.setOnItemClickListener(new OnItemClickListener() { //长按实现页面跳转,到notepaddetil @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { c.moveToPosition(position);//获取点击位置的Cursor String content = c.getString(c.getColumnIndex("content"));//查询取值 Intent intent = new Intent(NotePadList.this, NotepadDetil.class); intent.putExtra("contentKey",content);//通过intent传递相应的值 intent.putExtra("idKey",String.valueOf(position+1)); startActivityForResult(intent, 100);//传递请求码以便执行刷新 } }); } Cursor c;/**查询*/ private Cursor qurydata() { String sql="select * from notetab"; c =nProvider.query(sql, null); return c; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) {//点击添加按钮菜单,跳转添加界面 if (item.getItemId() == R.id.noteAddId) { startActivityForResult(new Intent(this, NotePadAdd.class), 100);//请求码 } return super.onOptionsItemSelected(item); } /**刷新*/ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode==100&&resultCode==200) { c = qurydata();<span style="font-family: Arial, Helvetica, sans-serif;">//再次获取</span> adapter.changeCursor(c); } } }menu-xml <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/noteAddId" android:orderInCategory="100" android:showAsAction="always" android:title="添加" android:icon="@android:drawable/ic_menu_add"/> </menu>------------------------------------------------------------------ 第二步,书写notePadProvider类,实现对数据库的操作 java代码 public class NotePadProvider { private DBHelper dbHelper; public NotePadProvider(Context context) { dbHelper=new DBHelper(context, "notepad.db", null, 1); } /**写入数据*/ public long insert(String table,ContentValues values){ SQLiteDatabase sdb=dbHelper.getWritableDatabase();//获取数据库的写入功能 long id=sdb.insert(table, null, values); sdb.close(); return id; } /**更新*/ public long updata(String table, ContentValues values, String whereClause, String[] whereArgs){ SQLiteDatabase sdb = dbHelper.getWritableDatabase(); long id = sdb.update(table, values, whereClause, whereArgs); return id; } /**查询*/ public Cursor query(String sql,String whereArgs[]){ SQLiteDatabase sdb= dbHelper.getReadableDatabase();<span style="font-family: Arial, Helvetica, sans-serif;">//获取数据库的读入功能</span> return sdb.rawQuery(sql, whereArgs); } /**操作SQLite的一个工具类*/ class DBHelper extends SQLiteOpenHelper{ public DBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } /**数据库创建时执行,且只执行一次*/ @Override public void onCreate(SQLiteDatabase db) { String sql= "create table if not exists notetab(" + "_id integer primary key autoincrement," + "content text not null," + "created text not null)"; db.execSQL(sql); Log.i("TAG", "table create ok!"); } /**数据库版本升级时执行*/ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("TAG", "onUpgrade"); } } } ------------------------------------------------------------------- 第三步:编写NotePadAdd类,实现增加笔记功能 public class NotePadAdd extends Activity { private EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_note_pad); et = (EditText) findViewById(R.id.editText1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.note_pad, menu); return true; } @SuppressLint("SimpleDateFormat") @Override public boolean onOptionsItemSelected(MenuItem item) { NotePadProvider nProvider = new NotePadProvider(this); if (item.getItemId() == R.id.save) { //获得页面数据 String content = et.getText().toString(); //对数据进行非空验证 if (TextUtils.isEmpty(content)) { et.setError("null"); return true; } //将数据写到数据库 ContentValues values = new ContentValues(); values.put("content", content); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); values.put("created", sdf.format(new Date())); long id = nProvider.insert("notetab", values); if (id==-1) { Toast.makeText(this, "insert error", 0).show(); return true; } //关闭当前页面 setResult(200);//跳转回notepadlist主界面的时候,自动刷新主界面 finish(); } return super.onOptionsItemSelected(item); } }xml <RelativeLayout 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=".NotePadAdd" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="22dp" android:layout_marginTop="31dp" android:ems="10" android:hint="输入"> <requestFocus /> </EditText> </RelativeLayout>menu <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/save" android:orderInCategory="100" android:showAsAction="always" android:title="保存" android:icon="@android:drawable/ic_menu_save"></item> </menu>--------------------------------------------------------------------- 第四步,编写NotePadDetil,实现修改 java public class NotepadDetil extends Activity { private EditText et; private MenuItem et_save; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notepad_detil); initView(); et.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { et.setFocusableInTouchMode(true); et_save.setVisible(true); } }); } private void initView() { et = (EditText) findViewById(R.id.id_editText); Intent intent = getIntent(); String contentNew = intent.getStringExtra("contentKey"); String contentOld = et.getText().toString(); if (TextUtils.isEmpty(contentOld)) {//非空判断 et.setText(contentNew); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.notepad_detil, menu); et_save = menu.findItem(R.id.edt_save); et_save.setVisible(false); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.edt_save) { //获取当前页面的数据 String newcontent = et.getText().toString(); NotePadProvider nProvider=new NotePadProvider(this); ContentValues values=new ContentValues(); values.put("content", newcontent); Intent intent=getIntent(); String id=intent.getStringExtra("idKey"); nProvider.updata("notetab", values, "_id=?", new String[]{id}); //4.关闭页面 setResult(200);//跳转后刷新 finish(); } return super.onOptionsItemSelected(item); } }menu <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/edt_save" android:orderInCategory="100" android:showAsAction="always" android:title="保存" android:icon="@android:drawable/ic_menu_save"/> </menu> xml <RelativeLayout 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=".NotepadDetil" > <EditText android:layout_width="wrap_content" android:id="@+id/id_editText" android:layout_height="wrap_content" android:focusable="false" android:background="@null"/> </RelativeLayout>
    转载请注明原文地址: https://ju.6miu.com/read-1305474.html
    最新回复(0)