android中l(wèi)istview與SQLite結(jié)合如何實(shí)現(xiàn)記事本功能-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)android中l(wèi)istview與SQLite結(jié)合如何實(shí)現(xiàn)記事本功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

目前創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、平遠(yuǎn)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

android記事本的demo在網(wǎng)上一搜一大堆,但是大神寫的demo往往功能太多導(dǎo)致新手難以著手,很難啃得動(dòng);而一些新手寫的demo又往往是東拼西湊,代碼很多都是copy的別人的,直接放在項(xiàng)目里面用,也不知道代碼有什么作用。往往代碼特別丑,重復(fù)性的代碼也比較多。

筆者近期學(xué)到此處,自己理解之后也還是打算寫個(gè)demo供新手學(xué)習(xí)一下。代碼說不上優(yōu)雅,但在筆者看來已經(jīng)盡力去讓人容易理解了。

為了便于新手學(xué)習(xí),在此也是羅列一下涉及的知識(shí)點(diǎn):
1、SQLite的基本使用,增刪查改
2、listview,adapeter的基本使用
3、activity生命周期
4、intent、bundle傳遞參數(shù)
5、AlertDialog的基本使用

另外還有一些零碎知識(shí)點(diǎn)都可以百度到。

遇到的問題:

SQlite有個(gè)問題,就是主鍵不能夠自動(dòng)排序。比如說主鍵id為1 2 3 4,共4條記錄?,F(xiàn)在刪除2 3,還剩下1 4記錄,當(dāng)再次插入時(shí),id會(huì)變成5,而不是2.假設(shè)在初始4條記錄的基礎(chǔ)上,把這4條記錄全都刪掉,再次插入時(shí),得到的id是5.
筆者在這點(diǎn)上也是花了比較久的時(shí)間,原本為了精簡代碼,想法是用listview中的arg2直接通過數(shù)據(jù)庫記錄的id進(jìn)行操作,但是由于SQLite的這個(gè)問題,所以這種方法就有問題了。
最終,筆者采用的是內(nèi)容搜索的方法,從listview的每個(gè)item中獲取內(nèi)容,然后到數(shù)據(jù)庫中通過內(nèi)容搜索該記錄,最后對(duì)其進(jìn)行操作。

效果:

android中l(wèi)istview與SQLite結(jié)合如何實(shí)現(xiàn)記事本功能

