从SQLite数据库生成的Android ListView无法在手机屏幕上显示(但在模拟器上显示)

时间:2017-05-06 17:42:24

标签: android database sqlite listview

我是Android新手,我正在设计一款应用程序,我需要在某个时刻显示一个鸟类列表。为此,我将数据存储在SQLite数据库中。

所以,在某些时候,我点击一个ListView项目,它开始一个新的活动,一个应该显示我需要的鸟类列表。事情就是当我从Android Studio中的模拟器运行应用程序时,鸟ListView显示得很好但是当我尝试从我的手机运行它时,ListView没有显示,只显示活动的标题,其余的屏幕保持空白。我应该提一下,在从模拟器运行应用程序之前,我使用ADB和Android设备监视器将数据库放入模拟器的文件系统中,只有我不知道我需要做什么才能将数据库传输到我的手机当我想从手机上运行应用程序时

MenuOiseaux.java,我想要显示鸟类列表的活动:

package com.example.ant.kits;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;

public class MenuOiseaux extends AppCompatActivity
{

    ListView vue;
    private SQLiteDatabase db;
    DBOpenHelper dbOpenHelper;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
        dbOpenHelper = new DBOpenHelper(this, DBOpenHelper.Constants.DATABASE_NAME, null,
                DBOpenHelper.Constants.DATABASE_VERSION);
        db = dbOpenHelper.getWritableDatabase();

        Cursor cursor = db.rawQuery("SELECT " + DBOpenHelper.Constants.KEY_COL_ID
                + " , " + DBOpenHelper.Constants.KEY_COL_MINIATURE
                + " , " + DBOpenHelper.Constants.KEY_COL_NOM
                + " FROM " + DBOpenHelper.Constants.MY_TABLE ,null);

        ArrayList<HashMap<String, Object>> liste = new ArrayList<>();
        HashMap<String, Object> element;
        while (cursor.moveToNext())
        {
            byte[] byteArray = cursor.getBlob(1);
            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
            if(bmp == null){db.close();}
            element = new HashMap<>();
            element.put("miniature", bmp);
            element.put("nom",cursor.getString(2)); 
            liste.add(element);
        }
        cursor.close();
        vue = (ListView) findViewById(R.id.listView);
        SimpleAdapter adapter = new SimpleAdapter(this,
                liste,
                R.layout.list_item,
                new String[] {"miniature","nom"},
                new int[] {R.id.img,R.id.txt});
        adapter.setViewBinder(new MyViewBinder());
        vue.setAdapter(adapter);

        vue.setOnItemClickListener(new ListView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
            {
                Cursor cursor = db.rawQuery("SELECT " + DBOpenHelper.Constants.KEY_COL_ID
                        + " , " + DBOpenHelper.Constants.KEY_COL_NOM
                        + " , " + DBOpenHelper.Constants.KEY_COL_PHOTO
                        + " , " + DBOpenHelper.Constants.KEY_COL_DESCRIPTION
                        + " FROM " + DBOpenHelper.Constants.MY_TABLE
                        + " WHERE " + DBOpenHelper.Constants.KEY_COL_ID + " = " + String.valueOf(position+1),null);

                cursor.moveToFirst();
                Intent intent = new Intent(MenuOiseaux.this, FicheBiodiv.class);
                intent.putExtra("Nom", cursor.getString(1));
                byte[] byteArray = cursor.getBlob(2);
                Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0 ,byteArray.length);
                intent.putExtra("BitmapImage", bmp);
                intent.putExtra("Description", cursor.getString(3));
                startActivity(intent);
            }
        });
    }
}

DBOpenHelper.java,用于创建数据库(?):

package com.example.ant.kits;

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


public class DBOpenHelper extends SQLiteOpenHelper
{
    public static class Constants implements BaseColumns
    {
        public static final String DATABASE_NAME = "animaux.db";
        public static final int DATABASE_VERSION = 1;
        public static final String MY_TABLE = "Animaux";
        public static final String KEY_COL_ID = "_id";
        public static final String KEY_COL_MINIATURE = "miniature";
        public static final String KEY_COL_NOM = "nom";
        public static final String KEY_COL_DESCRIPTION = "description";
        public static final String KEY_COL_PHOTO = "photo";
        public static final int ID_COL = 1;
        public static final int NOM_COL = 2;
        public static final int DESCRIPTION_COL = 3;
        public static final int PHOTO_COL = 4;
    }

    private static final String DATABASE_CREATE = "CREATE TABLE " + Constants.MY_TABLE + " ( "
            + Constants.KEY_COL_ID + " integer primary key autoincrement, "
            + Constants.KEY_COL_MINIATURE + " BLOB , "
            + Constants.KEY_COL_NOM + " TEXT , "
            + Constants.KEY_COL_DESCRIPTION + " TEXT ,"
            + Constants.KEY_COL_PHOTO + " BLOB )";
    public static final String METIER_TABLE_DROP = " DROP TABLE IF EXISTS " + Constants.MY_TABLE + " ;";

    public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
    {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL(METIER_TABLE_DROP);
        onCreate(db);
    }
}

对不起,如果我不清楚,并提前感谢帮助我!

1 个答案:

答案 0 :(得分:0)

您没有在设备上获取任何列表,因为您的数据库是空的。尝试通过代码向表中添加一些条目。它应该工作。

您应该阅读此答案以复制您的数据库:

https://stackoverflow.com/a/29733700/5671534