Android Studio 移动开发 新建数据库并从数据库中获取到图片文字数据显示在Listview列表上

Android 移动开发 新建数据库并从数据库中获取到图片,文字数据显示在Listview列表上

思路:
1.新建数据库(方法有很多,我这里借用SQLiteExpert可视化数据库建的)并添加相应字段字段得到.db文件
2.查询数据库内我们想要的数据(通过自己写的类NewDao中的静态方法查询)
3.显示在控件view上(通过适配器将listview中的内容显示出来,重写适配器中getView方法用”打气筒”inflate去填充listview.)
1.1
如图建如下数据库List和表actor
在这里插入如图建如下数据库List和表actor图片描述

img (这里要注意一下我就弄了好久)类型用blob类型,然后点load导入图片点再点ok图片就放入表中了.
在这里插入图片描述

运行模拟器后打开Database管理视图找到data/data/项目名/database 通过update添加进来或更换如图
在这里插入图片描述

1.2也可以通过新建Mydatahelp去继承SQLiteOpenHelper 新建数据库和表

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
   //新建表
    public void onCreate(SQLiteDatabase db) {
    
    
        //id,tile, text , img与数据库中对应
        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在查询数据库中先新建一个与数据库的字段对照的类(我建的是Actor)好去存放从数据库中获得的数据
package com.example.android_autolist;
/*
创建与数据库相对于的时段类型

  • */
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查询数据库,在写的类NewDao中的静态getList方法中

 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;
//查询获取数据库表actor中内容
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);
                 //添加到列表newActors中
                newActors.add(actor);
            }
            while (cursor.moveToNext());
        }
        return  newActors;
    }

}

此外:
Layout中要有连个视图xml文件 activity_main.xml和layout.xml文件.activity_main.xml中要有Listview控件
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/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        />

layout.xml中代码

<?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.现在准备工作就完成了剩下的就是在MainActivity中写主要逻辑代码去调用上面的类和控件了

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.新建数据库  2.查询出数据库信息 3.显示在view上
        Mydatahelp openHelper=new Mydatahelp(this,"List.db",null,1);
        SQLiteDatabase database=openHelper.getWritableDatabase();
        //2.通过NewDao中的静态方法查询
        actors=NewDao.getList(database);
         //3.显示
        listView=findViewById(R.id.listView1);
        //通过适配器将listview中的内容显示出来
        listView.setAdapter(new MyAdapter());
}
//新建MyAdapter 方法
    public class  MyAdapter extends BaseAdapter{
    
    
        @Override
        public int getCount() {
    
    
            //   动态的获取列表的个数,决定显示的个数(我在数据库中写了两条信息就会显示俩个列表)
            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)
           {
    
        //打气筒 取得xml 里定义的view
               LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
               view =inflater.inflate(R.layout.layout,null);
           }
           else  view=convertView;
           //找到控件
            TextView title=view.findViewById(R.id.title);
            TextView text=view.findViewById(R.id.text);
            ImageView imageView=view.findViewById(R.id.img);
            //重要
            //获取当前项的数据
            Actor actor=actors.get(position);
            //设置文本
            title.setText(actor.getTitle());
            text.setText(actor.getText());
            //将字节数组转换成bitmap型为了在Listview中显示
            Bitmap bitmap=stringToBitmap(actor.getImg());
            imageView.setImageBitmap(bitmap);
            return view;
        }
    }
    //字节流数值 转换成bitmap为了调用imageView.setImageBitmap()来显示图片
    public Bitmap stringToBitmap(byte[] bytes) {
    
    
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
}

最终效果:
在这里插入图片描述

希望看到这篇文章对你有帮助,代码可能有不足之处,愿与君共议.
整体的项目压缩包见下面的连接:
https://download.csdn.net/download/weixin_46097122/16138055

猜你喜欢

转载自blog.csdn.net/weixin_46097122/article/details/115100912