2016-09-09 10 views
0

私は問題がある、私は各画像パスに割り当てられたすべての名前がす​​べて同じ複数の画像パスを取得したい。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は、私が得たものである

... enter image description here

UPDATE:行0で

パスとrow1はすべての画像パスを表示するようになりました。ただし、recyclerviewの最後のインデックスまたは行がアプリをクラッシュさせます。あなたの方法で

+0

あなたはString locate = dbList.get(index).getPath();を置く必要があります。をループに挿入し、出力をlocate変数に連結します。 – Sandeep

+0

これはSQLの質問でしょうか?これを試したことがありますか? 'String query = "select * from" + TBNAME + "'%" + name + "% ';" ; ' – gus27

+0

私はあなたがSQLステートメントを更新し、今や 'LIKE'用語を含んでいるのを見ています。 'Log.d(" student data "、modelList.toString());の出力は何ですか? – gus27

答えて

1

次のように条件ブロック、それはあなたがクエリのように使用しているので、あなたのsqliteのテーブル内のすべてのデータが、それはすべて、その後の生のテーブルに関連する名前を返します

String locate = ""; 

    if(dbList.size() >0) 
    { 
     for(GettersSetters currentClass : dbList) 
     { 
     locate += currentClass.getPath() + ",";   
     } 
    } 
    txtLoc.setText(locate); 
+0

の1つだけが表示されます画像パスが表示されます –

+0

確認済みの回答を確認します – Vickyexpert

+0

dbクエリから得た結果をチェックします。複数のレコードまたは1つしかありません。 – Vickyexpert

0

チェックを動作する場合だけ変更。

String query = "select * from "+ TBNAME + " where name like '%" + name + "%';" 

これはあなたが選択したデータ

String query = "select * from "+ TBNAME + " where name like '%admin%';" 

のために、このクエリを使用するときに

admin 
administrator 
superadmin 
mainuser 

以下のようなテーブル名を持っている場合、クエリは、その後、あなたからの復帰最初の3つのデータを意味していますテーブル生

特定のデータを検索したい場合は、条件付きでwhereを使用する必要があります。

String query = "select * from "+ TBNAME + " where name = 'admin';" 

次に、唯一の結果が見つかりました。

+0

これはSQLインジェクションを要求しています。 'whereArgs'を受け入れる' query'メソッドを使うのがベストです –

+0

これはsqliteのクエリ情報のためのものであり、どんな種類の注入の意味でもありません。あなたのテーブルに表示すると、テーブルに重複した名前があるので、特定のデータを取得するために名前の代わりにIDを使用する必要があります。 –

+0

何を意味するのかわかりませんが、 'rawQuery'を連結文字列で使用することはセキュリティリスクです。それは私が言っているすべてです –

関連する問題