saeed1366412

نمایش عکس با SQlite

2 ارسال در این موضوع قرار دارد

ارسال شده در (ویرایش شده)

من یک سری عکس در  drawable که اسمشون رو در SQlite ذخیره کردم میخوام اونارو در listview نمایش بدم

کد دیتابیسم

public class database extends SQLiteOpenHelper {

    public final String   path = "data/data/com.example.asus.myapplication/databases/";
    public final String   Name = "mydb";
    public SQLiteDatabase mydb;
    private final Context mycontext;


    public database(Context context) {
        super(context, "mydb", null, 1);
        mycontext = context;
    }


    @Override
    public void onCreate(SQLiteDatabase arg0) {

    }


    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

    }


    public void useable() {
        boolean checkdb = checkdb();
        if (checkdb) {

        } else {
            this.getReadableDatabase();
            try {
                copydatabase();
            }
            catch (IOException e) {

            }
        }
    }


    public void open() {
        mydb = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READWRITE);
    }


    @Override
    public void close() {
        mydb.close();
    }


    public boolean checkdb() {
        SQLiteDatabase db = null;
        try {
            db = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READONLY);
        }
        catch (SQLException e) {

        }
        return db != null ? true : false;
    }


    public void copydatabase() throws IOException {

        OutputStream myOutput = new FileOutputStream(path + Name);
        byte[] buffer = new byte[1024];
        int lenght;
        InputStream myInput = mycontext.getAssets().open(Name);
        while ((lenght = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, lenght);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();
    }
    public String namayesh(int row , int field , String table){

        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table , null);
        Cursor.moveToPosition(row);
        String str = Cursor.getString(field);
        return str;
    }
    public Integer shomaresh_field(String table,String field){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by "+field, null);
        int i = Cursor.getCount();
        return i ;
    }
    public String namayesh_fasl(String table , int row){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by onvan", null);
        Cursor.moveToPosition(row);
        String s = Cursor.getString(1);
        return s ;
    }
    public String namayesh_aks(String table , int row){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by onvan", null);
        Cursor.moveToPosition(row);
        String s = Cursor.getString(4);
        return s ;
    }

    public Integer shomaresh_dastan (String table , String onvan){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" where onvan='"+onvan+"' group by matn", null);
        int s = Cursor.getCount();
        return s;

    }

    public String namayesh_dastan (String table , int row , String onvan , int field){
        Cursor Cursor = mydb.rawQuery("select * from "+table+" where onvan='"+onvan+"' group by matn",null);
        Cursor.moveToPosition(row);
        String save = Cursor.getString(field);
        return save;

    }




    public String jostojoo(int row , int col , String word , String field){
        Cursor cursor;
        if (field.equals("onvan")) {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%' group by onvan", null);
        }else {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%'", null);
        }
        cursor.moveToPosition(row);
        String save = cursor.getString(col);
        return save;
    }

    public Integer shmaresh_jostojoo(String word , String field){
        Cursor cursor;
        if (field.equals("onvan")) {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%' group by onvan" , null);
        }else {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%'", null);
        }
        int save = cursor.getCount();
        return save;
    }

    public void beroozresani_doostdashtaniha (String table , String onvan , String matn , String value){
        ContentValues cv = new ContentValues();

        cv.put("fav", value);
        mydb.update(table, cv, "onvan='"+onvan+"' and matn='"+matn+"'", null);
    }

    public Integer shomaresh_doosrdashtaniha(String table){
        Cursor Cursor = mydb.rawQuery("select * from "+table+" where fav=1 group by onvan", null);
        int save = Cursor.getCount();
        return save;
    }

    public String namayesh_doosrdashtaniha(String table , int row , int field){
        Cursor Cursor = mydb.rawQuery("select * from "+table+" where fav=1 group by onvan", null);
        Cursor.moveToPosition(row);
        String save = Cursor.getString(field);
        return save;
    }

}

 کد نمایش lisview

public class Activity2 extends ListActivity{

    private database db;
    private String[] Name;
    private String[] Teedad;
    private String[] Aks;

   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        db = new database(this);
        

        refresher();
        setListAdapter(new AA());

    }



    class AA extends ArrayAdapter<String>{

        public AA(){
            super(Activity2.this,R.layout.raw_seasone,Name);

        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater in = getLayoutInflater();
            View row = in.inflate(R.layout.raw_seasone, parent,false);

            TextView name = (TextView) row.findViewById(R.id.name_season);
            TextView teedad = (TextView) row.findViewById(R.id.teedad_dastan);
            ImageView aks = (ImageView)  row.findViewById(R.id.name_aks);








            name.setText(Name [position]);
            teedad.setText(Teedad [position]);


            return (row);
        }

    }

    private void refresher(){
        db.open();
        int save = db.shomaresh_field("dastanha", "onvan");
        Name = new String[save];
        Teedad = new String[save];
        Aks = new String[save];

        for (int i = 0; i <save; i++) {
            Name[i] = db.namayesh_fasl("dastanha", i);
            Teedad[i] = db.shomaresh_dastan("dastanha", Name[i].toString())+"";
            Aks[i] = db.namayesh_fasl("dastanha", i);
        }
        db.close();
    }
}

چه کدی اضافه کنم برای resource 

ویرایش شده در توسط saeed1366412

به اشتراک گذاری این پست


لینک به ارسال
به اشتراک گذاری در سایت های دیگر
در 2 ساعت قبل، saeed1366412 گفته است :

من یک سری عکس در  drawable که اسمشون رو در SQlite ذخیره کردم میخوام اونارو در listview نمایش بدم

کد دیتابیسم


public class database extends SQLiteOpenHelper {

    public final String   path = "data/data/com.example.asus.myapplication/databases/";
    public final String   Name = "mydb";
    public SQLiteDatabase mydb;
    private final Context mycontext;


