Android中ListView怎么使用

Android中ListView怎么使用

这篇文章主要讲解了“Android中ListView怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android中ListView怎么使用”吧!

一、具体思路

1、创建Listview控件

2、创建子布局

创建数据库

Android中ListView怎么使用

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

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怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

发布于 2022-01-05 23:32:23
收藏
分享
海报
0 条评论
37
上一篇:如何使用CSS实现一个吃豆人的Loading加载效果 下一篇:C++名称空间特性是什么
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码