91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中ListView怎么使用

發布時間:2022-01-05 10:19:09 來源:億速云 閱讀:147 作者:iii 欄目:開發技術

這篇文章主要講解了“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、寫入

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怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

汉中市| 军事| 安乡县| 儋州市| 昭平县| 安吉县| 唐河县| 泰和县| 资中县| 灵川县| 治多县| 玉屏| 凭祥市| 普陀区| 大厂| 翼城县| 团风县| 桂林市| 盈江县| 图木舒克市| 濮阳县| 东至县| 苍南县| 三门县| 溧阳市| 大田县| 江源县| 海安县| 浙江省| 北辰区| 昭平县| 上林县| 乌苏市| 宾川县| 永丰县| 集安市| 五家渠市| 平利县| 南充市| 阳曲县| 和田市|