Commit 16ad45d7 authored by m-spi's avatar m-spi

rien qui marche

parent fd794904
import android.content.Context import android.content.Context
import android.net.Uri
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
...@@ -21,15 +22,14 @@ class AdAdapter(private val context: Context, private val adModelArrayList: List ...@@ -21,15 +22,14 @@ class AdAdapter(private val context: Context, private val adModelArrayList: List
fun usingJavaStringFormat(input: Double, scale: Int) = String.format("%.${scale}f", input) fun usingJavaStringFormat(input: Double, scale: Int) = String.format("%.${scale}f", input)
override fun getView(i: Int, view: View?, viewGroup: ViewGroup): View { override fun getView(i: Int, view: View?, viewGroup: ViewGroup): View {
var convertView = view
val ad = adModelArrayList[i] val ad = adModelArrayList[i]
convertView = inflater.inflate(R.layout.item_listview_ad, viewGroup, false) val convertView: View = inflater.inflate(R.layout.item_listview_ad, viewGroup, false)
val imageIV: ImageView = convertView.findViewById(R.id.itemListViewImageView) val imageIV: ImageView = convertView.findViewById(R.id.itemListViewImageView)
val titleTV: TextView = convertView.findViewById(R.id.itemListViewTitleView) val titleTV: TextView = convertView.findViewById(R.id.itemListViewTitleView)
val addressTV: TextView = convertView.findViewById(R.id.itemListViewTextView) val addressTV: TextView = convertView.findViewById(R.id.itemListViewTextView)
val prixTV: TextView = convertView.findViewById(R.id.itemListViewPrixView) val prixTV: TextView = convertView.findViewById(R.id.itemListViewPrixView)
imageIV.setImageResource(ad.image) imageIV.setImageURI(Uri.parse(ad.image))
titleTV.text = ad.title titleTV.text = ad.title
addressTV.text = ad.address addressTV.text = ad.address
prixTV.text = "${usingJavaStringFormat(ad.prix, 2)} €" prixTV.text = "${usingJavaStringFormat(ad.prix, 2)} €"
......
...@@ -2,6 +2,7 @@ package com.example.elbuenopeso.adapters ...@@ -2,6 +2,7 @@ package com.example.elbuenopeso.adapters
import android.content.Context import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.net.Uri
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
...@@ -10,12 +11,11 @@ import android.widget.ImageView ...@@ -10,12 +11,11 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import com.example.elbuenopeso.R import com.example.elbuenopeso.R
import com.example.elbuenopeso.database.DBHelper import com.example.elbuenopeso.database.DBHelper
import
class DbAdapter(context: Context, c: Cursor, layout: Int): CursorAdapter(context, c) { class DbAdapter(context: Context, c: Cursor, layout: Int): CursorAdapter(context, c) {
val item_layout = layout val item_layout = layout
override fun newView(context: Context, cursor: Cursor, viewGroup: ViewGroup): View { override fun newView(context: Context, cursor: Cursor, viewGroup: ViewGroup): View {
return LayoutInflater.from(context).inflate(item_layout, viewGroup, false); return LayoutInflater.from(context).inflate(item_layout, viewGroup, false)
} }
override fun bindView(view: View, context: Context, cursor: Cursor) { override fun bindView(view: View, context: Context, cursor: Cursor) {
...@@ -24,16 +24,17 @@ class DbAdapter(context: Context, c: Cursor, layout: Int): CursorAdapter(context ...@@ -24,16 +24,17 @@ class DbAdapter(context: Context, c: Cursor, layout: Int): CursorAdapter(context
val imageView: ImageView = view.findViewById(R.id.itemListViewImageView) val imageView: ImageView = view.findViewById(R.id.itemListViewImageView)
val prixView: TextView = view.findViewById(R.id.itemListViewPrixView) val prixView: TextView = view.findViewById(R.id.itemListViewPrixView)
val id: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper._ID)) val _id: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper._ID))
val title: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.TITLE)) val title: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.TITLE))
val address: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.ADDRESS)) val address: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.ADDRESS))
val prix: Double = cursor.getDouble(cursor.getColumnIndexOrThrow(DBHelper.PRIX.toString())) val prix: Double = cursor.getDouble(cursor.getColumnIndexOrThrow(DBHelper.PRIX))
val image: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.IMAGE)) val image: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.IMAGE))
titleTextView.setText(title) titleTextView.setText(title)
addressTextView.setText(address) addressTextView.setText(address)
prixView.setText(prix.toString()) prixView.setText(prix.toString())
imageView.setImageURI(Uri.parse(image))
//with(Glide) //with(Glide)
//TODO: trouver une alternative !!! //TODO: trouver une alternative !!!
} }
......
...@@ -3,23 +3,25 @@ package com.example.elbuenopeso.database ...@@ -3,23 +3,25 @@ package com.example.elbuenopeso.database
import android.content.Context import android.content.Context
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import android.util.Log
import com.example.elbuenopeso.models.AdModel import com.example.elbuenopeso.models.AdModel
class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
companion object { companion object {
const val TABLE_NAME = "publication_table" const val TABLE_NAME = "product"
const val _ID = "unID" const val _ID = "_id"
const val TITLE = "Mur en briques" const val TITLE = "title"
const val ADDRESS = "5 Rue de Monsieur Spi" const val ADDRESS = "address"
const val IMAGE = "theURLblablabla" const val IMAGE = "image_url"
const val PRIX = 25.00 const val PRIX = "price"
private const val DB_NAME = "LEBONCOIN.DB" private const val DB_NAME = "LEBONCOIN.DB"
private const val DB_VERSION = 1 private const val DB_VERSION = 1
private val CREATE_TABLE = "create table $TABLE_NAME ($_ID INTEGER PRIMARY KEY AUTOINCREMENT, $TITLE TEXT NOT NULL, $ADDRESS TEXT, $IMAGE TEXT);" const val CREATE_TABLE = "CREATE TABLE $TABLE_NAME ($_ID INTEGER PRIMARY KEY AUTOINCREMENT, $TITLE TEXT NOT NULL, $ADDRESS TEXT, $IMAGE TEXT, $PRIX REAL NOT NULL);"
} }
override fun onCreate(db: SQLiteDatabase) { override fun onCreate(db: SQLiteDatabase) {
Log.i(null, "table sql create : $CREATE_TABLE")
db.execSQL(CREATE_TABLE) db.execSQL(CREATE_TABLE)
} }
...@@ -32,16 +34,17 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V ...@@ -32,16 +34,17 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
val db = this.writableDatabase val db = this.writableDatabase
val query = "SELECT * FROM $TABLE_NAME where $_ID=?" val query = "SELECT * FROM $TABLE_NAME where $_ID=?"
val data = db.rawQuery(query, arrayOf(id.toString())) val data = db.rawQuery(query, arrayOf(id.toString()))
if (data.moveToFirst()) {
return if (data.moveToFirst()) {
val title = data.getString(data.getColumnIndexOrThrow(TITLE)) val title = data.getString(data.getColumnIndexOrThrow(TITLE))
val address = data.getString(data.getColumnIndexOrThrow(ADDRESS)) val address = data.getString(data.getColumnIndexOrThrow(ADDRESS))
val image = data.getString(data.getColumnIndexOrThrow(IMAGE)) val image = data.getString(data.getColumnIndexOrThrow(IMAGE))
val prix = data.getDouble(data.getColumnIndexOrThrow(PRIX.toString())) val prix = data.getDouble(data.getColumnIndexOrThrow(PRIX))
data.close() data.close()
return AdModel(title, address, image, prix) AdModel(title, address, image, prix)
} else { } else {
data.close() data.close()
return null null
} }
} }
} }
......
...@@ -5,6 +5,7 @@ import android.content.Context ...@@ -5,6 +5,7 @@ import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.database.SQLException import android.database.SQLException
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.util.Log
import com.example.elbuenopeso.models.AdModel import com.example.elbuenopeso.models.AdModel
...@@ -14,19 +15,19 @@ private constructor(private val context: Context) { ...@@ -14,19 +15,19 @@ private constructor(private val context: Context) {
private var database: SQLiteDatabase? = null private var database: SQLiteDatabase? = null
@Throws(SQLException::class) @Throws(SQLException::class)
fun open(): DBManager { fun open(): DBManager {
dbHelper = DBHelper(context) this.dbHelper = DBHelper(this.context)
database = dbHelper!!.writableDatabase this.database = this.dbHelper!!.writableDatabase
return this return this
} }
fun close() { fun close() {
dbHelper?.close() this.dbHelper?.close()
} }
// Add ads manually. // Add ads manually.
fun init() { fun init() {
open() this.open()
insert( this.insert(
AdModel( AdModel(
"Wood", "Wood",
"Douai", "Douai",
...@@ -34,35 +35,35 @@ private constructor(private val context: Context) { ...@@ -34,35 +35,35 @@ private constructor(private val context: Context) {
25.0 25.0
) )
) )
insert( this.insert(
AdModel( AdModel(
"Steel", "Steel",
"Lille", "Lille",
"https://as2.ftcdn.net/v2/jpg/03/91/83/87/1000_F_391838708_4HFADW5beay2VVlnoual6Qi5fWeIaD9V.jpg",25.0 "https://as2.ftcdn.net/v2/jpg/03/91/83/87/1000_F_391838708_4HFADW5beay2VVlnoual6Qi5fWeIaD9V.jpg",25.0
) )
) )
insert( this.insert(
AdModel( AdModel(
"Clay", "Clay",
"Douai", "Douai",
"https://constrofacilitator.com/wp-content/uploads/2020/02/clay-in-construction.jpg",25.0 "https://constrofacilitator.com/wp-content/uploads/2020/02/clay-in-construction.jpg",25.0
) )
) )
insert( this.insert(
AdModel( AdModel(
"Metal", "Metal",
"Lyon", "Lyon",
"https://www.meto-constructions.fr/wp-content/uploads/2018/12/IMG_6067.jpg",25.0 "https://www.meto-constructions.fr/wp-content/uploads/2018/12/IMG_6067.jpg",25.0
) )
) )
insert( this.insert(
AdModel( AdModel(
"Glass", "Glass",
"Valenciennes", "Valenciennes",
"https://i0.wp.com/www.tipsnepal.com/wp-content/uploads/2020/09/simple-float-glass-1505049573-3306125.jpeg?resize=500%2C317&quality=100&strip=all&ssl=1",25.0 "https://i0.wp.com/www.tipsnepal.com/wp-content/uploads/2020/09/simple-float-glass-1505049573-3306125.jpeg?resize=500%2C317&quality=100&strip=all&ssl=1",25.0
) )
) )
insert( this.insert(
AdModel( AdModel(
"Wood", "Wood",
"Orchies", "Orchies",
...@@ -76,13 +77,14 @@ private constructor(private val context: Context) { ...@@ -76,13 +77,14 @@ private constructor(private val context: Context) {
contentValue.put(DBHelper.TITLE, ad.title) contentValue.put(DBHelper.TITLE, ad.title)
contentValue.put(DBHelper.ADDRESS, ad.address) contentValue.put(DBHelper.ADDRESS, ad.address)
contentValue.put(DBHelper.IMAGE, ad.image) contentValue.put(DBHelper.IMAGE, ad.image)
database!!.insert(DBHelper.TABLE_NAME, null, contentValue) this.database!!.insert(DBHelper.TABLE_NAME, null, contentValue)
} }
fun fetch(): Cursor? { fun fetch(): Cursor? {
val columns = val columns: Array<String> =
arrayOf<String>(DBHelper._ID, DBHelper.TITLE, DBHelper.ADDRESS, DBHelper.IMAGE, DBHelper.PRIX.toString()) arrayOf(DBHelper._ID, DBHelper.TITLE, DBHelper.ADDRESS, DBHelper.IMAGE, DBHelper.PRIX)
val cursor = database!!.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null) columns.forEach { e -> Log.i(null, e) }
val cursor = this.database!!.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null)
cursor?.moveToFirst() cursor?.moveToFirst()
return cursor return cursor
} }
...@@ -92,7 +94,7 @@ private constructor(private val context: Context) { ...@@ -92,7 +94,7 @@ private constructor(private val context: Context) {
contentValues.put(DBHelper.TITLE, ad.title) contentValues.put(DBHelper.TITLE, ad.title)
contentValues.put(DBHelper.ADDRESS, ad.address) contentValues.put(DBHelper.ADDRESS, ad.address)
contentValues.put(DBHelper.IMAGE, ad.image) contentValues.put(DBHelper.IMAGE, ad.image)
return database!!.update( return this.database!!.update(
DBHelper.TABLE_NAME, DBHelper.TABLE_NAME,
contentValues, contentValues,
DBHelper._ID + " = " + _id, DBHelper._ID + " = " + _id,
...@@ -101,20 +103,20 @@ private constructor(private val context: Context) { ...@@ -101,20 +103,20 @@ private constructor(private val context: Context) {
} }
fun delete(_id: Long) { fun delete(_id: Long) {
database!!.delete(DBHelper.TABLE_NAME, DBHelper._ID + "=" + _id, null) this.database!!.delete(DBHelper.TABLE_NAME, DBHelper._ID + "=" + _id, null)
} }
fun getById(id: Int): AdModel? { fun getById(id: Int): AdModel? {
return dbHelper?.getById(id) return this.dbHelper?.getById(id)
} }
companion object { companion object {
var DBManager: DBManager? = null var DBManager: DBManager? = null
// normalement pas de fuite, puisque 1 seule activité // normalement pas de fuite, puisque 1 seul contexte
fun getDBManager(context: Context): DBManager? { fun getDBManager(context: Context): DBManager? {
return if (DBManager == null) { return if (this.DBManager == null) {
DBManager(context) DBManager(context)
} else DBManager } else this.DBManager
} }
} }
} }
......
package com.example.elbuenopeso.models package com.example.elbuenopeso.models
class DbAdModel(var id: Int, var title: String, var address: String, var image: String, var prix: Double) { class DbAdModel(var _id: Int, var title: String, var address: String, var image: String, var prix: Double) {
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ import androidx.lifecycle.ViewModelProvider ...@@ -13,6 +13,7 @@ import androidx.lifecycle.ViewModelProvider
import com.example.elbuenopeso.databinding.FragmentMarketBinding import com.example.elbuenopeso.databinding.FragmentMarketBinding
import com.example.elbuenopeso.models.AdModel import com.example.elbuenopeso.models.AdModel
import com.example.elbuenopeso.R import com.example.elbuenopeso.R
import com.example.elbuenopeso.adapters.DbAdapter
import com.example.elbuenopeso.database.DBManager import com.example.elbuenopeso.database.DBManager
class MarketFragment : Fragment() { class MarketFragment : Fragment() {
...@@ -37,30 +38,30 @@ class MarketFragment : Fragment() { ...@@ -37,30 +38,30 @@ class MarketFragment : Fragment() {
val listView: ListView = binding.listView val listView: ListView = binding.listView
// seed market items // seed market items
var annonces: List<AdModel> = listOf<AdModel>( // var annonces: List<AdModel> = listOf<AdModel>(
AdModel("Poutre", "1 rue Jean-Pierre", "drawable/pichu.png", 25.0), // AdModel("Poutre", "1 rue Jean-Pierre", "drawable/pichu.png", 25.0),
AdModel("Briques", "2 rue Jean-Michel", "drawable/pichu.png", 16.0), // AdModel("Briques", "2 rue Jean-Michel", "drawable/pichu.png", 16.0),
AdModel("Mur en bois", "4 rue Alain-Juju", "drawable/pichu.png", 7.2), // AdModel("Mur en bois", "4 rue Alain-Juju", "drawable/pichu.png", 7.2),
AdModel("Maison de pierre", "7 rue Joris Belhomme", "drawable/pichu.png", 3.99), // AdModel("Maison de pierre", "7 rue Joris Belhomme", "drawable/pichu.png", 3.99),
AdModel("Téléphone de Timothé", "8 rue de Timothé", "drawable/pichu.png", 0.85), // AdModel("Téléphone de Timothé", "8 rue de Timothé", "drawable/pichu.png", 0.85),
AdModel("Oridnateur", "10 rue Jean-Charles", "drawable/pichu.png", 104.98), // AdModel("Oridnateur", "10 rue Jean-Charles", "drawable/pichu.png", 104.98),
AdModel("Charnières", "22 rue Jeanne-Marie", "drawable/pichu.png", 2648.97), // AdModel("Charnières", "22 rue Jeanne-Marie", "drawable/pichu.png", 2648.97),
AdModel("Porte en marbre", "1 rue Jean-Pierre", "drawable/pichu.png", 480.0), // AdModel("Porte en marbre", "1 rue Jean-Pierre", "drawable/pichu.png", 480.0),
AdModel("Cheminée", "33 rue du Gouvernement", "drawable/pichu.png", 800.0), // AdModel("Cheminée", "33 rue du Gouvernement", "drawable/pichu.png", 800.0),
) // )
//
var adAdapter: AdAdapter = AdAdapter(requireContext(), annonces) // val adAdapter: AdAdapter = AdAdapter(requireContext(), annonces)
listView.adapter = adAdapter // listView.adapter = adAdapter
/*marketViewModel.text.observe(viewLifecycleOwner) { /*marketViewModel.text.observe(viewLifecycleOwner) {
textView.text = it textView.text = it
}*/ }*/
val dbManager: DBManager? = DBManager.getDBManager(requireContext()) var dbManager: DBManager? = DBManager.getDBManager(requireContext());
dbManager?.open() dbManager = dbManager!!.open();
val cursor: Cursor? = dbManager?.fetch() val cursor = dbManager.fetch();
val adapter = DbAdapter(requireContext(), cursor!!, R.layout.fragment_market);
val cursorAdapter: CursorAdapter = AdAdapter(requireContext(), cursor, R.layout.fragment_market) adapter.notifyDataSetChanged();
listView.adapter = adapter;
return root return root
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment