经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android中ListView使用示例介绍
来源:jb51  时间:2022/1/2 12:22:10  对本文有异议

简单ListView实例

数据库读取数据存入ListView

一、具体思路

1、创建Listview控件

2、创建子布局

创建数据库

主方法调用数据库继承类且初始化数据库,写入数据

? MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this,"Co.db",null,1); ?

SQLiteDatabase db = databaseHelper.getWritableDatabase(); ?

SQLiteDatabase db2 = databaseHelper.getReadableDatabase();

3、写入

  1. ContentValues values = new ContentValues();
  2. values.put("Code","1");
  3. values.put("Name","Admin");
  4. values.put("Post","32");
  5. values.put("Tel","123456789");
  6. db.insert("employee",null,values);
  7. values.clear();
  8. values.put("Code","2");
  9. values.put("Name","Admin1");
  10. values.put("Post","22");
  11. values.put("Tel","23342e");
  12. db.insert("employee",null,values);

4、读取

  1. Cursor cursor = db2.query("Employee",null,null,null,null,null,null);
  2. arrayList = new ArrayList<>();
  3. if (cursor.moveToFirst()) {
  4. do {
  5. name = cursor.getString(cursor.getColumnIndex("Name"));
  6. code = cursor.getString(cursor.getColumnIndex("Code"));
  7. post = cursor.getString(cursor.getColumnIndex("Post"));
  8. tel = cursor.getString(cursor.getColumnIndex("Tel"));
  9. System.out.println("查找到的值:"+ name +"---"+ code +"---"+ post +"---"+ tel);
  10. Employee employee=new Employee(name, tel, post, code);
  11. arrayList.add(employee);
  12.  
  13. }while (cursor.moveToNext());
  14. }

5、创建对象,构造器,GETSET方法

6、创建Adapter

二、具体实施

1、适配器

  1. lv.setAdapter(new BaseAdapter() {
  2. @Override
  3. public int getCount() {
  4. return arrayList.size();
  5. }
  6.  
  7. @Override
  8. public Object getItem(int position) {
  9. return null;
  10. }
  11.  
  12. @Override
  13. public long getItemId(int position) {
  14. return 0;
  15. }
  16.  
  17. @Override
  18. public View getView(int position, View convertView, ViewGroup parent) {
  19. View view;
  20. if (convertView==null){
  21. view=View.inflate(getBaseContext(),R.layout.listitem,null);
  22. }else{
  23. view=convertView;
  24. }
  25. Employee ee=(Employee) arrayList.get(position);
  26. TextView eename=view.findViewById(R.id.name);
  27. TextView eedianhua=view.findViewById(R.id.dianhua);
  28. TextView eezhiwei=view.findViewById(R.id.zhiwei);
  29. TextView eekahao=view.findViewById(R.id.kahao);
  30. eename.setText(ee.getName());
  31. eedianhua.setText(ee.getTel());
  32. eezhiwei.setText(ee.getPost());
  33. eekahao.setText(ee.getCode());
  34. return view;
  35. }
  36. });

2、数据库

  1. public class MyDatabaseHelper extends SQLiteOpenHelper {
  2. public static final String CREATE_Employees = "create table employee ("
  3. + "Code text, "
  4. + "Name text unique, "
  5. + "Post text, "
  6. + "Tel text)";
  7.  
  8. private Context mContext;
  9. public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory
  10. factory, int version) {
  11. super(context, name, factory, version);
  12. mContext = context;
  13. }
  14. @Override
  15. public void onCreate(SQLiteDatabase db) {
  16. db.execSQL(CREATE_Employees);
  17. Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
  18. }
  19. @Override
  20. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  21. }
  22. }

3、对象

  1. package com.example.a4_7_1_lv;
  2.  
  3. public class Employee {
  4. private String name;
  5. private String tel;
  6. private String post;
  7. private String code;
  8.  
  9. public String getName() {
  10. return name;
  11. }
  12.  
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16.  
  17. public String getTel() {
  18. return tel;
  19. }
  20.  
  21. public void setTel(String tel) {
  22. this.tel = tel;
  23. }
  24.  
  25. public String getPost() {
  26. return post;
  27. }
  28.  
  29. public void setPost(String post) {
  30. this.post = post;
  31. }
  32.  
  33. public String getCode() {
  34. return code;
  35. }
  36.  
  37. public void setCode(String code) {
  38. this.code = code;
  39. }
  40.  
  41. public Employee(String name, String tel, String post, String code) {
  42. this.name = name;
  43. this.tel = tel;
  44. this.post = post;
  45. this.code = code;
  46. }
  47.  
  48. public Employee() {
  49. }
  50. }

4、等等等等

三、案例分享

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8.  
  9. <ListView
  10. android:id="@+id/list"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"/>
  13.  
  14. </RelativeLayout>

