Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize notepad function with listview and SQLite in android

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to achieve notepad function with listview and SQLite in android. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Effect:

MainActivity:

Import android.app.Activity; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import java.util.ArrayList Import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {private ListView listview; private SimpleAdapter simple_adapter; private List dataList; private Button addNote; private TextView tv_content; private NoteDateBaseHelper DbHelper; private SQLiteDatabase DB; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); InitView () Update listview @ Override protected void onStart () {super.onStart (); RefreshNotesList ();} private void InitView () {tv_content = (TextView) findViewById (R.id.tv_content); listview = (ListView) findViewById (R.id.listview); dataList = new ArrayList (); addNote = (Button) findViewById (R.id.btn_editnote); DbHelper = new NoteDateBaseHelper (this); DB = DbHelper.getReadableDatabase () when activity is displayed Listview.setOnItemClickListener (this); listview.setOnItemLongClickListener (this); addNote.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {Intent intent = new Intent (MainActivity.this, noteEdit.class); Bundle bundle = new Bundle (); bundle.putString ("info", "); bundle.putInt (" enter_state ", 0); intent.putExtras (bundle); startActivity (intent);}}) } / / refresh listview public void RefreshNotesList () {/ / if dataList already has content, delete / / and update simp_adapter int size = dataList.size (); if (size > 0) {dataList.removeAll (dataList); simple_adapter.notifyDataSetChanged ();} / / read information from the database Cursor cursor = DB.query ("note", null, null); startManagingCursor (cursor) While (cursor.moveToNext ()) {String name = cursor.getString (cursor.getColumnIndex ("content")); String date = cursor.getString (cursor.getColumnIndex ("date")); Map map = new HashMap (); map.put ("tv_content", name); map.put ("tv_date", date); dataList.add (map) } simple_adapter = new SimpleAdapter (this, dataList, R.layout.item, new String [] {"tv_content", "tv_date"}, new int [] {R.id.tv_content, R.id.tv_date}); listview.setAdapter (simple_adapter) } / / Click the click listening event @ Override public void onItemClick (AdapterView arg0, View arg1, int arg2, long arg3) {/ / to get the contents of this item in listview String content = listview.getItemAtPosition (arg2) + "; String content1 = content.substring (content.indexOf (" = ") + 1, content.indexOf (", ")); Intent myIntent = new Intent (MainActivity.this, noteEdit.class); Bundle bundle = new Bundle () Bundle.putString ("info", content1); bundle.putInt ("enter_state", 1); myIntent.putExtras (bundle); startActivity (myIntent);} / / Click a long click event in listview @ Override public boolean onItemLongClick (AdapterView arg0, View arg1, final int arg2, long arg3) {Builder builder = new Builder (this); builder.setTitle ("Delete this log"); builder.setMessage ("confirm deletion?") ; builder.setPositiveButton ("OK", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {/ / get the contents of this item in listview / / refresh the contents of listview after deleting the row String content = listview.getItemAtPosition (arg2) + "; String content1 = content.substring (content.indexOf (" = ") + 1, content.indexOf (", ")) DB.delete ("note", "content =?", new String [] {content1}); RefreshNotesList ();}}); builder.setNegativeButton ("cancel", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); builder.create (); builder.show (); return true;}

NoteDateBaseHelper:

Import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class NoteDateBaseHelper extends SQLiteOpenHelper {public static final String CreateNote = "create table note (" + "id integer primary key autoincrement," + "content text," + "date text)"; public NoteDateBaseHelper (Context context) {super (context, "note", null, 1);} @ Override public void onCreate (SQLiteDatabase db) {db.execSQL (CreateNote) } @ Override public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2) {/ / TODO Auto-generated method stub}}

NoteEdit:

Import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; public class noteEdit extends Activity implements OnClickListener {private TextView tv_date; private EditText et_content; private Button btn_ok; private Button btn_cancel; private NoteDateBaseHelper DBHelper Public int enter_state = 0R.layout.edit / used to distinguish between creating a new note or changing the original note public String last_content;// to get edittext content @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.edit); InitView ();} private void InitView () {tv_date = (TextView) findViewById (R.id.tv_date); et_content = (EditText) findViewById (R.id.et_content) Btn_ok = (Button) findViewById (R.id.btn_ok); btn_cancel = (Button) findViewById (R.id.btn_cancel); DBHelper = new NoteDateBaseHelper (this); / / get the time Date date = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm"); String dateString = sdf.format (date); tv_date.setText (dateString); / / receive content and id Bundle myBundle = this.getIntent (). GetExtras () Last_content = myBundle.getString ("info"); enter_state = myBundle.getInt ("enter_state"); et_content.setText (last_content); btn_cancel.setOnClickListener (this); btn_ok.setOnClickListener (this);} @ Override public void onClick (View view) {switch (view.getId ()) {case R.id.btn_ok: SQLiteDatabase db = DBHelper.getReadableDatabase () / / get edittext content String content = et_content.getText () .toString (); / / add a new log if (enter_state = = 0) {if (! content.equals (")) {/ / get the time at this time Date date = new Date (); SimpleDateFormat sdf = new SimpleDateFormat (" yyyy-MM-dd HH:mm "); String dateString = sdf.format (date) / / add information to the database ContentValues values = new ContentValues (); values.put ("content", content); values.put ("date", dateString); db.insert ("note", null, values); finish ();} else {Toast.makeText (noteEdit.this, "Please enter your content!" , Toast.LENGTH_SHORT) .show ();}} / view and modify an existing log else {ContentValues values = new ContentValues (); values.put ("content", content); db.update ("note", values, "content =?", new String [] {last_content}); finish ();} break; case R.id.btn_cancel: finish () Break;}

Activity_main:

Edit:

Item:

These are all the contents of the article "how to realize the notepad function of listview and SQLite in android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report