前言:   这个博客主要实现listview的跳转并实现对数据库内容的更新并显示到listview上,还没有实现listview的实时更新和listview具体线条的添加(接下来的几篇博客会实现),如果对于基本布局不是很了解的可以看我上一篇的博客,那么话不多少,步入正题。

一、设置listview中item的点击事件

 lv.setOnItemClickListener(new OnItemClickListener() {

             @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
int id1 = list.get(position).getId();
String thing = list.get(position).getT_name();
String place = list.get(position).getT_place();
String time = list.get(position).getTime();
bundle.putInt("id", id1);
bundle.putString("thing", thing);
bundle.putString("place", place);
bundle.putString("time", time);
Intent intent = new Intent();
intent.putExtras(bundle); intent.setClass(MainActivity.this, Updata_delete.class); startActivity(intent); }
});

二、点击item之后会跳到updata_delete页面,就如同这个页面的名字一样,我们要在这个页面实现数据库数据的更新和删除(在这只实现更新),在写代码的时候我们需要设置几个EditTextView用来接收从listview传来的数据,同时也能让用户进行修改操作具体布局如下:

 <?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="wrap_content"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="事件名:"
android:textSize="30dp" /> <EditText
android:id="@+id/et_things1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入事件名"
android:textSize="25dp" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="时间:"
android:textSize="30dp" /> <EditText
android:id="@+id/et_time1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入时间"
android:textSize="25dp" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="地点:"
android:textSize="30dp" /> <EditText
android:id="@+id/et_place1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入地点"
android:textSize="25dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="update"
android:text="修改" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="delete"
android:text="删除" />
</LinearLayout> </LinearLayout>

三、剩下的就是实现接收数据并更新的操作代码了,具体解释:1、我在里面使用了onstart()方法来获取id,这个方法是在这个页面可见时就会调用(具体细节可参考http://www.cnblogs.com/fansen/p/5667450.html).2、当选择id作为筛选条件的时候,用一般的execsql()方法(因为内置的update方法里的参数是String类型,但是execsql()方法没有返回值,所以一定要通过测试最终是否成功更新来设置吐司),我想了很长时间,还是没能实现用内置方法来进行更新,可能是要重写这个方法吧。。。maybe。。。。

 package com.example.hhah;

 import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class Updata_delete extends Activity { private EditText et_time;
private EditText et_place;
private EditText et_things;
private MyOpenHelper myOpenHelper;
private int id; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_listview);
myOpenHelper = new MyOpenHelper(getApplicationContext());
et_things = (EditText) findViewById(R.id.et_things1);
et_place = (EditText) findViewById(R.id.et_place1);
et_time = (EditText) findViewById(R.id.et_time1);
Bundle bundle = getIntent().getExtras();
String thing = bundle.getString("thing");
String place = bundle.getString("place");
String time = bundle.getString("time");
et_things.setText(thing);
et_time.setText(place);
et_place.setText(time);
} @Override
protected void onStart() {
Bundle bundle = getIntent().getExtras();
id = bundle.getInt("id");
System.out.println(id);
super.onStart();
} public void update(View view) {
String thing = et_things.getText().toString().trim();
String place = et_place.getText().toString().trim();
String time = et_time.getText().toString().trim();
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
db.execSQL("update biao01 set t_name='" + thing + "',t_place='" + place + "',time='" + time + "' where _id='"
+ id + "'");
Toast.makeText(getApplicationContext(), "更新成功", 1).show();
/*
* ContentValues values=new ContentValues(); values.put("t_thing", thing);
* values.put("t_place", place); values.put("time", time); db.update("biao01",
* values, "_id=?",new String[] {});
*/ }
}

四、由于我在这个更新的代码编写过程中对布局,界面风格、表的结构(我加了主键)有了调整,所以之前的记账本开发(一)需要有一定的修改,我会进行修改。。。,之后的博客会对其他几项内容进行实现,并且实现listview的实时更新,还会不断美化布局。。。

五、稳定心态,不被外界干扰、厚积薄发!

2019-03-27

