私は問題がある、私は各画像パスに割り当てられたすべての名前がすべて同じ複数の画像パスを取得したい。WHERE句から返された行を表示する方法
イメージパスの名前を同じ名前に割り当てました。同じ名前のすべてのイメージパスが取得されるアルバムのようにしたいからです。
私はこのクエリをやってみました:
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;
}
}
これは私が
RecyclerAdapter.java Flip.javaにを通過したIntent
です
@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); } } }
次に、次のコードを使用して結果を取得しようとしましたが、ここでの問題は1つの画像パスしか表示されていないことです。すべての画像パスを表示するにはどうしたらいいですか?助けてください、私は立ち往生しました。私はテーブルこの内のすべての行を選択し
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);
}
Flip.javaは、私が得たものである
UPDATE:行0で
パスとrow1はすべての画像パスを表示するようになりました。ただし、recyclerviewの最後のインデックスまたは行がアプリをクラッシュさせます。あなたの方法で
あなたはString locate = dbList.get(index).getPath();を置く必要があります。をループに挿入し、出力をlocate変数に連結します。 – Sandeep
これはSQLの質問でしょうか?これを試したことがありますか? 'String query = "select * from" + TBNAME + "'%" + name + "% ';" ; ' – gus27
私はあなたがSQLステートメントを更新し、今や 'LIKE'用語を含んでいるのを見ています。 'Log.d(" student data "、modelList.toString());の出力は何ですか? – gus27