android中listview与SQLite怎么实现记事本功能

发布时间:2022-04-07 17:13:53 作者:iii
来源:亿速云 阅读:137

今天小编给大家分享一下android中listview与SQLite怎么实现记事本功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

效果: 

android中listview与SQLite怎么实现记事本功能

android中listview与SQLite怎么实现记事本功能

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显示的时候更新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已经有的内容,全部删掉 
  //并且更新simp_adapter 
  int size = dataList.size(); 
  if (size > 0) { 
   dataList.removeAll(dataList); 
   simple_adapter.notifyDataSetChanged(); 
  } 
 
  //从数据库读取信息 
  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); 
 } 
 
 
 
 // 点击listview中某一项的点击监听事件 
 @Override 
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
  //获取listview中此个item中的内容 
  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); 
 
 } 
 
 // 点击listview中某一项长时间的点击事件 
 @Override 
 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, 
         long arg3) { 
  Builder builder = new Builder(this); 
  builder.setTitle("删除该日志"); 
  builder.setMessage("确认删除吗?"); 
  builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
   @Override 
   public void onClick(DialogInterface dialog, int which) { 
    //获取listview中此个item中的内容 
    //删除该行后刷新listview的内容 
    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;//用来区分是新建一个note还是更改原来的note 
 public String last_content;//用来获取edittext内容 
 
 @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); 
 
  //获取此时时刻时间 
  Date date = new Date(); 
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
  String dateString = sdf.format(date); 
  tv_date.setText(dateString); 
 
  //接收内容和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内容 
    String content = et_content.getText().toString(); 
 
    // 添加一个新的日志 
    if (enter_state == 0) { 
     if (!content.equals("")) { 
      //获取此时时刻时间 
      Date date = new Date(); 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
      String dateString = sdf.format(date); 
 
      //向数据库添加信息 
      ContentValues values = new ContentValues(); 
      values.put("content", content); 
      values.put("date", dateString); 
      db.insert("note", null, values); 
      finish(); 
     } else { 
      Toast.makeText(noteEdit.this, "请输入你的内容!", Toast.LENGTH_SHORT).show(); 
     } 
    } 
    // 查看并修改一个已有的日志 
    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="编辑时间" 
   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="内容编辑:" 
   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>

以上就是“android中listview与SQLite怎么实现记事本功能”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. Android Studio怎样获取SQLite数据并显示到ListView上
  2. android中如何实现listview

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android listview sqlite

上一篇:Android Studio常用的快捷键功能是什么

下一篇:Android怎么通过XListView实现上拉加载下拉刷新功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》