DatabaseListArrayAdapter.java
Go to the documentation of this file.
00001 package com.introlab.rtabmap;
00002 
00003 import android.content.Context;
00004 import android.database.Cursor;
00005 import android.database.sqlite.SQLiteDatabase;
00006 import android.graphics.Bitmap;
00007 import android.graphics.BitmapFactory;
00008 import android.graphics.drawable.Drawable;
00009 import android.util.Log;
00010 import android.view.LayoutInflater;
00011 import android.view.View;
00012 import android.view.ViewGroup;
00013 import android.widget.BaseAdapter;
00014 import android.widget.ImageView;
00015 import android.widget.LinearLayout;
00016 import android.widget.SimpleAdapter;
00017 
00018 import java.io.ByteArrayInputStream;
00019 import java.io.InputStream;
00020 import java.util.ArrayList;
00021 import java.util.HashMap;
00022 
00023 public class DatabaseListArrayAdapter extends SimpleAdapter {
00024         LayoutInflater inflater;
00025         Context context;
00026         ArrayList<HashMap<String, String>> arrayList;
00027         int imageWidth;
00028 
00029         public DatabaseListArrayAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {
00030                 super(context, data, resource, from, to);
00031                 this.context = context;
00032                 this.arrayList = data;
00033                 this.imageWidth = (int)context.getResources().getDimension(R.dimen.image_width);
00034                 inflater.from(context);
00035         }
00036 
00037         @Override
00038         public View getView(final int position, View convertView, ViewGroup parent) {
00039                 View view = super.getView(position, convertView, parent);
00040                 ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
00041                 
00042                 boolean imageSet = false;
00043                 String path = this.arrayList.get(position).get("path");
00044                 if(!path.isEmpty())
00045                 {
00046                         SQLiteDatabase db = null;
00047                         try {
00048                                 db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
00049                                 
00050                                 // get version
00051                                 Cursor c1 = db.rawQuery("SELECT version FROM Admin", null);    
00052                         if(c1.moveToFirst()) {
00053                                 String version = c1.getString(c1.getColumnIndex("version"));
00054                                 Log.i(RTABMapActivity.TAG, "Version="+version);
00055                                 if(Util.versionCompare(version, "0.12.0") >= 0) {
00056                                         Cursor c2 = db.rawQuery("SELECT preview_image FROM Admin WHERE preview_image is not null", null); 
00057                                         if(c2.moveToFirst()) {
00058                                                 Log.i(RTABMapActivity.TAG, "Found image preview for db " + path);
00059 
00060                                                 byte[] bytes = c2.getBlob(c2.getColumnIndex("preview_image"));
00061                                                 ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
00062                                                 Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
00063                                                 imageView.setImageBitmap(bitmap);
00064                                                 imageSet = true;
00065                                         }
00066                                         else {
00067                                                 Log.i(RTABMapActivity.TAG, "Not found image preview for db " + path);
00068                                         }
00069                                 }
00070                                 else {
00071                                         Log.i(RTABMapActivity.TAG, "Too old database for preview image, path = " + path);
00072                                 }
00073                         }
00074                         else {
00075                                 Log.e(RTABMapActivity.TAG, "Failed getting version from database");
00076                         }
00077         
00078                 } catch (Exception e) {
00079                     Log.e(RTABMapActivity.TAG, e.getMessage());
00080                 }
00081                         finally {
00082                                 if(db != null && db.isOpen()) {
00083                         db.close();
00084                     }
00085                         }
00086                 }
00087                 else
00088                 {
00089                         Log.e(RTABMapActivity.TAG, "Database path empty for item " + position);
00090                 }
00091                 
00092                 if(!imageSet)
00093                 {
00094                         Drawable myDrawable = context.getResources().getDrawable(R.drawable.ic_launcher);
00095                         imageView.setImageDrawable(myDrawable);
00096                 }
00097                 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(imageWidth,imageWidth/(!imageSet?2:1));
00098                 imageView.setLayoutParams(layoutParams);
00099                 return view;
00100         }
00101 
00102 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:19