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

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

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

 1 lv.setOnItemClickListener(new OnItemClickListener() {
 2 
 3             @Override
 4             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 5                 Bundle bundle = new Bundle();
 6                 int id1 = list.get(position).getId();
 7                 String thing = list.get(position).getT_name();
 8                 String place = list.get(position).getT_place();
 9                 String time = list.get(position).getTime();
10                 bundle.putInt("id", id1);
11                 bundle.putString("thing", thing);
12                 bundle.putString("place", place);
13                 bundle.putString("time", time);
14                 Intent intent = new Intent();
15                 intent.putExtras(bundle);
16 
17                 intent.setClass(MainActivity.this, Updata_delete.class);
18 
19                 startActivity(intent);
20 
21             }
22         });

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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:text="事件名:"
11         android:textSize="30dp" />
12 
13     <EditText
14         android:id="@+id/et_things1"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:hint="请输入事件名"
18         android:textSize="25dp" />
19 
20     <TextView
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="时间:"
24         android:textSize="30dp" />
25 
26     <EditText
27         android:id="@+id/et_time1"
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content"
30         android:hint="请输入时间"
31         android:textSize="25dp" />
32 
33     <TextView
34         android:layout_width="match_parent"
35         android:layout_height="wrap_content"
36         android:text="地点:"
37         android:textSize="30dp" />
38 
39     <EditText
40         android:id="@+id/et_place1"
41         android:layout_width="match_parent"
42         android:layout_height="wrap_content"
43         android:hint="请输入地点"
44         android:textSize="25dp" />
45 
46     <LinearLayout
47         android:layout_width="match_parent"
48         android:layout_height="wrap_content" >
49 
50         <Button
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content"
53             android:layout_weight="1"
54             android:onClick="update"
55             android:text="修改" />
56 
57         <Button
58             android:layout_width="wrap_content"
59             android:layout_height="wrap_content"
60             android:layout_weight="1"
61             android:onClick="delete"
62             android:text="删除" />
63     </LinearLayout>
64 
65 </LinearLayout>

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

 1 package com.example.hhah;
 2 
 3 import android.app.Activity;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 import android.widget.Toast;
 9 
10 public class Updata_delete extends Activity {
11 
12     private EditText et_time;
13     private EditText et_place;
14     private EditText et_things;
15     private MyOpenHelper myOpenHelper;
16     private int id;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.item_listview);
22         myOpenHelper = new MyOpenHelper(getApplicationContext());
23         et_things = (EditText) findViewById(R.id.et_things1);
24         et_place = (EditText) findViewById(R.id.et_place1);
25         et_time = (EditText) findViewById(R.id.et_time1);
26         Bundle bundle = getIntent().getExtras();
27         String thing = bundle.getString("thing");
28         String place = bundle.getString("place");
29         String time = bundle.getString("time");
30         et_things.setText(thing);
31         et_time.setText(place);
32         et_place.setText(time);
33     }
34 
35     @Override
36     protected void onStart() {
37         Bundle bundle = getIntent().getExtras();
38         id = bundle.getInt("id");
39         System.out.println(id);
40         super.onStart();
41     }
42 
43     public void update(View view) {
44         String thing = et_things.getText().toString().trim();
45         String place = et_place.getText().toString().trim();
46         String time = et_time.getText().toString().trim();
47         SQLiteDatabase db = myOpenHelper.getWritableDatabase();
48         db.execSQL("update biao01 set t_name='" + thing + "',t_place='" + place + "',time='" + time + "' where _id='"
49                 + id + "'");
50         Toast.makeText(getApplicationContext(), "更新成功", 1).show();
51         /*
52          * ContentValues values=new ContentValues(); values.put("t_thing", thing);
53          * values.put("t_place", place); values.put("time", time); db.update("biao01",
54          * values, "_id=?",new String[] {});
55          */
56 
57     }
58 }

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

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

2019-03-27

原文地址:https://www.cnblogs.com/zhang188660586/p/10610834.html