安卓入門——————簡單記賬本的開發(二)-點擊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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM