Slide the images using ViewFlipper stored in the database

91 Views Asked by At

I want to add the images stored in the database in the image view of the ViewFlipper. I have tried to do so but Only the last image stored is being displayed but not the all. I cannot find where i have made the mistakes. Here are my codes please help me out. I am new to android.

All the images in database are not being displayed in the viewflipper. Only last one image from the database is being displayed in the viewflipper. I want all the images from the database to be displayed in the viewflipper.

MainActivity.java

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editImagename;
Button btnAddData;
Button btnview;
ImageView imgview;
ViewFlipper viewflips;
private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    editImagename = findViewById(R.id.image_name);
    btnAddData = findViewById(R.id.addbtn);
    btnview = findViewById(R.id.getbtn);
    imgview = new ImageView(this);
    viewflips = findViewById(R.id.viewflip);
    viewAll();

    Button buttonLoadImage = findViewById(R.id.btn1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(Intent.ACTION_PICK,Uri.parse("content://media/internal/images/media"));
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

    myDb = new DatabaseHelper(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        final String x = getPath(uri);
        final String ImageName = editImagename.getText().toString();

        btnAddData.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View v){
                        boolean isInserted = myDb.insertData(ImageName, x);
                        if(isInserted == true)
                            Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
                    }
                }
        );

    }
}

private String getPath(Uri uri) {
    if (uri==null) return null;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri,projection,null,null,null);
    if (cursor!=null){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
}

public void viewAll() {
    btnview.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor res = myDb.getAllData();
                    if (res.getCount()!=0)
                    while(res.moveToNext()){
                        Uri mUri = Uri.parse(res.getString(2));
                        imgview.setImageURI(mUri);
                        viewflips.removeView(imgview);
                        viewflips.addView(imgview);
                    }
                }
            }
    );
}

}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Flight.db";
public static final String TABLE_NAME = "flight_table";
public static final String COL_1 = "ImageName";
public static final String COL_2 = "Image";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,ImageName TEXT,Image BLOB)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String ImageName, String x) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1,ImageName);
    contentValues.put(COL_2,x);
    db.insert(TABLE_NAME,null ,contentValues);
    return true;
}

Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
    return res;
}

}
1

There are 1 best solutions below

4
muthugiridharan On

Don't remove the imageview from viewflipper.

while(res.moveToNext()){
  Uri mUri = Uri.parse(res.getString(2));
  ImageView imgview = new ImageView(context);
  imgview.setImageURI(mUri);
  viewflips.addView(imgview);
}