当前位置:网站首页>Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
2022-06-12 06:10:00 【Programming from 0 to 1】
Android Mobile development Create a new database and get pictures from the database , Text data is displayed in Listview On the list
Ideas :
1. New database ( There are many ways , I borrow it here SQLiteExpert Visual database is built ) And add the corresponding fields to get .db file
2. Query the data we want in the database ( Write your own class NewDao Static method query in )
3. Show in control view On ( Through the adapter listview The contents of the are displayed , Rewrite the adapter getView Method use ” tyre pump ”inflate To fill in listview.)
1.1
Build the following database as shown in the figure List And table actor
img ( I have to pay attention here. I have been doing it for a long time ) Type use blob type , Then click load Import the picture and click again ok The picture is put into the table .
Open after running the simulator Database Management view found data/data/ Project name /database adopt update Add in or replace as shown in the figure 
1.2 You can also create a new Mydatahelp To inherit SQLiteOpenHelper Create new databases and tables
package com.example.android_autolist;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class Mydatahelp extends SQLiteOpenHelper {
public Mydatahelp( Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
// new table
public void onCreate(SQLiteDatabase db) {
//id,tile, text , img Corresponding to the database
String sql =("create table actor(id integer primary key autoincrement,title varchar(20),text varchar(50),img blob )");
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.1 In the query database, create a new class to compare with the fields in the database ( What I built is Actor) To store the data obtained from the database
package com.example.android_autolist;
/*
Create a time period type relative to the database
- */
public class Actor {
private Integer id;
private String title;
private String text;
private byte[] img;
public byte[] getImg() {
return img; }
public void setImg(byte[] img) {
this.img = img; }
public Integer getId() {
return id; }
public void setId(Integer id) {
this.id = id; }
public String getTitle() {
return title; }
public void setTitle(String title) {
this.title = title; }
public String getText() {
return text; }
public void setText(String text) {
this.text = text; }
}
2.2 Query the database , The class being written NewDao Static in getList In the method
package com.example.android_autolist;
import android.app.blob.BlobHandle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
// Query to get database tables actor Chinese content
public class NewDao {
public static List<Actor> getList(SQLiteDatabase db){
List<Actor> newActors=new ArrayList<>();
Cursor cursor=db.query("actor",new String[]{
"title","text","img"},null,null,null,null,null);
if(cursor.moveToFirst()){
Actor actor=null;
do {
actor=new Actor();
String title=cursor.getString(0);
actor.setTitle(title);
String text=cursor.getString(cursor.getColumnIndex("text"));
actor.setText(text);
byte[] img = cursor.getBlob(2);
actor.setImg(img);
// Add to list newActors in
newActors.add(actor);
}
while (cursor.moveToNext());
}
return newActors;
}
}
Besides :
Layout There should be multiple views in the xml file activity_main.xml and layout.xml file .activity_main.xml Must have Listview Control
activity_main.xml In the code :
<?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/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" />
layout.xml In the code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:id="@+id/img" android:layout_width="145dp" android:layout_height="110dp" />
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/img" android:textSize="18dp" android:singleLine="true" android:textColor="#000000" />
<TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title" android:layout_toRightOf="@id/img" android:textSize="14dp" />
</RelativeLayout>
3. Now the preparatory work is finished, and the rest is in MainActivity Write the main logic code to call the above classes and controls
package com.example.android_autolist;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
List <Actor> actors=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1. New database 2. Query the database information 3. Displayed in the view On
Mydatahelp openHelper=new Mydatahelp(this,"List.db",null,1);
SQLiteDatabase database=openHelper.getWritableDatabase();
//2. adopt NewDao Static method query in
actors=NewDao.getList(database);
//3. Show
listView=findViewById(R.id.listView1);
// Through the adapter listview The contents of the are displayed
listView.setAdapter(new MyAdapter());
}
// newly build MyAdapter Method
public class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// Get the number of lists dynamically , Determine the number of displays ( When I write two pieces of information in the database, two lists will be displayed )
return actors.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)
{
// tyre pump obtain xml The definition of view
LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view =inflater.inflate(R.layout.layout,null);
}
else view=convertView;
// Find control
TextView title=view.findViewById(R.id.title);
TextView text=view.findViewById(R.id.text);
ImageView imageView=view.findViewById(R.id.img);
// important
// Get the data of the current item
Actor actor=actors.get(position);
// Set text
title.setText(actor.getTitle());
text.setText(actor.getText());
// Convert a byte array to bitmap Type in order to Listview It shows that
Bitmap bitmap=stringToBitmap(actor.getImg());
imageView.setImageBitmap(bitmap);
return view;
}
}
// Byte stream value convert to bitmap In order to call imageView.setImageBitmap() To show pictures
public Bitmap stringToBitmap(byte[] bytes) {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}
Final effect :
I hope this article will help you , The code may have shortcomings , I would like to discuss with you .
See the following link for the overall project package :
https://download.csdn.net/download/weixin_46097122/16138055
边栏推荐
- RNN model
- Leetcode-1552. Magnetic force between two balls
- [untitled]
- dlib 人脸检测
- Simulateur nightGod ADB View log
- Leetcode-553. Optimal division
- (UE4 4.27) add globalshder to the plug-in
- 单通道图片的读入
- [PowerShell] command line output and adding system environment variables
- Unity can realize the rotation, translation and scaling script of the camera around the target point on the mobile terminal device
猜你喜欢

Project and build Publishing

EBook list page

Front desk display LED number (number type on calculator)

IO to IO multiplexing from traditional network

Redis队列

MNIST handwritten data recognition by RNN

MySQL master-slave, 6 minutes to master

SQLite cross compile dynamic library

Using hidden Markov model to mark part of speech

Summary of some problems in sensor bring up
随机推荐
Database Experiment 3: data query
Leetcode-93. Restore IP address
Leetcode sword finger offer II 033 Modified phrase
Houdini terrain creation
Sensor bringup 中的一些问题总结
BRDF of directx11 advanced tutorial PBR (2)
Unity3d display FPS script
Leetcode-1260. 2D mesh migration
RNN model
Unity custom translucent surface material shader
2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation
Leetcode-646. Longest number pair chain
关于 Sensor flicker/banding现象的解释
Sqlite Cross - compile Dynamic Library
Leetcode-1706. Where does the club fall
MySQL notes
How do I get the date and time from the Internet- How to get DateTime from the internet?
. Net core - pass Net core will Net to cross platform
分段贝塞尔曲线
sqlite交叉編譯動態庫