2017-04-27 73 views
0

我不明白爲什麼我的recyclerview只顯示我的數據庫的最後一行,儘管我用5個數據初始化了它。我在互聯網上搜索了很多,甚至更高級,但沒有任何解決方案正在工作。你能幫我弄清楚爲什麼我的recyclerView只顯示數據庫的最後一行嗎?在此先感謝大家。Recyclerview只顯示我的數據庫的最後一行

RecyclerView.adapter類別:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { 
Context mContext; 
ArrayList<ImageInfos> imageInfosArrayList; 

public RecyclerAdapter(Context ctx,ArrayList<ImageInfos> imageInfoses){ 
    this.imageInfosArrayList= new ArrayList<ImageInfos>(); 
    this.mContext=ctx; 
    this.imageInfosArrayList=imageInfoses; 

} 



@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,null); 
    return new ViewHolder(view); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

    holder.imageView.setImageResource(R.drawable.screen1); 
    holder.year.setText(holder.year.getText()+imageInfosArrayList.get(position).getYear()); 
    holder.keyword.setText(holder.keyword.getText()+imageInfosArrayList.get(position).getKeyword()); 
    holder.place.setText(holder.place.getText()+imageInfosArrayList.get(position).getPlace()); 
    holder.resume.setText(holder.resume.getText()+imageInfosArrayList.get(position).getResume()); 

    /* holder.year.setText(holder.year.getText()+ImageInfos.Year); 
    holder.keyword.setText(holder.keyword.getText()+ImageInfos.Keyword); 
    holder.place.setText(holder.place.getText()+ImageInfos.Place); 
    holder.resume.setText(holder.resume.getText()+ImageInfos.Resume); 
    */ 
    //holder.setIsRecyclable(false); 

} 

@Override 
public int getItemCount() { 
    return imageInfosArrayList.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder{ 
    ImageView imageView; 
    TextView year; 
    TextView keyword; 
    TextView place; 
    TextView resume; 


    public ViewHolder(View view) { 
     super(view); 
     imageView=(ImageView) view.findViewById(R.id.imageID); 
     imageView.setAdjustViewBounds(true); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 

     year=(TextView) view.findViewById(R.id.year_text_view); 
     keyword=(TextView) view.findViewById(R.id.keyword_text_view); 
     place=(TextView) view.findViewById(R.id.place_text_view); 
     resume=(TextView) view.findViewById(R.id.resume_text_view); 
    } 
} 

}

DBhelper.class:

public class DBHelper extends SQLiteOpenHelper { 
// Database and Table version 
private static final String DATABASE_NAME = "Images.db"; 
public static final String TABLE_NAME = "Images"; 

//Columns 
public static final String COLUMN_ID = "ID"; 
public static final String COLUMN_IMAGE = "Image"; 
public static final String COLUMN_YEAR = "Year"; 
public static final String COLUMN_KEYWORD = "Keyword"; 
public static final String COLUMN_PLACE = "Place"; 
public static final String COLUMN_RESUME="Resume"; 
//Database version 
private static final int DATABASE_VERSION = 1; 

// Database creation sql statement 
// + COLUMN_IMAGE+ " BLOB NOT NULL," 
private static final String DATABASE_CREATE = "CREATE TABLE " 
     + TABLE_NAME + "(" 
     + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
     + COLUMN_IMAGE+" BLOB," 
     + COLUMN_YEAR+ " TEXT," 
     +COLUMN_KEYWORD+ " TEXT," 
     +COLUMN_PLACE+ " TEXT," 
     + COLUMN_RESUME+ " TEXT);"; 
Context mContext; 


public DBHelper(Context context) { 

    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    this.mContext=context; 
} 


@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(DATABASE_CREATE); 
    Toast.makeText(mContext,"Data Base created",Toast.LENGTH_LONG).show(); 
} 
public boolean insertImageWithInfos(ImageInfos imageInfos) throws SQLException{ 


    SQLiteDatabase sqLiteDatabase= this.getWritableDatabase(); 
    ContentValues contentValues= new ContentValues(); 
    // contentValues.put(COLUMN_ID,imageInfos.getId()); 
    contentValues.put(COLUMN_IMAGE, imageInfos.getImage()); 
    contentValues.put(COLUMN_KEYWORD,imageInfos.getKeyword()); 
    contentValues.put(COLUMN_YEAR,imageInfos.getYear()); 
    contentValues.put(COLUMN_PLACE,imageInfos.getPlace()); 
    contentValues.put(COLUMN_RESUME,imageInfos.getResume()); 
    long i= sqLiteDatabase.insert(TABLE_NAME, null,contentValues); 
    if (i==-1) 
    { 
     Toast.makeText(mContext,"Elements are not inserted",Toast.LENGTH_LONG).show(); 
     return false; 
    } 
    else 
    { 
     sqLiteDatabase.close(); 
     Toast.makeText(mContext,"Elements are inserted",Toast.LENGTH_LONG).show(); 
     return true; 

    } 



} 

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

