Can you tell me or give me some codes that I need to add in this codes to retrieve images from my database. Im a beginner in android programming any help would be much appreciated.
DBHelper Class:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;
class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.example.imagelist/databases/";
private static final String DATABASE_NAME = "DbImage.db";
private static final int SCHEMA_VERSION = 1;
private static final String TABLE_NAME = "image";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_IMAGE = "img";
private static final String COLUMN_NAME = "name";
public SQLiteDatabase dbSqlite;
private final Context myContext;
public DBHelper(Context context){
super(context, DATABASE_NAME, null, SCHEMA_VERSION );
this.myContext= context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
createDB();
}
public void createDB(){
boolean dbExist = DBExists();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try{
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null){
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource(){
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try{
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte [] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
} catch (IOException e){
throw new Error("Problem copying database from resource file.");
}
}
public void openDatabase () throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close () {
if (dbSqlite !=null){
dbSqlite.close();
}
super.close();
}
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder ();
queryBuilder.setTables(TABLE_NAME);
String [] asColumnsToReturn = new String [] { COLUMN_ID, COLUMN_IMAGE, COLUMN_NAME};
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null, null, null, null, "_id ASC");
return mCursor;
}
public String getName (Cursor c){
return(c.getString(2));
}
}
Main Class:
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends Activity {
private DBHelper dblistHelper = null;
private Cursor ourCursor = null;
private recipeAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.listView);
dblistHelper = new DBHelper(this);
dblistHelper.createDatabase();
dblistHelper.openDatabase();
ourCursor=dblistHelper.getCursor();
startManagingCursor(ourCursor);
adapter = new recipeAdapter(ourCursor);
myListView.setAdapter(adapter);
}
catch (Exception e)
{
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
class recipeAdapter extends CursorAdapter {
recipeAdapter(Cursor c){
super(Main.this, c);
}
@Override
public void bindView (View row, Context ctxt, Cursor c)
{
Holder holder = (Holder)row.getTag();
holder.populateFrom(c, dblistHelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
Holder holder = new Holder(row);
row.setTag(holder);
return(row);
}
}
static class Holder {
private TextView name=null;
private ImageView image=null;
Holder(View row){
name=(TextView)row.findViewById(R.id.textView1);
image=(ImageView)row.findViewById(R.id.imageView1);
}
void populateFrom(Cursor c, DBHelper r){
name.setText(r.getName(c));
image.setImageBitmap(bm);
}
}
}
Create an XML file for the elements in your listview. Something along these lines:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/icon"
android:layout_toRightOf="@id/icon"
android:textColor="#FF0000" />
</RelativeLayout>
Create an adapter class of some sort that pulls the content from your database. Then set whatever fields you have in your list view to the appropriate data from your database, something like this:
public class CustomAdapter {
public CustomAdapter(Context context){
// connect to you database
}
public View getItemView(/*pass your db object*/, View v, ViewGroup parent){
if (v == null) {
v = View.inflate(getContext(), R.layout.row, null);
}
super.getItemView(object, v, parent);
TextView textView = (TextView) v.findViewById(R.id.text1);
textView.setText(/*dataObject.getdata*/);
// Do this as much as necessary
}
public void loadObject(){
//Get all of your data, set each row in list view by calling getView
}
Then in your activity call create an instance of you object, get an instance of your listview, set the listview's adapter and call loadobjects
private CustomAdapter customAdapter;
//...
customAdapter = new CustomAdapter(this);
ListView listView = (ListView) findViewById(R.id.main_list_view);
listView.setAdapter(customAdapter);
customAdapter.loadObjects();
This is how I have done it, also this is based on information found from using Parse database: https://parse.com/tutorials/parse-query-adapter
This may help you as it is SQL related: http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html
The basic idea is to create your own adapter for a listview where you set your data. Then set the listview adapter to that adapter.