简体   繁体   English

如何显示来自外部数据库的图像(kotlin)

[英]How to show image from an external database (kotlin)

Hello guys I want to make app with an External Database, I made my Database with SQLite browser, Put my image to drawable, I need to show image and text but it just shows text I mean it works (Image does not show also this is not any error log about it ) For more guidance I have some code in Adapter also I put gitlab link any help would be appreciate.大家好,我想用外部数据库制作应用程序,我用 SQLite 浏览器制作了我的数据库,将我的图像放入可绘制的,我需要显示图像和文本,但它只显示文本我的意思是它有效(图像也没有显示这是没有关于它的任何错误日志)为了获得更多指导,我在适配器中有一些代码,我也放了 gitlab 链接任何帮助将不胜感激。 Thank you谢谢


    class model {
        var id:Int?=null
        var name:String?=null
        var image:String?=null
    }


    object info_db {
    
        val dbName="country"
        val dbversion=2
        val dbId="id"
        val dbcname="name"
        val dbimage="image"
        val packageData="data/data/com.alialim.countrylist_sample/databases/"
        val source ="country.sqlite"
    
    }


    class Database(val context: Context): SQLiteOpenHelper(context,info_db.dbName,null,info_db.dbversion)  {
        override fun onCreate(p0: SQLiteDatabase?) {
        }
    
        override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
    
        }
    
        init {
            initDatabase()
        }
        private fun initDatabase() {
    
            var file = File(info_db.packageData)
    
            if (file.exists()) {
    
            } else {
                file.mkdirs()
            }
    
            file = context.getDatabasePath(info_db.dbName)
            if (file.exists()) {
    
            } else {
                copyDataBase()
            }
    
        }
    
        private fun copyDataBase() {
            val inputStream = context.assets.open(source)
            val outFile = packageData + dbName
            val uotPutStream = FileOutputStream(outFile)
            val buffer = ByteArray(1024)
            var lenght = 0
            while (inputStream.read(buffer).also({ lenght = it }) > 0)
                uotPutStream.write(buffer, 0, lenght)
            uotPutStream.flush()
            uotPutStream.close()
            inputStream.close()
        }
    
        @SuppressLint("Range")
        fun getAll(): ArrayList<model> {
            val db = readableDatabase
            val query = "SELECT * FROM countrylist"
            var list: ArrayList<model> = ArrayList()
            val cursor = db.rawQuery(query, null)
            if (cursor.moveToFirst()) {
                do {
                    val lmodel = model()
                    lmodel.id=cursor.getInt(cursor.getColumnIndex(info_db.dbId))
                    lmodel.name=cursor.getString(cursor.getColumnIndex(info_db.dbcname))
    
                    lmodel.image=cursor.getString(cursor.getColumnIndex(info_db.dbimage))
                    list.add(lmodel)
                    Log.d(
                        "massage",
                        "log Database name ==>${lmodel.name} and category ==> ${lmodel.image}  "
                    )
                } while (cursor.moveToNext())
            }
            cursor.close()
            db.close()
            return list
        }
    }


    object Base: Application() {
        var database: Database?=null
        var activity: Activity?=null
    
        override fun onCreate() {
            super.onCreate()
        }
    }


    class AdapterCountry(val context: Context, val countrylists:ArrayList<model>): RecyclerView.Adapter<AdapterCountry.HolderCountry>() {
    
        class HolderCountry(itemView: View): RecyclerView.ViewHolder(itemView){
            val imagerecy=itemView.findViewById<ImageView>(R.id.recycler_main_image)
            val txtfname=itemView.findViewById<TextView>(R.id.recycler_main_text)
    
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HolderCountry {
            val layout= LayoutInflater.from(parent.context).inflate(R.layout.item,parent,false)
            return HolderCountry(layout)
        }
        override fun onBindViewHolder(holder: HolderCountry, position: Int) {
            val countrylist=countrylists.get(position)
            holder.txtfname.text=countrylist.name
            //code that should display the images but dosent work
            val img=countrylist.image
            val Image: Int = Base.activity!!.getResources()
                .getIdentifier(img, "drawable", Base.activity!!.getPackageName())
            holder.imagerecy.setImageResource(Image)
            //////////////
        }
        override fun getItemCount()=countrylists.size
    }


    class MainActivity : AppCompatActivity() {
    
        var btnclick: Button?=null
        var recycler: RecyclerView?=null
        var adaptecounry: AdapterCountry?=null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            btnclick=findViewById(R.id.showcountrye)
            recycler=findViewById(R.id.mainactivity_recy_image)
            Base.activity=this
            Base.database= Database(this)
            btnclick!!.setOnClickListener {
                recycler!!.setHasTransientState(true)
                recycler!!.layoutManager= LinearLayoutManager(Base.activity)
                val list= Base.database!!.getAll()
                adaptecounry = AdapterCountry(Base.activity!!,list)
                recycler!!.adapter=adaptecounry
    
            }
    
        }
    
    }

SQLitebrowser drawable SQLitebrowser可绘制

please visit: https://gitlab.com/salin1/country_list/-/tree/master请访问: https://gitlab.com/salin1/country_list/-/tree/master

Hello everybody I could fix my problem because I was Short of time anyway I writ my correct code here maybe one day somebody like me needed it, My cod is correct but you need to put this code in your RecyclerView Adapter大家好,我可以解决我的问题,因为无论如何我时间不够,我在这里写了正确的代码,也许有一天像我这样的人需要它,我的代码是正确的,但您需要将此代码放入您的 RecyclerView 适配器

   

override fun onBindViewHolder(holder: HolderCountry, position: Int) {

        val countrylist=countrylists.get(position)
        Log.i("imaaagee","${countrylist.image}")
        holder.txtfname.text=countrylist.name
        val img=countrylist.name
        img!!.trim()
        val identifier=context.resources.getIdentifier(img,"drawable",
        context!!.getPackageName())
        holder.imagerecy.setImageResource(identifier)

    }

also I put link and image if you look at my database image you see in image column I write image name with format (.png) but its mistake because getIdentifier does not need image format the all thing you should do is put your image to drawable and recover with your adapter code for more understanding I put my gitlab link you can download and see code if you have any question just ask I will respond Thank you如果您查看在图像列中看到的我的数据库图像,我也会放置链接和图像并使用您的适配器代码恢复以获得更多理解我把我的 gitlab 链接您可以下载并查看代码如果您有任何问题只是问我会回复谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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