網(wǎng)站首頁 編程語言 正文
簡單ListView實(shí)例
數(shù)據(jù)庫讀取數(shù)據(jù)存入ListView
一、具體思路
1、創(chuàng)建Listview控件
2、創(chuàng)建子布局
創(chuàng)建數(shù)據(jù)庫
主方法調(diào)用數(shù)據(jù)庫繼承類且初始化數(shù)據(jù)庫,寫入數(shù)據(jù)
? MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this,"Co.db",null,1); ?
SQLiteDatabase db = databaseHelper.getWritableDatabase(); ?
SQLiteDatabase db2 = databaseHelper.getReadableDatabase();
3、寫入
ContentValues values = new ContentValues(); values.put("Code","1"); values.put("Name","Admin"); values.put("Post","32"); values.put("Tel","123456789"); db.insert("employee",null,values); values.clear(); values.put("Code","2"); values.put("Name","Admin1"); values.put("Post","22"); values.put("Tel","23342e"); db.insert("employee",null,values);
4、讀取
Cursor cursor = db2.query("Employee",null,null,null,null,null,null); arrayList = new ArrayList<>(); if (cursor.moveToFirst()) { do { name = cursor.getString(cursor.getColumnIndex("Name")); code = cursor.getString(cursor.getColumnIndex("Code")); post = cursor.getString(cursor.getColumnIndex("Post")); tel = cursor.getString(cursor.getColumnIndex("Tel")); System.out.println("查找到的值:"+ name +"---"+ code +"---"+ post +"---"+ tel); Employee employee=new Employee(name, tel, post, code); arrayList.add(employee); }while (cursor.moveToNext()); }
5、創(chuàng)建對象,構(gòu)造器,GETSET方法
6、創(chuàng)建Adapter
二、具體實(shí)施
1、適配器
lv.setAdapter(new BaseAdapter() { @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView==null){ view=View.inflate(getBaseContext(),R.layout.listitem,null); }else{ view=convertView; } Employee ee=(Employee) arrayList.get(position); TextView eename=view.findViewById(R.id.name); TextView eedianhua=view.findViewById(R.id.dianhua); TextView eezhiwei=view.findViewById(R.id.zhiwei); TextView eekahao=view.findViewById(R.id.kahao); eename.setText(ee.getName()); eedianhua.setText(ee.getTel()); eezhiwei.setText(ee.getPost()); eekahao.setText(ee.getCode()); return view; } });
2、數(shù)據(jù)庫
public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_Employees = "create table employee (" + "Code text, " + "Name text unique, " + "Post text, " + "Tel text)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); mContext = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_Employees); Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
3、對象
package com.example.a4_7_1_lv; public class Employee { private String name; private String tel; private String post; private String code; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getPost() { return post; } public void setPost(String post) { this.post = post; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Employee(String name, String tel, String post, String code) { this.name = name; this.tel = tel; this.post = post; this.code = code; } public Employee() { } }
4、等等等等
三、案例分享
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".MainActivity"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
listitem.xml
<?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="match_parent" android:orientation="vertical"> <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="姓名" android:textSize="30dp"/> <TextView android:layout_marginLeft="30dp" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="30dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電話" android:layout_marginLeft="100dp" android:textSize="30dp"/> <TextView android:layout_marginLeft="30dp" android:id="@+id/dianhua" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="30dp"/> </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="職位" android:textSize="30dp"/> <TextView android:layout_marginLeft="30dp" android:id="@+id/zhiwei" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="30dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="卡號" android:layout_marginLeft="100dp" android:textSize="30dp"/> <TextView android:layout_marginLeft="30dp" android:id="@+id/kahao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="30dp"/> </LinearLayout> </LinearLayout>
MyDatabaseHelper.java
package com.example.a4_7_1_lv; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_Employees = "create table employee (" + "Code text, " + "Name text unique, " + "Post text, " + "Tel text)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); mContext = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_Employees); Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
MainActivity.java
package com.example.a4_7_1_lv; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ListView lv; private MyDatabaseHelper databaseHelper; private SQLiteDatabase db; private SQLiteDatabase db2; private ArrayList arrayList; private String name; private String code; private String post; private String tel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = findViewById(R.id.list); } @Override protected void onStart() { super.onStart(); MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this,"Co.db",null,1); db = databaseHelper.getWritableDatabase(); db2 = databaseHelper.getReadableDatabase(); DBInsert(); Cursor cursor = db2.query("Employee",null,null,null,null,null,null); arrayList = new ArrayList<>(); if (cursor.moveToFirst()) { do { name = cursor.getString(cursor.getColumnIndex("Name")); code = cursor.getString(cursor.getColumnIndex("Code")); post = cursor.getString(cursor.getColumnIndex("Post")); tel = cursor.getString(cursor.getColumnIndex("Tel")); System.out.println("查找到的值:"+ name +"---"+ code +"---"+ post +"---"+ tel); Employee employee=new Employee(name, tel, post, code); arrayList.add(employee); }while (cursor.moveToNext()); } lv.setAdapter(new BaseAdapter() { @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView==null){ view=View.inflate(getBaseContext(),R.layout.listitem,null); }else{ view=convertView; } Employee ee=(Employee) arrayList.get(position); TextView eename=view.findViewById(R.id.name); TextView eedianhua=view.findViewById(R.id.dianhua); TextView eezhiwei=view.findViewById(R.id.zhiwei); TextView eekahao=view.findViewById(R.id.kahao); eename.setText(ee.getName()); eedianhua.setText(ee.getTel()); eezhiwei.setText(ee.getPost()); eekahao.setText(ee.getCode()); return view; } }); } private void DBInsert() { ContentValues values = new ContentValues(); values.put("Code","1"); values.put("Name","Admin"); values.put("Post","32"); values.put("Tel","123456789"); db.insert("employee",null,values); values.clear(); values.put("Code","2"); values.put("Name","Admin1"); values.put("Post","22"); values.put("Tel","23342e"); db.insert("employee",null,values); values.clear(); values.put("Code","4"); values.put("Name","Admin13"); values.put("Post","2sda2"); values.put("Tel","233asd42e"); db.insert("employee",null,values); values.clear(); values.put("Code","Code"); values.put("Name","Name"); values.put("Post","Post"); values.put("Tel","Tel"); db.insert("employee",null,values); } }
Employee.java
package com.example.a4_7_1_lv; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ListView lv; private MyDatabaseHelper databaseHelper; private SQLiteDatabase db; private SQLiteDatabase db2; private ArrayList arrayList; private String name; private String code; private String post; private String tel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = findViewById(R.id.list); } @Override protected void onStart() { super.onStart(); MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this,"Co.db",null,1); db = databaseHelper.getWritableDatabase(); db2 = databaseHelper.getReadableDatabase(); DBInsert(); Cursor cursor = db2.query("Employee",null,null,null,null,null,null); arrayList = new ArrayList<>(); if (cursor.moveToFirst()) { do { name = cursor.getString(cursor.getColumnIndex("Name")); code = cursor.getString(cursor.getColumnIndex("Code")); post = cursor.getString(cursor.getColumnIndex("Post")); tel = cursor.getString(cursor.getColumnIndex("Tel")); System.out.println("查找到的值:"+ name +"---"+ code +"---"+ post +"---"+ tel); Employee employee=new Employee(name, tel, post, code); arrayList.add(employee); }while (cursor.moveToNext()); } lv.setAdapter(new BaseAdapter() { @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView==null){ view=View.inflate(getBaseContext(),R.layout.listitem,null); }else{ view=convertView; } Employee ee=(Employee) arrayList.get(position); TextView eename=view.findViewById(R.id.name); TextView eedianhua=view.findViewById(R.id.dianhua); TextView eezhiwei=view.findViewById(R.id.zhiwei); TextView eekahao=view.findViewById(R.id.kahao); eename.setText(ee.getName()); eedianhua.setText(ee.getTel()); eezhiwei.setText(ee.getPost()); eekahao.setText(ee.getCode()); return view; } }); } private void DBInsert() { ContentValues values = new ContentValues(); values.put("Code","1"); values.put("Name","Admin"); values.put("Post","32"); values.put("Tel","123456789"); db.insert("employee",null,values); values.clear(); values.put("Code","2"); values.put("Name","Admin1"); values.put("Post","22"); values.put("Tel","23342e"); db.insert("employee",null,values); values.clear(); values.put("Code","4"); values.put("Name","Admin13"); values.put("Post","2sda2"); values.put("Tel","233asd42e"); db.insert("employee",null,values); values.clear(); values.put("Code","Code"); values.put("Name","Name"); values.put("Post","Post"); values.put("Tel","Tel"); db.insert("employee",null,values); } }
原文鏈接:https://blog.csdn.net/w_Eternal/article/details/122265523
相關(guān)推薦
- 2022-07-13 Stream的源碼簡單解析
- 2022-12-13 torch.optim優(yōu)化算法理解之optim.Adam()解讀_python
- 2022-06-21 C#多線程異步執(zhí)行和跨線程訪問控件Helper_C#教程
- 2022-04-07 一篇文章帶你學(xué)習(xí)Python3的高階函數(shù)_python
- 2022-11-26 Python?Django教程之模板的使用_python
- 2022-04-11 解決git push 錯誤error: src refspec master does not ma
- 2022-02-01 es ik分詞插件安裝
- 2023-05-20 Kotlin作用域函數(shù)使用示例詳細(xì)介紹_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支