listitem.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal">
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="姓名"
  15. android:textSize="30dp"/>
  16. <TextView
  17. android:layout_marginLeft="30dp"
  18. android:id="@+id/name"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text=""
  22. android:textSize="30dp"/>
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="电话"
  27. android:layout_marginLeft="100dp"
  28. android:textSize="30dp"/>
  29. <TextView
  30. android:layout_marginLeft="30dp"
  31. android:id="@+id/dianhua"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text=""
  35. android:textSize="30dp"/>
  36. </LinearLayout>
  37. <LinearLayout
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:orientation="horizontal">
  41. <TextView
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="职位"
  45. android:textSize="30dp"/>
  46. <TextView
  47. android:layout_marginLeft="30dp"
  48. android:id="@+id/zhiwei"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:text=""
  52. android:textSize="30dp"/>
  53. <TextView
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:text="卡号"
  57. android:layout_marginLeft="100dp"
  58. android:textSize="30dp"/>
  59. <TextView
  60. android:layout_marginLeft="30dp"
  61. android:id="@+id/kahao"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:text=""
  65. android:textSize="30dp"/>
  66. </LinearLayout>
  67. </LinearLayout>

MyDatabaseHelper.java

  1. package com.example.a4_7_1_lv;
  2.  
  3. import android.content.Context;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.widget.Toast;
  7.  
  8. public class MyDatabaseHelper extends SQLiteOpenHelper {
  9. public static final String CREATE_Employees = "create table employee ("
  10. + "Code text, "
  11. + "Name text unique, "
  12. + "Post text, "
  13. + "Tel text)";
  14.  
  15. private Context mContext;
  16. public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory
  17. factory, int version) {
  18. super(context, name, factory, version);
  19. mContext = context;
  20. }
  21. @Override
  22. public void onCreate(SQLiteDatabase db) {
  23. db.execSQL(CREATE_Employees);
  24. Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
  25. }
  26. @Override
  27. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  28. }
  29. }

MainActivity.java

  1. package com.example.a4_7_1_lv;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.ContentValues;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.BaseAdapter;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14.  
  15. import java.util.ArrayList;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19. private ListView lv;
  20. private MyDatabaseHelper databaseHelper;
  21. private SQLiteDatabase db;
  22. private SQLiteDatabase db2;
  23. private ArrayList arrayList;
  24. private String name;
  25. private String code;
  26. private String post;
  27. private String tel;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. lv = findViewById(R.id.list);
  34. }
  35.  
  36. @Override
  37. protected void onStart() {
  38. super.onStart();
  39. MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this,"Co.db",null,1);
  40. db = databaseHelper.getWritableDatabase();
  41. db2 = databaseHelper.getReadableDatabase();
  42. DBInsert();
  43. Cursor cursor = db2.query("Employee",null,null,null,null,null,null);
  44. arrayList = new ArrayList<>();
  45. if (cursor.moveToFirst()) {
  46. do {
  47. name = cursor.getString(cursor.getColumnIndex("Name"));
  48. code = cursor.getString(cursor.getColumnIndex("Code"));
  49. post = cursor.getString(cursor.getColumnIndex("Post"));
  50. tel = cursor.getString(cursor.getColumnIndex("Tel"));
  51. System.out.println("查找到的值:"+ name +"---"+ code +"---"+ post +"---"+ tel);
  52. Employee employee=new Employee(name, tel, post, code);
  53. arrayList.add(employee);
  54.  
  55. }while (cursor.moveToNext());
  56. }
  57. lv.setAdapter(new BaseAdapter() {
  58. @Override
  59. public int getCount() {
  60. return arrayList.size();
  61. }
  62.  
  63. @Override
  64. public Object getItem(int position) {
  65. return null;
  66. }
  67.  
  68. @Override
  69. public long getItemId(int position) {
  70. return 0;
  71. }
  72.  
  73. @Override
  74. public View getView(int position, View convertView, ViewGroup parent) {
  75. View view;
  76. if (convertView==null){
  77. view=View.inflate(getBaseContext(),R.layout.listitem,null);
  78. }else{
  79. view=convertView;
  80. }
  81. Employee ee=(Employee) arrayList.get(position);
  82. TextView eename=view.findViewById(R.id.name);
  83. TextView eedianhua=view.findViewById(R.id.dianhua);
  84. TextView eezhiwei=view.findViewById(R.id.zhiwei);
  85. TextView eekahao=view.findViewById(R.id.kahao);
  86. eename.setText(ee.getName());
  87. eedianhua.setText(ee.getTel());
  88. eezhiwei.setText(ee.getPost());
  89. eekahao.setText(ee.getCode());
  90. return view;
  91. }
  92. });
  93. }
  94.  
  95. private void DBInsert() {
  96. ContentValues values = new ContentValues();
  97. values.put("Code","1");
  98. values.put("Name","Admin");
  99. values.put("Post","32");
  100. values.put("Tel","123456789");
  101. db.insert("employee",null,values);
  102. values.clear();
  103. values.put("Code","2");
  104. values.put("Name","Admin1");
  105. values.put("Post","22");
  106. values.put("Tel","23342e");
  107. db.insert("employee",null,values);
  108. values.clear();
  109. values.put("Code","4");
  110. values.put("Name","Admin13");
  111. values.put("Post","2sda2");
  112. values.put("Tel","233asd42e");
  113. db.insert("employee",null,values);
  114. values.clear();
  115. values.put("Code","Code");
  116. values.put("Name","Name");
  117. values.put("Post","Post");
  118. values.put("Tel","Tel");
  119. db.insert("employee",null,values);
  120. }
  121. }

