Android中ListView怎么使用
Android中ListView怎么使用
这篇文章主要讲解了“Android中ListView怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android中ListView怎么使用”吧!
一、具体思路
1、创建Listview控件
2、创建子布局
创建数据库
主方法调用数据库继承类且初始化数据库,写入数据
MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this,"Co.db",null,1);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
SQLiteDatabase db2 = databaseHelper.getReadableDatabase();
3、写入
ContentValuesvalues=newContentValues();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、读取
Cursorcursor=db2.query("Employee",null,null,null,null,null,null);arrayList=newArrayList<>();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);Employeeemployee=newEmployee(name,tel,post,code);arrayList.add(employee);}while(cursor.moveToNext());}
5、创建对象,构造器,GETSET方法
6、创建Adapter
二、具体实施
1、适配器
lv.setAdapter(newBaseAdapter(){@OverridepublicintgetCount(){returnarrayList.size();}@OverridepublicObjectgetItem(intposition){returnnull;}@OverridepubliclonggetItemId(intposition){return0;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){Viewview;if(convertView==null){view=View.inflate(getBaseContext(),R.layout.listitem,null);}else{view=convertView;}Employeeee=(Employee)arrayList.get(position);TextVieweename=view.findViewById(R.id.name);TextVieweedianhua=view.findViewById(R.id.dianhua);TextVieweezhiwei=view.findViewById(R.id.zhiwei);TextVieweekahao=view.findViewById(R.id.kahao);eename.setText(ee.getName());eedianhua.setText(ee.getTel());eezhiwei.setText(ee.getPost());eekahao.setText(ee.getCode());returnview;}});
2、数据库
publicclassMyDatabaseHelperextendsSQLiteOpenHelper{publicstaticfinalStringCREATE_Employees="createtableemployee("+"Codetext,"+"Nametextunique,"+"Posttext,"+"Teltext)";privateContextmContext;publicMyDatabaseHelper(Contextcontext,Stringname,SQLiteDatabase.CursorFactoryfactory,intversion){super(context,name,factory,version);mContext=context;}@OverridepublicvoidonCreate(SQLiteDatabasedb){db.execSQL(CREATE_Employees);Toast.makeText(mContext,"Createsucceeded",Toast.LENGTH_SHORT).show();}@OverridepublicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){}}
3、对象
packagecom.example.a4_7_1_lv;publicclassEmployee{privateStringname;privateStringtel;privateStringpost;privateStringcode;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetTel(){returntel;}publicvoidsetTel(Stringtel){this.tel=tel;}publicStringgetPost(){returnpost;}publicvoidsetPost(Stringpost){this.post=post;}publicStringgetCode(){returncode;}publicvoidsetCode(Stringcode){this.code=code;}publicEmployee(Stringname,Stringtel,Stringpost,Stringcode){this.name=name;this.tel=tel;this.post=post;this.code=code;}publicEmployee(){}}
4、等等等等
三、案例分享
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns: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"><ListViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>
listitem.xml
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名"android:textSize="30dp"/><TextViewandroid:layout_marginLeft="30dp"android:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:textSize="30dp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="电话"android:layout_marginLeft="100dp"android:textSize="30dp"/><TextViewandroid:layout_marginLeft="30dp"android:id="@+id/dianhua"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:textSize="30dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="职位"android:textSize="30dp"/><TextViewandroid:layout_marginLeft="30dp"android:id="@+id/zhiwei"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:textSize="30dp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="卡号"android:layout_marginLeft="100dp"android:textSize="30dp"/><TextViewandroid: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
packagecom.example.a4_7_1_lv;importandroid.content.Context;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteOpenHelper;importandroid.widget.Toast;publicclassMyDatabaseHelperextendsSQLiteOpenHelper{publicstaticfinalStringCREATE_Employees="createtableemployee("+"Codetext,"+"Nametextunique,"+"Posttext,"+"Teltext)";privateContextmContext;publicMyDatabaseHelper(Contextcontext,Stringname,SQLiteDatabase.CursorFactoryfactory,intversion){super(context,name,factory,version);mContext=context;}@OverridepublicvoidonCreate(SQLiteDatabasedb){db.execSQL(CREATE_Employees);Toast.makeText(mContext,"Createsucceeded",Toast.LENGTH_SHORT).show();}@OverridepublicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){}}
MainActivity.java
packagecom.example.a4_7_1_lv;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.ContentValues;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importandroid.os.Bundle;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ListView;importandroid.widget.TextView;importjava.util.ArrayList;publicclassMainActivityextendsAppCompatActivity{privateListViewlv;privateMyDatabaseHelperdatabaseHelper;privateSQLiteDatabasedb;privateSQLiteDatabasedb2;privateArrayListarrayList;privateStringname;privateStringcode;privateStringpost;privateStringtel;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv=findViewById(R.id.list);}@OverrideprotectedvoidonStart(){super.onStart();MyDatabaseHelperdatabaseHelper=newMyDatabaseHelper(this,"Co.db",null,1);db=databaseHelper.getWritableDatabase();db2=databaseHelper.getReadableDatabase();DBInsert();Cursorcursor=db2.query("Employee",null,null,null,null,null,null);arrayList=newArrayList<>();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);Employeeemployee=newEmployee(name,tel,post,code);arrayList.add(employee);}while(cursor.moveToNext());}lv.setAdapter(newBaseAdapter(){@OverridepublicintgetCount(){returnarrayList.size();}@OverridepublicObjectgetItem(intposition){returnnull;}@OverridepubliclonggetItemId(intposition){return0;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){Viewview;if(convertView==null){view=View.inflate(getBaseContext(),R.layout.listitem,null);}else{view=convertView;}Employeeee=(Employee)arrayList.get(position);TextVieweename=view.findViewById(R.id.name);TextVieweedianhua=view.findViewById(R.id.dianhua);TextVieweezhiwei=view.findViewById(R.id.zhiwei);TextVieweekahao=view.findViewById(R.id.kahao);eename.setText(ee.getName());eedianhua.setText(ee.getTel());eezhiwei.setText(ee.getPost());eekahao.setText(ee.getCode());returnview;}});}privatevoidDBInsert(){ContentValuesvalues=newContentValues();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
packagecom.example.a4_7_1_lv;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.ContentValues;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importandroid.os.Bundle;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ListView;importandroid.widget.TextView;importjava.util.ArrayList;publicclassMainActivityextendsAppCompatActivity{privateListViewlv;privateMyDatabaseHelperdatabaseHelper;privateSQLiteDatabasedb;privateSQLiteDatabasedb2;privateArrayListarrayList;privateStringname;privateStringcode;privateStringpost;privateStringtel;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv=findViewById(R.id.list);}@OverrideprotectedvoidonStart(){super.onStart();MyDatabaseHelperdatabaseHelper=newMyDatabaseHelper(this,"Co.db",null,1);db=databaseHelper.getWritableDatabase();db2=databaseHelper.getReadableDatabase();DBInsert();Cursorcursor=db2.query("Employee",null,null,null,null,null,null);arrayList=newArrayList<>();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);Employeeemployee=newEmployee(name,tel,post,code);arrayList.add(employee);}while(cursor.moveToNext());}lv.setAdapter(newBaseAdapter(){@OverridepublicintgetCount(){returnarrayList.size();}@OverridepublicObjectgetItem(intposition){returnnull;}@OverridepubliclonggetItemId(intposition){return0;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){Viewview;if(convertView==null){view=View.inflate(getBaseContext(),R.layout.listitem,null);}else{view=convertView;}Employeeee=(Employee)arrayList.get(position);TextVieweename=view.findViewById(R.id.name);TextVieweedianhua=view.findViewById(R.id.dianhua);TextVieweezhiwei=view.findViewById(R.id.zhiwei);TextVieweekahao=view.findViewById(R.id.kahao);eename.setText(ee.getName());eedianhua.setText(ee.getTel());eezhiwei.setText(ee.getPost());eekahao.setText(ee.getCode());returnview;}});}privatevoidDBInsert(){ContentValuesvalues=newContentValues();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);}}
感谢各位的阅读,以上就是“Android中ListView怎么使用”的内容了,经过本文的学习后,相信大家对Android中ListView怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
推荐阅读
-
android(如何快速开发框架 小米note开发版MIUI,安卓6.0,怎么安装Xposed框架)
稳定版,你必须先根除。你上网搜索安卓可以叫别人s框架,对方可以把框架做成jar包,把这个jar包加载到项目目录的libs文件中使...
-
android(studio 虚拟机启动不了 android studio可以当模拟器用吗)
androidstudio可以当模拟器用吗?AmdCUP引导模拟器有点复杂。雷电模拟器上的抖音怎么登录不上?不是,闪电模拟调用...
-
从实践中学习手机抓包与数据分析(android 手机抓包app)
android手机抓包app?netcapture抓包精灵app(手机抓包工具)又名sslcapture,是什么专业的安卓手机抓...
-
android(studio全局搜索 android studio怎么看app界面)
androidstudio怎么看app界面?在设备桌面点击运用直接进入到App界面,就也可以参与其他你的操作了。android-...
-
怎么把android框架源代码拉到本地(android studio如何运行别人的源代码)
androidstudio如何运行别人的源代码?androidstudio点击刚建在列表中你选择导入module,导入即可在用...
-
android(studio2022年使用教程 怎么安装Android studio详细教程)
怎么安装Androidstudio详细教程?androidstudio中haxm直接安装的方法追加:1、简单的方法打开Andr...
-
怎么使用Android基准配置文件Baseline Profile方案提升启动速度
-
HTML5如何实现禁止android视频另存为
-
学java好还是学php好?
-
Android如何实现多点触控功能