I have a problem, I want to retrieve multiple images path where all the names assigned to each image path are all the same.
I assigned the name of the image path to be the same because I want to make it like an album where the all the image path having the same name will be retrieved.
I tried doing this query:
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
public static final int VERSION = 1;
public static final String DBNAME = "timelife";
public static final String TBNAME = "album";
private static DatabaseHandler sInstance;
private static synchronized DatabaseHandler getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHandler(context.getApplicationContext());
}
return sInstance;
}
public DatabaseHandler(Context context) {
super(context, DBNAME, null, VERSION);
}
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE IF NOT EXISTS " +
TBNAME + " ( name varchar(255) , path VARCHAR(255) );";
db.execSQL(query);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query2 = "DROP TABLE IF EXISTS " + TBNAME + " ;";
db.execSQL(query2);
onCreate(db);
}
public void insert(String[] apath, String title) {
ArrayList<String> strings = new ArrayList<String>();
for (String string : apath) {
strings.add(string);
}
Iterator iterate = strings.iterator();
String query = "INSERT INTO " + TBNAME + " VALUES (?,?);";
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
SQLiteStatement statement = db.compileStatement(query);
while (iterate.hasNext()) {
statement.clearBindings();
statement.bindString(1, title);
statement.bindString(2, iterate.next().toString());
statement.execute();
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
Log.w("Inserting", "Successful");
}
public List<GettersSetters> getDataFromDB(){
List<GettersSetters> modelList = new ArrayList<GettersSetters>();
String query = "select * from "+ TBNAME + " group by name;";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()){
do {
GettersSetters model = new GettersSetters();
model.setName(cursor.getString(0));
model.setPath(cursor.getString(1));
modelList.add(model);
}while (cursor.moveToNext());
}
Log.d("student data", modelList.toString());
cursor.close();
db.close();
return modelList;
}
public List<GettersSetters> searchFromDB(String name){
List<GettersSetters> modelList = new ArrayList<GettersSetters>();
String query = "select * from "+ TBNAME + " where name like '%" + name + "%';" ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()){
do {
GettersSetters model = new GettersSetters();
model.setName(cursor.getString(0));
model.setPath(cursor.getString(1));
modelList.add(model);
}while (cursor.moveToNext());
}
Log.d("student data", modelList.toString());
return modelList;
}
}
This is the Intent
that I passed in Flip.java
RecyclerAdapter.java
@Override
public void onClick(View v) {
String location = name.getText().toString();
Intent goFlip = new Intent(RecyclerAdapter.context, FlipActivity.class);
Bundle bundle = new Bundle();
bundle.putString("path", location);
bundle.putInt("pos", getAdapterPosition());
goFlip.putExtras(bundle);
context.startActivity(goFlip);
}
}
}
And then I tried to retrieve the results using the following code, but the problem here is that only one image path is showing. How can I make all the image path show? Please help, Im stuck.
Flip.java
txtLoc = (TextView) findViewById(R.id.samp);
Intent goFlip = getIntent();
Bundle bundle = goFlip.getExtras();
String getLocation = bundle.getString("path");
index =bundle.getInt("pos");
db = new DatabaseHandler(this);
dbList = new ArrayList<GettersSetters>();
dbList = db.searchFromDB(getLocation);
if(dbList.size() >0){
String locate = dbList.get(index).getPath();
txtLoc.setText(locate);
}
I selected all the rows in table this is what I got..
UPDATE:
The path in row 0 and row1 now shows all the images path. However, the last index or row in the recyclerview makes the app crash.