MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

MyOpenHelper myOpenHelper = null;
ListView lv = null;
MyAdapter adapter = null;

private ArrayList<Person&gt; persons = new ArrayList<Person&gt;();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = findViewById(R.id.lv_listview);
    myOpenHelper = new MyOpenHelper(this);
    SQLiteDatabase mdb = myOpenHelper.getReadableDatabase();
    adapter = new MyAdapter();

};

private class MyAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return persons.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View itemview = null;
        if (view == null) {
            itemview = View.inflate(MainActivity.this, R.layout.item, null);
        } else {
            itemview = view;
        }
        TextView tv_name = itemview.findViewById(R.id.tv_name);
        TextView tv_phone = itemview.findViewById(R.id.tv_phone);
        Person person = persons.get(i);
        tv_name.setText(person.name);
        tv_phone.setText(person.phone);
        return itemview;
    }
};

public void db_insert(View view) {
    SQLiteDatabase mdb = myOpenHelper.getReadableDatabase();
    mdb.execSQL("insert into info (name,phone) values('王五','13777777')");
    mdb.execSQL("insert into info (name,phone) values('王2','13888888')");
    mdb.execSQL("insert into info (name,phone) values('王4','188999555')");
    mdb.close();
    System.out.println("insert data to database");
}

public void db_query(View view) {
    SQLiteDatabase mdb = myOpenHelper.getReadableDatabase();
    Cursor cursor = mdb.rawQuery("select * from info", null);
    while (cursor.moveToNext()) {
        //String name = cursor.getString(1);

// System.out.println(cursor.getString(1));
Person person = new Person();
person.name = cursor.getString(1);
person.phone = cursor.getString(2);
persons.add(person);
}
cursor.close();
mdb.close();
for(Person person:persons) {
System.out.println(person.toString());
}
lv.setAdapter((ListAdapter) adapter);
}
}

MyOpenHelper.java

package com.example.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, “test.db”, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table info (_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
    System.out.println("create database");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

}

Person.java

package com.example.myapplication;

public class Person {
public String name;
public String phone;
public String toString() {
return “Person [name=” + name + “, phone=” + phone + “]”;
}
}

activity_main.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<Button
    android:id="@+id/bt_insert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="db_insert"
    android:text="插入数据"/>
<Button
    android:id="@+id/bt_query"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="db_query"
    android:text="查询数据"
    android:layout_toRightOf="@+id/bt_insert"/>
<ListView
    android:id="@+id/lv_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World!"
    android:layout_below="@+id/bt_query" />

item.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<ImageView
    android:id="@+id/iv_head"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>
<TextView
    android:id="@+id/tv_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/iv_head"
    android:layout_marginTop="5dp"
    android:textSize="25dp"
    android:text="HELLO WORLD"/>
<TextView
    android:id="@+id/tv_phone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/iv_head"
    android:layout_below="@+id/tv_name"
    android:layout_marginTop="5dp"
    android:textSize="20dp"
    android:text="hello world"/>

结果

在这里插入图片描述

原文地址:https://blog.csdn.net/katerdaisy/article/details/130382629

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_18931.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注