[英]I'm trying to show images from firebase into a carousal view using a ViewPager and picasso but Images are not coming into view
[英]Images are not coming in viewpager from SQLite in Android and in logcat show SqliteConstraint Exception
我想從sqlite的viewpager
插入很多圖像,但是這些圖像沒有顯示在view pager中,而在logcat中則顯示SqliteConstraint異常錯誤。
但是,相同的代碼適用於Gridview,並且在gridview中的圖像可以正常顯示,但在View pager中不可用。 這是我引用的鏈接http://androidsurya.blogspot.in/2014/01/multiple-images-insert-and-retrieve.html
這是MainActivity
public class InsertandRetriveBlobData extends Activity
{
private DBhelper DbHelper;
public static final String EMP_ID = "id";
public static final String EMP_NAME = "name";
public static final String EMP_AGE = "age";
public static final String EMP_PHOTO = "photo";
ArrayList<Employee> employeeList = new ArrayList<Employee>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, employeeList);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
DbHelper = new DBhelper(this);
Employee employee_One = new Employee(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher), "Manish", 25);
//Employee employee_Two = new Employee(BitmapFactory.decodeResource(
// getResources(), R.drawable.ic_launcher), "Bondada", 26);
DbHelper.open();
// insert first employee one details
DbHelper.insertEmpDetails(employee_One);
// insert first employee two details
// DbHelper.insertEmpDetails(employee_Two);
employeeList = DbHelper.retriveallEmpDetails();
DbHelper.close();
/* GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this, employeeList));*/
}
}
這是適配器ViewPagerAdapter.java
public class ViewPagerAdapter extends PagerAdapter{
private Context mContext;
ArrayList<Employee> employeeList = new ArrayList<Employee>();
// Constructor
public ViewPagerAdapter(Context c, ArrayList<Employee> employeeList) {
mContext = c;
this.employeeList = employeeList;
}
@Override
public Object instantiateItem(View collection, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(employeeList.get(position).getBitmap());
System.out.println("image showing according to position"+employeeList.get(position).getBitmap());
((ViewPager) collection).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return employeeList.size();
}
}
public class DBhelper {
public static final String EMP_ID = "id";
public static final String EMP_NAME = "name";
public static final String EMP_AGE = "age";
public static final String EMP_PHOTO = "photo";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "EmployessDB.db";
private static final int DATABASE_VERSION = 1;
private static final String EMPLOYEES_TABLE = "Employees";
private static final String CREATE_EMPLOYEES_TABLE = "create table "
+ EMPLOYEES_TABLE + " (" + EMP_ID
+ " integer primary key autoincrement, " + EMP_PHOTO
+ " blob, " + EMP_NAME + " text, "
+ EMP_AGE + " integer );";
private final Context mCtx;
// create an empty array list with an initial capacity
ArrayList<Employee> employeeList = new ArrayList<Employee>();
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_EMPLOYEES_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + EMPLOYEES_TABLE);
onCreate(db);
}
}
public void Reset() {
mDbHelper.onUpgrade(this.mDb, 1, 1);
}
public DBhelper(Context ctx) {
mCtx = ctx;
mDbHelper = new DatabaseHelper(mCtx);
}
public DBhelper open() throws SQLException {
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public void insertEmpDetails(Employee employee) {
ContentValues cv = new ContentValues();
cv.put(EMP_PHOTO, Utility.getBytes(employee.getBitmap()));
cv.put(EMP_NAME, employee.getName());
cv.put(EMP_AGE, employee.getAge());
mDb.insert(EMPLOYEES_TABLE, null, cv);
}
// To get first employee details
public Employee retriveEmpDetails() throws SQLException {
Cursor cur = mDb.query(true, EMPLOYEES_TABLE, new String[] { EMP_PHOTO,
EMP_NAME, EMP_AGE }, null, null, null, null, null, null);
if (cur.moveToFirst()) {
byte[] blob = cur.getBlob(cur.getColumnIndex(EMP_PHOTO));
String name = cur.getString(cur.getColumnIndex(EMP_NAME));
int age = cur.getInt(cur.getColumnIndex(EMP_AGE));
cur.close();
return new Employee(Utility.getPhoto(blob), name, age);
}
cur.close();
return null;
}
// To get list of employee details
public ArrayList<Employee> retriveallEmpDetails() throws SQLException {
Cursor cur = mDb.query(true, EMPLOYEES_TABLE, new String[] { EMP_PHOTO,
EMP_NAME, EMP_AGE }, null, null, null, null, null, null);
if (cur.moveToFirst()) {
do {
byte[] blob = cur.getBlob(cur.getColumnIndex(EMP_PHOTO));
String name = cur.getString(cur.getColumnIndex(EMP_NAME));
int age = cur.getInt(cur.getColumnIndex(EMP_AGE));
employeeList
.add(new Employee(Utility.getPhoto(blob), name, age));
} while (cur.moveToNext());
}
return employeeList;
}
}
public class Employee {
private Bitmap bmp;
private String name;
private int age;
public Employee(Bitmap b, String n, int k) {
bmp = b;
name = n;
age = k;
}
public Employee(String n, int k) {
// TODO Auto-generated constructor stub
name = n;
age = k;
}
public Bitmap getBmp() {
return bmp;
}
public void setBmp(Bitmap bmp) {
this.bmp = bmp;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public Bitmap getBitmap() {
return bmp;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Utility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getPhoto(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
您可以在employeeList = DbHelper.retriveallEmpDetails();中找到完整的員工列表。 您沒有將其傳遞給viewpager適配器。 您之前傳遞的內容只是一個空的arraylist。 Gridview適配器可以工作,因為您在上面的代碼行之后對其進行了初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.