listview和sqlist實現學生列表增刪改查
實現介面和程式碼
總專案結構
student實體類
public class student implements Serializable{ private int id; private String name; private String sex; private int age; student(int i,String n,String s,int a){ id=i;name=n;sex=s;age=a; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
MySQLiteAccess (需要繼承SQLiteOpenHelper)
package com.example.databasetest; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteAccess extends SQLiteOpenHelper{ /** * * @param context :上下文 * @param name:資料庫名稱 * @param factory * @param version:版本號 */ public MySQLiteAccess(Context context,int version) { super(context, "stuAdmin.db", null, version); } /** * 資料庫檔案建立成功後呼叫 * SQL操作的語句 * @param db */ @Override public void onCreate(SQLiteDatabase db) { System.out.println("資料庫建立"); db.execSQL("create table student(\n"+ "id integer,\n"+ "name text,\n"+ "sex text,\n"+ "age integer\n"+ ");"); } /** * 在更新資料庫後呼叫方法 * @param db * @param oldVersion 舊版本 * @param newVersion 新版本 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("資料庫更新"); } }
DBOperate (裡面定義對資料庫的各操作)
package com.example.databasetest; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; public class DBOperate { private MySQLiteAccess mySQLiteAccess; private SQLiteDatabase database; public void test(Context context){ mySQLiteAccess=new MySQLiteAccess(context,3); database=mySQLiteAccess.getReadableDatabase(); } public void insert(student s){ database.execSQL("insert into student values(?,?,?,?)",new Object[] {s.getId(),s.getName(),s.getSex(),s.getAge()}); System.out.println("插入資料成功"); } public void delete(int id){ Integer I=new Integer(id); database.execSQL("delete from student where id=?",new Object[]{I}); } public List<student> searchByName(String key){ List<student> listByName=new ArrayList<student>(); key="%"+key+"%"; Cursor cursor=database.rawQuery("select * from student where name like?",new String[]{key}); while(cursor.moveToNext()){ int id=cursor.getInt(cursor.getColumnIndex("id")); String name=cursor.getString(cursor.getColumnIndex("name")); String sex=cursor.getString(cursor.getColumnIndex("sex")); int age=cursor.getInt(cursor.getColumnIndex("age")); listByName.add(new student(id,name,sex,age)); } return listByName; } public List<student> queryAll(){ List<student> stus=new ArrayList<student>(); Cursor cursor=database.rawQuery("select * from student",null); while(cursor.moveToNext()){ int id=cursor.getInt(cursor.getColumnIndex("id")); String name=cursor.getString(cursor.getColumnIndex("name")); String sex=cursor.getString(cursor.getColumnIndex("sex")); int age=cursor.getInt(cursor.getColumnIndex("age")); stus.add(new student(id,name,sex,age)); } return stus; } public void update(student stu) { database.execSQL("update student set name=?,sex=?,age=? where id=?" ,new Object[] { stu.getName(), stu.getSex(), stu.getAge(),stu.getId() }); } public List<student> searchByAll(String key){ List<student> listByAll=new ArrayList<student>(); key="%"+key+"%"; Cursor cursor=database.rawQuery("select * from student where id like? or name like? or sex like? or age like?" ,new String[]{key,key,key,key}); while(cursor.moveToNext()){ int id=cursor.getInt(cursor.getColumnIndex("id")); String name=cursor.getString(cursor.getColumnIndex("name")); String sex=cursor.getString(cursor.getColumnIndex("sex")); int age=cursor.getInt(cursor.getColumnIndex("age")); listByAll.add(new student(id,name,sex,age)); } return listByAll; } }
MyAdapter (自定義介面卡,繼承BaseAdapter)
package com.example.databasetest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter{
private List<student> list;//資料來源
private LayoutInflater inflater;//佈局載入器
DBOperate dbOperate=new DBOperate();
//有參的建構函式,為資料來源,上下文物件複製,同時例項化佈局載入器
public MyAdapter(List<student> list,Context context) {
this.list=list;
inflater=LayoutInflater.from(context);
dbOperate.test(context); //初始化一下資料庫,不然後面delete用的時候database是空的
}
//有多少條資料,需要建立多少個item佈局
@Override
public int getCount() {
return list.size();
}
//返回position對應位置的資料
@Override
public Object getItem(int position) {
return list.get(position);
}
//返回position對應位置item的id
@Override
public long getItemId(int position) {
return position;
}
/**
* 具體定義載入item佈局,並將資料顯示到item佈局上的方法。
* @param position
* @param convertView
* @param parent
* @return
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//載入stulist佈局 將xml佈局載入到記憶體中,形成一個view
final View view=inflater.inflate(R.layout.stulist,null);
//例項化stulist佈局上的控制元件
TextView tv_stuID= (TextView) view.findViewById(R.id.tv_stuID);
TextView tv_stuName= (TextView) view.findViewById(R.id.tv_stuName);
TextView tv_stuSex= (TextView) view.findViewById(R.id.tv_stuSex);
TextView tv_stuAge= (TextView) view.findViewById(R.id.tv_stuAge);
Button btn_del=(Button) view.findViewById(R.id.btn_del);
Button btn_alert=(Button) view.findViewById(R.id.btn_alert);
//往控制元件上顯示資料
//獲取position對應位置的資料
final student oneStu= (student) getItem(position);
//不轉String就會android.content.res.Resources$NotFoundException: String resource ID #0x1
tv_stuID.setText(String.valueOf(oneStu.getId()));
tv_stuName.setText(oneStu.getName());
tv_stuSex.setText(oneStu.getSex());
tv_stuAge.setText(String.valueOf(oneStu.getAge()));
//設定按鈕的點選事件
btn_del.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dbOperate.delete(oneStu.getId());
list.remove(oneStu);
MyAdapter.this.notifyDataSetChanged();
}
});
btn_alert.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Context context=view.getContext();
Intent intent=new Intent();
intent.setClass(context,UpdateDialog.class);
intent.putExtra("altStu",oneStu);
((Activity)context).startActivity(intent);
}
});
return view;
}
}
stulist.xml (對應listView裡一行的佈局)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:textSize="25sp"
android:id="@+id/tv_stuID" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20sp"
android:layout_toRightOf="@id/tv_stuID"
android:layout_marginLeft="10dp"
android:id="@+id/tv_stuName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex"
android:textSize="20sp"
android:layout_toRightOf="@id/tv_stuName"
android:layout_marginLeft="10dp"
android:id="@+id/tv_stuSex"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/tv_stuSex"
android:id="@+id/tv_stuAge"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert"
android:layout_alignParentRight="true"
android:id="@+id/btn_alert" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="@id/btn_alert"
android:text="Delete"
android:id="@+id/btn_del" />
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.databasetest.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="學生系統" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_search"
android:layout_marginLeft="20dp"
android:layout_weight="10"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜尋"
android:id="@+id/btn_search"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="2"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="全部學生"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="270dp"
android:id="@+id/lv_stulist"
android:layout_marginTop="10dp">
</ListView>
<View
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="1dip"
android:background="#696969" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="ID"
android:id="@+id/et_id"
android:layout_weight="1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/et_name"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Sex"
android:id="@+id/et_sex"
android:layout_weight="1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"
android:id="@+id/et_age"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新增學生"
android:id="@+id/btn_add"/>
</LinearLayout>
MainActivity
package com.example.databasetest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ListView lv_stulist;
private EditText et_id;
private EditText et_name;
private EditText et_sex;
private EditText et_age;
private EditText et_search;
private Button btn_add;
private Button btn_search;
private List<student>list=new ArrayList<student>();
DBOperate dbOperate=new DBOperate();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_stulist=(ListView)findViewById(R.id.lv_stulist);
et_id=(EditText)findViewById(R.id.et_id);
et_name=(EditText)findViewById(R.id.et_name);
et_sex=(EditText)findViewById(R.id.et_sex);
et_age=(EditText)findViewById(R.id.et_age);
et_search=(EditText)findViewById(R.id.et_search);
btn_add=(Button)findViewById(R.id.btn_add);
btn_search=(Button)findViewById(R.id.btn_search);
btn_add.setOnClickListener(this);
btn_search.setOnClickListener(this);
dbOperate.test(this);
list=dbOperate.queryAll();
if(!list.isEmpty()){
showList(list);
}
}
private void showList(List<student> stus){
/*List<Map<String,Object>> stuMap=new ArrayList<Map<String,Object>>();
for(student s:stus){
Map<String,Object> m=new HashMap<String,Object>();
m.put("id",s.getId());
m.put("name",s.getName());
m.put("sex",s.getSex());
m.put("age",s.getAge());
stuMap.add(m);
}
String[] key={"id","name","sex","age"};
int[] value={R.id.tv_stuID,R.id.tv_stuName,R.id.tv_stuSex,R.id.tv_stuAge};*/
// SimpleAdapter simpleAdapter=new SimpleAdapter(this,stuMap,R.layout.stulist,key,value);
// lv_stulist.setAdapter(simpleAdapter);
MyAdapter myAdapter=new MyAdapter(stus,this);
lv_stulist.setAdapter(myAdapter);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_add){
int id=Integer.parseInt(et_id.getText().toString());
String name=et_name.getText().toString();
String sex=et_sex.getText().toString();
int age=Integer.parseInt(et_age.getText().toString());
dbOperate.insert(new student(id,name,sex,age));
list=dbOperate.queryAll();
if(!list.isEmpty()){
showList(list);
}
}else if(v.getId()==R.id.btn_search){
String search=et_search.getText().toString();
if(search.isEmpty())return;
Intent intent=new Intent();
intent.setClass(this,SearchActivity.class);
intent.putExtra("searchKey",search);
startActivity(intent);
}
}
}
activity_update_dialog.xml (修改介面佈局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.databasetest.UpdateDialog"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改資訊"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name :"
android:layout_marginLeft="20dp"
android:layout_weight="1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_alName"
android:layout_marginRight="30dp"
android:layout_weight="11"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex :"
android:layout_marginLeft="20dp"
android:layout_weight="1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_alSex"
android:layout_marginRight="30dp"
android:layout_weight="11"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age :"
android:layout_marginLeft="20dp"
android:layout_weight="1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_alAge"
android:inputType="number"
android:layout_marginRight="30dp"
android:layout_weight="11"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="25dp"
android:id="@+id/btn_submit"
android:text="確認提交"/>
</LinearLayout>
UpdateDialog (修改介面,本來想做彈出框樣式,由於一些問題沒解決,就這樣醜陋的實現了)
package com.example.databasetest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class UpdateDialog extends AppCompatActivity implements View.OnClickListener {
private EditText et_alName;
private EditText et_alSex;
private EditText et_alAge;
private Button btn_submit;
private student altStu=null;
DBOperate dbOperate=new DBOperate();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_dialog);
et_alName=(EditText)findViewById(R.id.et_alName);
et_alSex=(EditText)findViewById(R.id.et_alSex);
et_alAge=(EditText)findViewById(R.id.et_alAge);
btn_submit=(Button)findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(this);
initEditText();
dbOperate.test(this);
}
private void initEditText(){
Bundle bundle=getIntent().getExtras();
altStu=(student)bundle.get("altStu");
if(altStu!=null){
et_alName.setText(altStu.getName());
et_alSex.setText(altStu.getSex());
et_alAge.setText(String.valueOf(altStu.getAge()));
}
}
@Override
public void onClick(View v) {
Intent intent=new Intent();
String name=et_alName.getText().toString();
String sex=et_alSex.getText().toString();
int age=Integer.parseInt(et_alAge.getText().toString());
if(altStu!=null){
altStu.setName(name);
altStu.setSex(sex);
altStu.setAge(age);
dbOperate.update(altStu);
}
intent.setClass(this,MainActivity.class);
startActivity(intent);
finish();
}
}
activity_search.xml (搜尋介面佈局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.databasetest.SearchActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
android:gravity="left"
android:id="@+id/btn_return"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_searchText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="搜尋結果"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_searchSum"/>
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv_searchlist"
android:layout_marginTop="10dp">
</ListView>
</LinearLayout>
SearchActivity
package com.example.databasetest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_return;
private ListView lv_searchlist;
private TextView tv_searchText;
private TextView tv_searchSum;
DBOperate dbOperate=new DBOperate();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
lv_searchlist=(ListView)findViewById(R.id.lv_searchlist);
tv_searchText=(TextView)findViewById(R.id.tv_searchText);
tv_searchSum=(TextView)findViewById(R.id.tv_searchSum);
btn_return=(Button)findViewById(R.id.btn_return);
btn_return.setOnClickListener(this);
dbOperate.test(this);
Bundle bundle=getIntent().getExtras();
String searchKey=bundle.getString("searchKey");
List<student> searchList=dbOperate.searchByAll(searchKey);
tv_searchText.setText(searchKey);
tv_searchSum.setText(String.valueOf(searchList.size()+"條資訊"));
if (!searchList.isEmpty()){
MyAdapter myAdapter=new MyAdapter(searchList,this);
lv_searchlist.setAdapter(myAdapter);
}
}
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(this,MainActivity.class);
startActivity(intent);
}
}
結果截圖
遇到的問題
問題1:現學自定義Adapter
由於在列表中要使用刪除按鈕,所以普通的ListView不能滿足需求,我需要自定義Adapter
問題2:關於TextView.setText裡引數是int時報錯
報錯資訊如下
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.databasetest, PID: 13838
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:378)
at android.widget.TextView.setText(TextView.java:4685)
at com.example.databasetest.MyAdapter.getView(MyAdapter.java:71)
修改後 tv_stuID.setText(String.valueOf(oneStu.getId()));
問題3:SQLiteDatabase.execSQL(java.lang.String, java.lang.Object[])' on a null object reference
報錯資訊如下:
Process: com.example.databasetest, PID: 20630
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String, java.lang.Object[])' on a null object reference
at com.example.databasetest.DBOperate.delete(DBOperate.java:29)
at com.example.databasetest.MyAdapter$1.onClick(MyAdapter.java:80)
at android.view.View.performClick(View.java:5774)
at android.view.View$PerformClick.run(View.java:22990)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6564)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1134)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
具體在DBOperate 裡
起初我以為是new Object[] {I}這裡有錯,經過多次debug發現這個值是拿到了的,而且也轉成Integer了,在網上很多人說這個空物件的錯可能是findViewById()的原因,我又排查了一遍發現沒有漏掉的。最後,大半天之後,又一次debug時突然看到database是null的。 由於前面除錯insert時是成功了的,所以我就沒去注意database的情況。
以下是我寫在mainactivity裡的新增學生
實際上,在mainActivity裡的oncreate()裡已經將資料庫初始化了dbOperate.test(this);,所以在使用insert沒有報錯。但是在Adapter裡是通過DBOperate物件來呼叫裡面的刪除方法,沒有初始化資料庫
所以在刪除時報的空物件其實指的這裡的database
找到問題所在就好辦了,第一種解決,在DBOperate裡每個方法裡都初始化一下資料庫,還有就是在需要用到DBOperate時,new一個物件後就初始化一下。再不然就在MainActivity裡的DBOperate設定為全域性變數。果然,我在Adapter的建構函式裡初始化一下資料庫就沒問題了。
問題4:
報錯
Process: com.example.databasetest, PID: 30263
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databasetest/com.example.databasetest.UpdateDialog}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2996)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1700)
修改成
問題5:自定義Adapter中實現startActivityForResult的分析
因為在list裡要實現修改的話,就要點選list裡按鈕跳轉去修改,再傳回值。本來以為不能用startActivityForResult,後來看論壇裡有人說可以強制轉換context為activity。參考博文https://blog.csdn.net/zhencheng20082009/article/details/62418777
暫時沒有解決,我最後是直接修改後跳主頁面,個人覺得使用者體驗感不好,這裡應該只重新整理list介面
問題六:關於sqlist3的模糊查詢問題
最初是這樣的:接收到輸入框裡的值了,但是查不到
資料庫操作是這樣的
改成key="%"+key+"%";就可
問題6:listView有很多項時的滾動問題
關於這個滾動,我在網上翻了好一會都沒解決,以前用list View的時候好像沒怎麼遇到這個問題。後來讓我翻著翻著就發現一個小細節。之前,我的listView的寬和高都是wrap_content,這導致了後面如果我的資料多的話,listview的高就越來越高,就會把listview下面的佈局擠到更下面去看不見。所以我們要給定listView高度,這樣當有多項時,就自然出來可以上下滑動了。
待解決的問題:如何建立adapter的activity去複寫onActivityResult()那個方法?????????