繁体   English   中英

图像不在Android中的SQLite和logcat的viewpager中显示SqliteConstraint异常

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM