[英]Android ListView generated from SQLite database not displaying on phone screen (but displaying on emulator)
我是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);
}
}
抱歉,如果我不清楚,請先感謝您的幫助!
您沒有在設備上得到任何列表,因為您的數據庫為空。 嘗試通過代碼向表中添加一些條目。 它應該工作。
您應該閱讀以下答案以復制數據庫:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.