I'm new to this, and watching tutorials and videos, I managed to make a sqlite database to save the data of my favorite series. What I keep is a field with the name of the series, another with the season, another with the chapter and an image of the series, I have buttons to add, delete and consult, another button to select the image from the gallery and a button that takes me to a listview, where I can track my series, the problem is that I do not know how to also show the image stored in the listview, then I put everything I have:
class to create the base:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {
public AdminSQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table datosseries(nombre text primary key, temporada text,capitulo text, imagen blob)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists datosseries");
db.execSQL("create table datosseries(nombre text primary key, temporada text,capitulo text, imagen blob)");
}
}
my MainActivity:
import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
EditText et1, et2, et3;
ImageView imageView;
final int REQUEST_CODE_GALLERY = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editnombre);
et2 = (EditText) findViewById(R.id.edittemporada);
et3 = (EditText) findViewById(R.id.editcapitulo);
imageView = (ImageView) findViewById(R.id.imagefoto);
}
public void altaseries(View v){
if (et1.getText().length()==0){
Toast.makeText(this, "INTRODUCE EL NOMBRE DE LA SERIE",
Toast.LENGTH_SHORT).show();
}
else {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,"series.db", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
String nombre = et1.getText().toString();
String temporada = et2.getText().toString();
String capitulo = et3.getText().toString();
byte [] imagen = imageViewToByte(imageView);
ContentValues registro = new ContentValues(); //es una clase para guardar datos
registro.put("nombre", nombre);
registro.put("temporada", temporada);
registro.put("capitulo", capitulo);
registro.put("imagen", imagen);
long cant = bd.insert("datosseries", null, registro);
if (cant > 0) {
Toast.makeText(this, "LA SERIE SE HA AGREGADO",
Toast.LENGTH_SHORT).show();}
else {
Toast.makeText(this, "LA SERIE YA EXISTE",
Toast.LENGTH_SHORT).show();
}
bd.close();
et1.setText("");
et2.setText("");
et3.setText("");
imageView.setImageResource(R.drawable.camara);
}
}
private byte[] imageViewToByte(ImageView image) {
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
public void consultaseries(View v){
if (et1.getText().length() == 0) {
Toast.makeText(this, "INTRODUCE EL NOMBRE DE LA SERIE",
Toast.LENGTH_SHORT).show();
} else {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"series.db", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
String nombre = et1.getText().toString();
String[] nombre1 = new String[]{nombre};
Cursor fila = bd.rawQuery(
"select temporada,capitulo,imagen from datosseries where nombre=?", nombre1);
if (fila.moveToFirst()) {
et2.setText(fila.getString(0));
et3.setText(fila.getString(1));
byte[] imgByte = fila.getBlob(2);
Bitmap bitmap= BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
imageView.setImageBitmap(bitmap);
} else
Toast.makeText(this, "NO EXISTE SERIE CON ESE NOMBRE",
Toast.LENGTH_SHORT).show();
bd.close();
}
}
public void borrarseries(View v){
if (et1.getText().length()==0){
Toast.makeText(this, "INTRODUCE EL NOMBRE DE LA SERIE",
Toast.LENGTH_SHORT).show();
}
else {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"series.db", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
String nombre = et1.getText().toString();
String[] nombre1 = new String[]{nombre};
int cant = bd.delete("datosseries", "nombre=?", nombre1);
bd.close();
et1.setText("");
et2.setText("");
et3.setText("");
imageView.setImageResource(R.drawable.camara);
if (cant == 1)
Toast.makeText(this, "SERIE BORRADA",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "NO EXISTE SERIE CON ESE NOMBRE",
Toast.LENGTH_SHORT).show();
}
}
public void boton_imagen(View v){
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_GALLERY
);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_CODE_GALLERY){
if (grantResults.length ==1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_GALLERY);
}
else {
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK && data != null){
Uri uri = data.getData();
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void cargarseries (View v){
Intent s = new Intent(this, listview.class);
startActivity(s);
}
}
to fill the Listview, I do not know what to put to show the image ????:
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class listview extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
cargarseries();
}
public void cargarseries() {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "series.db", null, 1);
SQLiteDatabase bd = admin.getReadableDatabase();
Cursor fila = bd.rawQuery("select * from datosseries order by nombre asc", null);// muestra registros por numero ascendente
int cantidad = fila.getCount();
int i = 0;
String[] listado = new String[cantidad];
if (fila.moveToFirst()) {
do {
String linea = "NOMBRE SERIE: " + fila.getString(0)+ "\nTEMPORADA: " + fila.getString(1)+ "\nCAPITULO: " + fila.getString(2);
listado[i] = linea;
// que poner para mostrar la imagen??????
i++;
} while (fila.moveToNext());
bd.close();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listado);
ListView lista = (ListView) findViewById(R.id.listviewseries);
lista.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // para activar boton atras action bar
Intent a = new Intent(this, MainActivity.class);
startActivity(a);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
the layout where the buttons are and other things:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.josehp.imageview_listview.MainActivity">
<EditText
android:id="@+id/editnombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/btconsultar"
android:layout_alignTop="@+id/btlistview"
android:layout_marginTop="39dp"
android:ems="10"
android:hint="@string/nombre"
android:imeOptions="actionDone"
android:inputType="text"
tools:ignore="LabelFor" />
<EditText
android:id="@+id/edittemporada"
android:imeOptions="actionDone"
tools:ignore="LabelFor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/editnombre"
android:layout_below="@+id/editnombre"
android:layout_marginEnd="6dp"
android:layout_marginTop="31dp"
android:ems="10"
android:inputType="number"
android:hint="@string/temporada" />
<EditText
android:id="@+id/editcapitulo"
android:imeOptions="actionDone"
tools:ignore="LabelFor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/editnombre"
android:layout_below="@+id/edittemporada"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="number"
android:hint="@string/capitulo" />
<ImageView
android:id="@+id/imagefoto"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:contentDescription="@string/imagenes"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_below="@+id/editcapitulo"
android:layout_marginTop="50dp"
app:srcCompat="@drawable/camara" />
<Button
android:id="@+id/btagregar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="69dp"
android:layout_marginStart="33dp"
android:onClick="altaseries"
android:text="@string/agregar" />
<Button
android:id="@+id/btconsultar"
android:layout_marginStart="135dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btagregar"
android:layout_alignBottom="@+id/btagregar"
android:onClick="consultaseries"
android:text="@string/consultar" />
<Button
android:id="@+id/btborrar"
android:layout_marginStart="255dp"
android:layout_alignBaseline="@+id/btagregar"
android:layout_alignBottom="@+id/btagregar"
android:onClick="borrarseries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="@string/borrar" />
<Button
android:id="@+id/btseleccionar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btconsultar"
android:layout_below="@+id/imagefoto"
android:layout_marginStart="6dp"
android:layout_marginTop="15dp"
android:onClick="boton_imagen"
android:text="@string/imagen" />
<Button
android:id="@+id/btlistview"
android:onClick="cargarseries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/btborrar"
android:layout_marginTop="9dp"
android:text="@string/listview" />
</RelativeLayout>
and the listview layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.josehp.imageview_listview.listview">
<ListView
android:id="@+id/listviewseries"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
attached images of the application
can someone help me? It is very difficult? as I comment I am new, everything I have been doing is based on watching videos and tutorials, how could the image stored in the base show on the listview ??