public ArrayList<String> getAllImages() { 
    ArrayList<String> array_list = new ArrayList<String>(); 

    //hp = new HashMap(); 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor res = db.rawQuery("select * from "+TABLE_NAME, null); 
    res.moveToFirst(); 

    while(res.isAfterLast() == false){ 
     array_list.add(res.getString(res.getColumnIndex(COLUMN_ID))); 
     res.moveToNext(); 
    } 
    return array_list; 
} 
/*public void showDbElements(){ 
    SQLiteDatabase db=this.getReadableDatabase(); 
    Cursor cursor= db.rawQuery("select * from "+TABLE_NAME,null); 
    if (!cursor.isAfterLast()){ 

    } 
} 
*/ 

public ArrayList<ImageInfos> getValuesFromDataBase() { 
    ArrayList<ImageInfos> imageInfosArrayList = new ArrayList<ImageInfos>(); 
    SQLiteDatabase db = getReadableDatabase(); 
    Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null); 

    if (cursor != null && cursor.moveToFirst()) 

    { 
     Log.e("moveToCursor: ", "Cursor.moveToFirst is true"); 

      while(cursor.isAfterLast()==false) 
       { 
        ImageInfos imageInfos=new ImageInfos(mContext); 
        imageInfos.setImage(); 
        imageInfos.setYear(cursor.getString(cursor.getColumnIndex(COLUMN_YEAR))); 
        imageInfos.setKeyword(cursor.getString(cursor.getColumnIndex(COLUMN_KEYWORD))); 
        imageInfos.setPlace(cursor.getString(cursor.getColumnIndex(COLUMN_PLACE))); 
        imageInfos.setResume(cursor.getString(cursor.getColumnIndex(COLUMN_RESUME))); 
        imageInfosArrayList.add(imageInfos); 
        cursor.moveToNext(); 


      } 
     } 
     cursor.close(); 
     db.close(); 
     Log.e("database elements: ", String.valueOf(imageInfosArrayList.size())); 

    return imageInfosArrayList; 

    } 
public Bitmap getImage(){ 
    SQLiteDatabase db=getReadableDatabase(); 


    Cursor cursor=db.rawQuery("select "+COLUMN_IMAGE+" from "+TABLE_NAME, new String[]{}); 
    if (cursor==null) 
    { 
     Toast.makeText(mContext,"Cursor is empty",Toast.LENGTH_LONG).show(); 
    } 
    cursor.moveToFirst(); 
    byte[] blob=cursor.getBlob(cursor.getColumnIndex(COLUMN_IMAGE)); 
     ByteArrayInputStream inputStream = new ByteArrayInputStream(blob); 
     Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
     return bitmap; 
} 

}

模型類 「ImageInfos」:

public class ImageInfos { 
public static long ID; 
public static String Year; 
public static String Keyword; 
public static String Place; 
public static String Resume; 
public static byte[] imageDB; 
Context mContext; 


public Integer[] mImages= { 
     R.drawable.screen1,R.drawable.screen2,R.drawable.screen3, 
     R.drawable.screen4,R.drawable.screen5,R.drawable.screen6, 
     R.drawable.screen7,R.drawable.screen8,R.drawable.screen9, 
     R.drawable.screen10,R.drawable.screen11,R.drawable.screen12, 
     R.drawable.screen13,R.drawable.screen14,R.drawable.screen15, 
     R.drawable.screen16,R.drawable.screen17,R.drawable.screen18, 
     R.drawable.screen19,R.drawable.screen20,R.drawable.screen21, 
}; 

public ImageInfos(Context ctx){ 
    this.mContext=ctx; 

} 
public ImageInfos(String Year, String keyword,String Place, String Resume){ 
    this.Year=Year; 
    this.Keyword=keyword; 
    this.Place=Place; 
    this.Resume=Resume; 
} 

public long setId(long id){ 
    return this.ID=id; 
} 
public long getId(){ 
    return this.ID; 
} 

public void setYear(String Year){ 
    this.Year=Year; 
} 

public String getYear(){ 
    return Year; 
} 
public void setKeyword(String Keyword){ 
    this.Keyword=Keyword; 
} 
public String getKeyword(){ 
    return Keyword; 
} 
public void setPlace(String Place){ 
    this.Place=Place; 
} 
public String getPlace(){ 
    return Place; 
} 
public void setResume(String Resume){ 
    this.Resume=Resume; 
} 
public String getResume(){ 
    return Resume; 
} 


public void setImage(){ 


    Drawable drawable= ContextCompat.getDrawable(mContext,R.drawable.screen2); 
    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap(); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 0, stream); 

    //byte[] bitMapData = stream.toByteArray(); 
    imageDB=stream.toByteArray(); 

} 




public byte[] getImage(){ 
    return imageDB; 

} 

}

其中數據會顯示「ShowData活動activityw:當您創建recyclerview cardview_layout.xml應該遵循這些東西

ublic class ShowData extends AppCompatActivity { 
DBHelper dbHelper; 

ArrayList<ImageInfos> imageInfosArrayList; 
RecyclerView recyclerView; 
RecyclerAdapter recyclerAdapter; 
LinearLayoutManager mLayoutManager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_data); 

    recyclerView=(RecyclerView) findViewById(R.id.recyclerviewImage); 
    dbHelper=new DBHelper(this); 
    imageInfosArrayList= new ArrayList<ImageInfos>(); 
    imageInfosArrayList=dbHelper.getValuesFromDataBase(); 

    recyclerAdapter= new RecyclerAdapter(this,imageInfosArrayList); 
    mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false); 
    recyclerView.setLayoutManager(mLayoutManager); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.scrollToPosition(0); 
    recyclerView.setAdapter(recyclerAdapter); 

     } 

}

+1

調試您的代碼並檢查您用作RecyclerAdapter構造函數參數的列表是否包含多個項目。如果有,請檢查imageInfosArrayList元素。如果它不工作,請嘗試關閉imageInfosArrayList = imageInfoses for imageInfosArrayList.addAll(imageInfoses); –

+0

檢查您傳遞給適配器的'imageInfosArrayList'中包含的項目數。 – Shark

+0

我已經完成了。它顯示我的ArrayList包含5個元素,但只顯示最後一行(第5個元素)。 – Bayram

回答

1

ImageInfos類中的字段是靜態的,因此與類的所有實例共享。要使字段實例特定,請刪除static關鍵字。

+0

你對拉爾託。它工作完美。感謝大家 – Bayram

0

始終使用「wrap_content」作爲行的高度,否則在「match_parent」中它將佔用整行的單個行。

相關問題