安卓入门——————简单记账本的开发(二)-点击listview跳转并实现数据的更新的更多相关文章

  1. 安卓入门——————简单记账本的开发(用sqlite存储数据)(一)

    设计思想————首先要确定有几个页面.和每个页面的大致布局 由于是入门,我也是学习了不是很长的时间,所以项目比较low.... 第一个页面,也就是打开APP的首页面: 今天这个博客,先实现添加功能!: ...

  2. javaweb学习总结(二十六)——jsp简单标签标签库开发(二)

    一.JspFragment类介绍 javax.servlet.jsp.tagext.JspFragment类是在JSP2.0中定义的,它的实例对象代表JSP页面中的一段符合JSP语法规范的JSP片段, ...

  3. javaweb(二十六)——jsp简单标签标签库开发(二)

    一.JspFragment类介绍 javax.servlet.jsp.tagext.JspFragment类是在JSP2.0中定义的,它的实例对象代表JSP页面中的一段符合JSP语法规范的JSP片段, ...

  4. 【搬砖】安卓入门(1)- Java开发入门

    01.01_计算机基础知识(计算机概述)(了解) A:什么是计算机?计算机在生活中的应用举例 计算机(Computer)全称:电子计算机,俗称电脑.是一种能够按照程序运行,自动.高速处理海量数据的现代 ...

  5. 【搬砖】安卓入门(3)- Java开发编程基础--循环控制语句

    04.01_Java语言基础(循环结构概述和for语句的格式及其使用) A:循环结构的分类 for(初始化表达式;条件表达式;循环后的操作表达式) { 循环体; } 复制代码 B:循环结构for语句的 ...

  6. 【搬砖】安卓入门(2)- Java开发编程基础--进制转换和运算符

    02.01_Java语言基础(常量的概述和使用)(掌握) A:什么是常量 在程序执行的过程中其值不可以发生改变 B:Java中常量的分类 字面值常量 自定义常量(面向对象部分讲) C:字面值常量的分类 ...

  7. 【搬砖】安卓入门(4)- Java开发编程基础--数组

    05.01_Java语言基础(数组概述和定义格式说明)(了解) A:为什么要有数组(容器) 为了存储同种数据类型的多个值 B:数组概念 数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器. ...

  8. 简单记账本APP开发二

    今天主要是进行了适配器的编写,数据库的创建以及对完善了业务逻辑,简单的APP到此已经通过测试可以使用.

  9. koa 基础(二十四)封装 DB 库 --- 新增数据、更新数据、删除数据

    1.根目录/module/db.js /** * DB库 */ var MongoClient = require('mongodb').MongoClient; var Config = requi ...

随机推荐

  1. python全栈开发 * mysql * 180828

    一.mysql概述\s 查看当前自己的信息1.mysql就是用于管理我们的文件的一个软件;2.mysql有两个软件: 服务器软件: socket服务端 本地文件操作 解析指令(mysql语句) 客户端 ...

  2. docker container 互联

    创建一个 network docker network create test-network 创建rocketmq docker run -d  -p 9876:9876 -p 10909:1090 ...

  3. graph engine

    有个侥幸的机会,参与了微软的项目,侥幸的接触了,graph engine图形数据库,感觉很是新颖,做点记录,和大家分享,理解有限,发现不足之处,还请指点. 微软发分布式图处理引擎GraphEngine ...

  4. Multi-Projector Based Display Code ---- Calibration

    Overview As mentioned previously, there are two main steps in generating a seamless display. The fir ...

  5. js 用touch事件实现简单tap

    function _tap(dom,callBack){ var startTime=0; var delayTime=200; var isMove=false; dom.addEventListe ...

  6. springboot整合微软的ad域,采用ldap的api来整合,实现用户登录验证、

    流程: 1.用户调登录接口,传用户名和密码2.用户名和密码在ad验证,验证通过后,返回当前用户的相关信息.(注:ldap为java自带的api不需要maven引入其他的)3.根据返回的用户信息,实现自 ...

  7. STM32 STOP模式唤醒后的时钟

    进了STOP模式后,PLL停掉了,所以,如果开始的时钟配置,用的是PLL,那么唤醒后,需要重新配置RCC. 如果使用的是PLL,及时是用MSI作为时钟源,放大出来的,比如4M的MSI,PLL放大到48 ...

  8. [emacs] emacs调整C代码的缩进格式等

    相比于VIM,emacs在默认配置下写C代码还真是不好用. 好多东西都要调整,其中最麻烦的就是缩进.调啊调,调了好久. 勉强形成一个配置如下: (add-hook 'c-mode-hook (lamb ...

  9. Autohotkey常用命令

    //输入密码#1::send test1234sleep 600send {enter}return //打开程序; win + t: open total cmd#t::IfWinNotExist ...

  10. 洛谷P4051 字符加密 [JSOI2007] SA

    正解:SA 解题报告: 传送门! 和工艺那题有点儿像鸭,,,反正肯定就都想到倍长然后SA拍个序嘛先 然后就做完了,,,我发现SA的题刷起来特别susi,,,基本上紫题级别的都just一个模板就欧克了最 ...