iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android中ListView使用示例介绍
  • 477
分享到

Android中ListView使用示例介绍

2024-04-02 19:04:59 477人浏览 独家记忆
摘要

目录一、具体思路1、创建Listview控件3、写入4、读取5、创建对象,构造器,GETSET方法二、具体实施1、适配器2、数据库3、对象4、等等等等三、案例分享activity_m

简单ListView实例

数据库读取数据存入ListView

一、具体思路

1、创建Listview控件

2、创建子布局

创建数据库

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

​ 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、创建对象,构造器,GETSET方法

6、创建Adapter

二、具体实施

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、数据库

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);
    }
}

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

--结束END--

本文标题: Android中ListView使用示例介绍

本文链接: https://www.lsjlt.com/news/161438.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作