android中l(wèi)istview與SQLite結(jié)合如何實(shí)現(xiàn)記事本功能

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<Map<String, Object>> 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(); 
 } 
 
 //在activity顯示的時(shí)候更新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<Map<String, Object>>(); 
  addNote = (Button) findViewById(R.id.btn_editnote); 
  DbHelper = new NoteDateBaseHelper(this); 
  DB = DbHelper.getReadableDatabase(); 
 
  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); 
   } 
  }); 
 } 
 
 
 //刷新listview 
 public void RefreshNotesList() { 
  //如果dataList已經(jīng)有的內(nèi)容,全部刪掉 
  //并且更新simp_adapter 
  int size = dataList.size(); 
  if (size > 0) { 
   dataList.removeAll(dataList); 
   simple_adapter.notifyDataSetChanged(); 
  } 
 
  //從數(shù)據(jù)庫讀取信息 
  Cursor cursor = DB.query("note", null, null, null, null, null, null); 
  startManagingCursor(cursor); 
  while (cursor.moveToNext()) { 
   String name = cursor.getString(cursor.getColumnIndex("content")); 
   String date = cursor.getString(cursor.getColumnIndex("date")); 
   Map<String, Object> map = new HashMap<String, Object>(); 
   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); 
 } 
 
 
 
 // 點(diǎn)擊listview中某一項(xiàng)的點(diǎn)擊監(jiān)聽事件 
 @Override 
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
  //獲取listview中此個(gè)item中的內(nèi)容 
  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); 
 
 } 
 
 // 點(diǎn)擊listview中某一項(xiàng)長時(shí)間的點(diǎn)擊事件 
 @Override 
 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, 
         long arg3) { 
  Builder builder = new Builder(this); 
  builder.setTitle("刪除該日志"); 
  builder.setMessage("確認(rèn)刪除嗎?"); 
  builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { 
   @Override 
   public void onClick(DialogInterface dialog, int which) { 
    //獲取listview中此個(gè)item中的內(nèi)容 
    //刪除該行后刷新listview的內(nèi)容 
    String content = listview.getItemAtPosition(arg2) + ""; 
    String content1 = content.substring(content.indexOf("=") + 1, 
      content.indexOf(",")); 
    DB.delete("note", "content = ?", new String[]{content1}); 
    RefreshNotesList(); 
   } 
  }); 
  builder.setNegativeButton("取消", 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 = 0;//用來區(qū)分是新建一個(gè)note還是更改原來的note 
 public String last_content;//用來獲取edittext內(nèi)容 
 
 @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); 
 
  //獲取此時(shí)時(shí)刻時(shí)間 
  Date date = new Date(); 
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
  String dateString = sdf.format(date); 
  tv_date.setText(dateString); 
 
  //接收內(nèi)容和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(); 
    // 獲取edittext內(nèi)容 
    String content = et_content.getText().toString(); 
 
    // 添加一個(gè)新的日志 
    if (enter_state == 0) { 
     if (!content.equals("")) { 
      //獲取此時(shí)時(shí)刻時(shí)間 
      Date date = new Date(); 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
      String dateString = sdf.format(date); 
 
      //向數(shù)據(jù)庫添加信息 
      ContentValues values = new ContentValues(); 
      values.put("content", content); 
      values.put("date", dateString); 
      db.insert("note", null, values); 
      finish(); 
     } else { 
      Toast.makeText(noteEdit.this, "請(qǐng)輸入你的內(nèi)容!", Toast.LENGTH_SHORT).show(); 
     } 
    } 
    // 查看并修改一個(gè)已有的日志 
    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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
 
 <TextView 
  android:layout_height="wrap_content" 
  android:layout_width="fill_parent" 
  android:text="記事本" 
  android:textStyle="bold" 
  android:textSize="22sp" 
  android:padding="15dp" 
  android:background="#000" 
  android:textColor="#fff" 
  /> 
 
 <LinearLayout 
  android:layout_width="fill_parent" 
  android:layout_height="0dp" 
  android:layout_weight="1" > 
 
  <ListView 
   android:id="@+id/listview" 
   android:layout_margin="5dp" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" > 
  </ListView> 
 </LinearLayout> 
 
 <Button 
  android:id="@+id/btn_editnote" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="添加備忘錄" 
  android:padding="10dp" 
  android:textSize="20sp" /> 
 
</LinearLayout>

edit:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
 
 <LinearLayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:background="#000" 
  android:orientation="vertical" 
 
  android:padding="15dp"> 
 
  <TextView 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="編輯備忘錄" 
   android:textColor="#fff" 
   android:textSize="22sp" 
   android:textStyle="bold" /> 
 
  <TextView 
   android:id="@+id/tv_date" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:gravity="end" 
   android:text="編輯時(shí)間" 
   android:textColor="#fff" /> 
 </LinearLayout> 
 
 <LinearLayout 
  android:layout_width="match_parent" 
  android:layout_height="0dp" 
  android:layout_weight="1" 
  android:padding="10dp" 
  android:orientation="vertical"> 
  <TextView 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="內(nèi)容編輯:" 
   android:textColor="#000" 
   android:textSize="20sp" 
   android:layout_margin="10dp" 
   android:textStyle="bold" /> 
 
  <EditText 
   android:id="@+id/et_content" 
   android:layout_width="match_parent" 
   android:layout_height="0dp" 
   android:layout_weight="1" 
   android:background="@drawable/edit_text_style" 
   android:gravity="start" 
   android:hint="此處記錄備忘事件" 
   android:textSize="20sp" /> 
 
  <LinearLayout 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:orientation="horizontal"> 
 
   <Button 
    android:id="@+id/btn_cancel" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="取消" /> 
 
   <Button 
    android:id="@+id/btn_ok" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="保存" /> 
 
  </LinearLayout> 
 </LinearLayout> 
 
</LinearLayout>

item:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:padding="10dp" 
 android:orientation="vertical"> 
 
 <TextView 
  android:id="@+id/tv_content" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:singleLine="true" 
  android:textSize="20sp" 
  android:textColor="#000" 
  android:text="Large Text" /> 
 
 <TextView 
  android:id="@+id/tv_date" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="TextView" /> 
 
</LinearLayout>

關(guān)于“android中l(wèi)istview與SQLite結(jié)合如何實(shí)現(xiàn)記事本功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

當(dāng)前文章:android中l(wèi)istview與SQLite結(jié)合如何實(shí)現(xiàn)記事本功能-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article22/dddecc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站內(nèi)鏈、動(dòng)態(tài)網(wǎng)站、網(wǎng)站導(dǎo)航、App設(shè)計(jì)、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名