0

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;
}

}
SUGAN
  • 3
  • 3

1 Answers1

1

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);
}
muthugiridharan
  • 120
  • 1
  • 4
  • If I dont do that then it gives me this error: "The specified child already has a parent. You must call removeView() on the child's parent first." – SUGAN Sep 17 '19 at 03:45
  • Yes @SUGAN, It would throw Exception because you have declared image view global scope move it inside the loop. Look at the snippet I have added I have defined the Imageview inside the loop. – muthugiridharan Sep 17 '19 at 04:51
  • It doesn't allow me to define the ImageView inside the loop with "context". I think the method I am trying to use is incorrect is there any other method to do so. Or else correct me Mr. muthugiridharan – SUGAN Sep 17 '19 at 05:17
  • I think you are passing this instead of getApplicationContext() or getContext(). You wrote while loop inside the OnClickListener if you pass this as the argument for Imageview constructor It will take the instance of View class, not your Activity or fragment. So change this to getApplicationContext() for activity and getContext() for fragments. @SUGAN – muthugiridharan Sep 17 '19 at 07:14