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

前言:   这个博客主要实现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

上一篇:Got fatal error 1236原因和解决方法


下一篇:vue 父子组件