Employee.java

  1. package com.example.a4_7_1_lv;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.ContentValues;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.BaseAdapter;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14.  
  15. import java.util.ArrayList;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19. private ListView lv;
  20. private MyDatabaseHelper databaseHelper;
  21. private SQLiteDatabase db;
  22. private SQLiteDatabase db2;
  23. private ArrayList arrayList;
  24. private String name;
  25. private String code;
  26. private String post;
  27. private String tel;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. lv = findViewById(R.id.list);
  34. }
  35.  
  36. @Override
  37. protected void onStart() {
  38. super.onStart();
  39. MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this,"Co.db",null,1);
  40. db = databaseHelper.getWritableDatabase();
  41. db2 = databaseHelper.getReadableDatabase();
  42. DBInsert();
  43. Cursor cursor = db2.query("Employee",null,null,null,null,null,null);
  44. arrayList = new ArrayList<>();
  45. if (cursor.moveToFirst()) {
  46. do {
  47. name = cursor.getString(cursor.getColumnIndex("Name"));
  48. code = cursor.getString(cursor.getColumnIndex("Code"));
  49. post = cursor.getString(cursor.getColumnIndex("Post"));
  50. tel = cursor.getString(cursor.getColumnIndex("Tel"));
  51. System.out.println("查找到的值:"+ name +"---"+ code +"---"+ post +"---"+ tel);
  52. Employee employee=new Employee(name, tel, post, code);
  53. arrayList.add(employee);
  54.  
  55. }while (cursor.moveToNext());
  56. }
  57. lv.setAdapter(new BaseAdapter() {
  58. @Override
  59. public int getCount() {
  60. return arrayList.size();
  61. }
  62.  
  63. @Override
  64. public Object getItem(int position) {
  65. return null;
  66. }
  67.  
  68. @Override
  69. public long getItemId(int position) {
  70. return 0;
  71. }
  72.  
  73. @Override
  74. public View getView(int position, View convertView, ViewGroup parent) {
  75. View view;
  76. if (convertView==null){
  77. view=View.inflate(getBaseContext(),R.layout.listitem,null);
  78. }else{
  79. view=convertView;
  80. }
  81. Employee ee=(Employee) arrayList.get(position);
  82. TextView eename=view.findViewById(R.id.name);
  83. TextView eedianhua=view.findViewById(R.id.dianhua);
  84. TextView eezhiwei=view.findViewById(R.id.zhiwei);
  85. TextView eekahao=view.findViewById(R.id.kahao);
  86. eename.setText(ee.getName());
  87. eedianhua.setText(ee.getTel());
  88. eezhiwei.setText(ee.getPost());
  89. eekahao.setText(ee.getCode());
  90. return view;
  91. }
  92. });
  93. }
  94.  
  95. private void DBInsert() {
  96. ContentValues values = new ContentValues();
  97. values.put("Code","1");
  98. values.put("Name","Admin");
  99. values.put("Post","32");
  100. values.put("Tel","123456789");
  101. db.insert("employee",null,values);
  102. values.clear();
  103. values.put("Code","2");
  104. values.put("Name","Admin1");
  105. values.put("Post","22");
  106. values.put("Tel","23342e");
  107. db.insert("employee",null,values);
  108. values.clear();
  109. values.put("Code","4");
  110. values.put("Name","Admin13");
  111. values.put("Post","2sda2");
  112. values.put("Tel","233asd42e");
  113. db.insert("employee",null,values);
  114. values.clear();
  115. values.put("Code","Code");
  116. values.put("Name","Name");
  117. values.put("Post","Post");
  118. values.put("Tel","Tel");
  119. db.insert("employee",null,values);
  120. }
  121. }
  122.  

到此这篇关于Android中ListView使用示例介绍的文章就介绍到这了,更多相关Android ListView使用内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号