    public database(Context context) {
        super(context, "mydb", null, 1);
        mycontext = context;
    }


    @Override
    public void onCreate(SQLiteDatabase arg0) {

    }


    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

    }


    public void useable() {
        boolean checkdb = checkdb();
        if (checkdb) {

        } else {
            this.getReadableDatabase();
            try {
                copydatabase();
            }
            catch (IOException e) {

            }
        }
    }


    public void open() {
        mydb = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READWRITE);
    }


    @Override
    public void close() {
        mydb.close();
    }


    public boolean checkdb() {
        SQLiteDatabase db = null;
        try {
            db = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READONLY);
        }
        catch (SQLException e) {

        }
        return db != null ? true : false;
    }


    public void copydatabase() throws IOException {

        OutputStream myOutput = new FileOutputStream(path + Name);
        byte[] buffer = new byte[1024];
        int lenght;
        InputStream myInput = mycontext.getAssets().open(Name);
        while ((lenght = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, lenght);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();
    }
    public String namayesh(int row , int field , String table){

        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table , null);
        Cursor.moveToPosition(row);
        String str = Cursor.getString(field);
        return str;
    }
    public Integer shomaresh_field(String table,String field){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by "+field, null);
        int i = Cursor.getCount();
        return i ;
    }
    public String namayesh_fasl(String table , int row){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by onvan", null);
        Cursor.moveToPosition(row);
        String s = Cursor.getString(1);
        return s ;
    }
    public String namayesh_aks(String table , int row){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by onvan", null);
        Cursor.moveToPosition(row);
        String s = Cursor.getString(4);
        return s ;
    }

    public Integer shomaresh_dastan (String table , String onvan){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" where onvan='"+onvan+"' group by matn", null);
        int s = Cursor.getCount();
        return s;

    }

    public String namayesh_dastan (String table , int row , String onvan , int field){
        Cursor Cursor = mydb.rawQuery("select * from "+table+" where onvan='"+onvan+"' group by matn",null);
        Cursor.moveToPosition(row);
        String save = Cursor.getString(field);
        return save;

    }




    public String jostojoo(int row , int col , String word , String field){
        Cursor cursor;
        if (field.equals("onvan")) {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%' group by onvan", null);
        }else {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%'", null);
        }
        cursor.moveToPosition(row);
        String save = cursor.getString(col);
        return save;
    }

    public Integer shmaresh_jostojoo(String word , String field){
        Cursor cursor;
        if (field.equals("onvan")) {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%' group by onvan" , null);
        }else {
            cursor = mydb.rawQuery("select * from dastanha where "+field+" like '%"+word+"%'", null);
        }
        int save = cursor.getCount();
        return save;
    }

    public void beroozresani_doostdashtaniha (String table , String onvan , String matn , String value){
        ContentValues cv = new ContentValues();

        cv.put("fav", value);
        mydb.update(table, cv, "onvan='"+onvan+"' and matn='"+matn+"'", null);
    }

    public Integer shomaresh_doosrdashtaniha(String table){
        Cursor Cursor = mydb.rawQuery("select * from "+table+" where fav=1 group by onvan", null);
        int save = Cursor.getCount();
        return save;
    }

    public String namayesh_doosrdashtaniha(String table , int row , int field){
        Cursor Cursor = mydb.rawQuery("select * from "+table+" where fav=1 group by onvan", null);
        Cursor.moveToPosition(row);
        String save = Cursor.getString(field);
        return save;
    }

}

 کد نمایش lisview


public class Activity2 extends ListActivity{

    private database db;
    private String[] Name;
    private String[] Teedad;
    private String[] Aks;

   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        db = new database(this);
        

        refresher();
        setListAdapter(new AA());

    }



    class AA extends ArrayAdapter<String>{

        public AA(){
            super(Activity2.this,R.layout.raw_seasone,Name);

        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater in = getLayoutInflater();
            View row = in.inflate(R.layout.raw_seasone, parent,false);

            TextView name = (TextView) row.findViewById(R.id.name_season);
            TextView teedad = (TextView) row.findViewById(R.id.teedad_dastan);
            ImageView aks = (ImageView)  row.findViewById(R.id.name_aks);








            name.setText(Name [position]);
            teedad.setText(Teedad [position]);


            return (row);
        }

    }

    private void refresher(){
        db.open();
        int save = db.shomaresh_field("dastanha", "onvan");
        Name = new String[save];
        Teedad = new String[save];
        Aks = new String[save];

        for (int i = 0; i <save; i++) {
            Name[i] = db.namayesh_fasl("dastanha", i);
            Teedad[i] = db.shomaresh_dastan("dastanha", Name[i].toString())+"";
            Aks[i] = db.namayesh_fasl("dastanha", i);
        }
        db.close();
    }
}

چه کدی اضافه کنم برای resource 

بدین صورت کد نمایش تصویر رو بنویسید

try {

			Resources res = context.getResources();
			int resourceId = res.getIdentifier("filename", "drawable",
					context.getPackageName());
		img_icon.setImageResource(resourceId);
		} catch (Exception e) {
			// TODO: handle exception
		}

 

به اشتراک گذاری این پست


لینک به ارسال
به اشتراک گذاری در سایت های دیگر

برای ارسال نظر یک حساب کاربری ایجاد کنید یا وارد حساب خود شوید

برای اینکه بتوانید نظر ارسال کنید نیاز دارید که کاربر سایت شوید

ایجاد یک حساب کاربری

برای حساب کاربری جدید در انجمن ما ثبت نام کنید. عضویت خیلی ساده است !


ثبت نام یک حساب کاربری جدید

ورود به حساب کاربری

دارای حساب کاربری هستید؟ از اینجا وارد شوید


ورود به حساب کاربری