0

I'm new to Android and I'm currently designing an app in which I need to display a list of birds at some point. In order to do this, I have data that is stored in a SQLite database.

So, at some point, I click on a ListView item which starts a new activity, the one that is supposed to display the list of birds I need. The thing is when I run the app from the emulator in Android Studio, the bird ListView is displayed just fine but when I try to run it from my phone, the ListView is not displayed, only the title of the activity is displayed, the rest of the screen stays blank. I should mention that before running the app from the emulator, I use ADB and Android Device Monitor to put the database in the emulator's file system, only I don't know what I have to do to transmit the database to my phone when I want to run the app from my phone.

MenuOiseaux.java, the activity in which I want to display the list of birds:

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, which is used to create the database (?):

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);
    }
}

Sorry if I'm not being clear and thanks in advance for helping me out!

Bunkor
  • 3
  • 1

1 Answers1

0

You aren't getting any list on device because your database is empty. Try adding a few entries to your table through code. It should work.

You should read this answer to copy your database:

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

Community
  • 1
  • 1