Android中ListView使用示例介紹
簡(jiǎn)單ListView實(shí)例
數(shù)據(jù)庫(kù)讀取數(shù)據(jù)存入ListView
一、具體思路
1、創(chuàng)建Listview控件
2、創(chuàng)建子布局
創(chuàng)建數(shù)據(jù)庫(kù)
主方法調(diào)用數(shù)據(jù)庫(kù)繼承類且初始化數(shù)據(jù)庫(kù),寫入數(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)建對(duì)象,構(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ù)庫(kù)
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、對(duì)象
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="卡號(hào)"
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);
}
}
到此這篇關(guān)于Android中ListView使用示例介紹的文章就介紹到這了,更多相關(guān)Android ListView使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)雙向滑動(dòng)選擇器范圍SeekBar實(shí)現(xiàn)
這篇文章主要為大家介紹了Android開發(fā)雙向滑動(dòng)范圍選擇器SeekBar實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼
在項(xiàng)目中需要用到將Uri轉(zhuǎn)換為絕對(duì)路徑,下面小編把Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼分享到腳本之家平臺(tái),需要的的朋友參考下吧2017-07-07
android studio xml文件實(shí)現(xiàn)添加注釋
這篇文章主要介紹了android studio xml文件實(shí)現(xiàn)添加注釋,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android GestureDetector實(shí)現(xiàn)手勢(shì)滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android GestureDetector實(shí)現(xiàn)手勢(shì)滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
淺談Android安全風(fēng)險(xiǎn)與防范措施
這篇文章主要介紹了淺談Android安全風(fēng)險(xiǎn)與防范措施,對(duì)安全感興趣的同學(xué)可以參考下2021-04-04
Android回調(diào)與觀察者模式的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Android回調(diào)與觀察者模式的實(shí)現(xiàn)原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android自定義view實(shí)現(xiàn)圓形進(jìn)度條效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)圓形進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
基于Flutter制作一個(gè)長(zhǎng)按展示操作項(xiàng)面板的桌面圖標(biāo)
Flutter是一種強(qiáng)大的跨平臺(tái)移動(dòng)應(yīng)用程序框架,它能夠幫助開發(fā)者輕松地創(chuàng)建漂亮、快速、高效的應(yīng)用程序,本文的主題是如何在Flutter中制作一個(gè)長(zhǎng)按展示操作項(xiàng)面板的桌面圖標(biāo),在某些場(chǎng)景下,這個(gè)功能會(huì)讓應(yīng)用程序更加便利和易用2023-06-06
Android之ArcSlidingHelper制作圓弧滑動(dòng)效果
這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動(dòng)效果,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08

