图像不是来自Android中的SQLite的viewpager,而是在logcat中显示SqliteConstraint异常

时间:2016-04-05 06:46:08

标签: android

我想在sqlite的viewpager中插入许多图像,但是这些图像没有在view pager中显示,而在logcat中它显示了SqliteConstraint异常的错误。

但是,相同的代码适用于Gridview,并且gridview图像正常,但不在视图寻呼机中。这是我引用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);
    }
}

1 个答案:

答案 0 :(得分:1)

您拥有完整的员工名单 employeeList = DbHelper.retriveallEmpDetails(); 您没有将它传递给viewpager适配器。你之前传递的只是一个空的arraylist。 Gridview适配器有效,因为您在上面的代码行之后初始化它。