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

rien qui marche

parent fd794904
import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -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)
override fun getView(i: Int, view: View?, viewGroup: ViewGroup): View {
var convertView = view
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 titleTV: TextView = convertView.findViewById(R.id.itemListViewTitleView)
val addressTV: TextView = convertView.findViewById(R.id.itemListViewTextView)
val prixTV: TextView = convertView.findViewById(R.id.itemListViewPrixView)
imageIV.setImageResource(ad.image)
imageIV.setImageURI(Uri.parse(ad.image))
titleTV.text = ad.title
addressTV.text = ad.address
prixTV.text = "${usingJavaStringFormat(ad.prix, 2)} €"
......
......@@ -2,6 +2,7 @@ package com.example.elbuenopeso.adapters
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -10,12 +11,11 @@ import android.widget.ImageView
import android.widget.TextView
import com.example.elbuenopeso.R
import com.example.elbuenopeso.database.DBHelper
import
class DbAdapter(context: Context, c: Cursor, layout: Int): CursorAdapter(context, c) {
val item_layout = layout
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) {
......@@ -24,16 +24,17 @@ class DbAdapter(context: Context, c: Cursor, layout: Int): CursorAdapter(context
val imageView: ImageView = view.findViewById(R.id.itemListViewImageView)
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 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))
titleTextView.setText(title)
addressTextView.setText(address)
prixView.setText(prix.toString())
imageView.setImageURI(Uri.parse(image))
//with(Glide)
//TODO: trouver une alternative !!!
}
......
......@@ -3,23 +3,25 @@ package com.example.elbuenopeso.database
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.util.Log
import com.example.elbuenopeso.models.AdModel
class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
companion object {
const val TABLE_NAME = "publication_table"
const val _ID = "unID"
const val TITLE = "Mur en briques"
const val ADDRESS = "5 Rue de Monsieur Spi"
const val IMAGE = "theURLblablabla"
const val PRIX = 25.00
const val TABLE_NAME = "product"
const val _ID = "_id"
const val TITLE = "title"
const val ADDRESS = "address"
const val IMAGE = "image_url"
const val PRIX = "price"
private const val DB_NAME = "LEBONCOIN.DB"
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) {
Log.i(null, "table sql create : $CREATE_TABLE")
db.execSQL(CREATE_TABLE)
}
......@@ -32,16 +34,17 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
val db = this.writableDatabase
val query = "SELECT * FROM $TABLE_NAME where $_ID=?"
val data = db.rawQuery(query, arrayOf(id.toString()))
if (data.moveToFirst()) {
return if (data.moveToFirst()) {
val title = data.getString(data.getColumnIndexOrThrow(TITLE))
val address = data.getString(data.getColumnIndexOrThrow(ADDRESS))
val image = data.getString(data.getColumnIndexOrThrow(IMAGE))
val prix = data.getDouble(data.getColumnIndexOrThrow(PRIX.toString()))
val prix = data.getDouble(data.getColumnIndexOrThrow(PRIX))
data.close()
return AdModel(title, address, image, prix)
AdModel(title, address, image, prix)
} else {
data.close()
return null
null
}
}
}
......
......@@ -5,6 +5,7 @@ import android.content.Context
import android.database.Cursor
import android.database.SQLException
import android.database.sqlite.SQLiteDatabase
import android.util.Log
import com.example.elbuenopeso.models.AdModel
......@@ -14,19 +15,19 @@ private constructor(private val context: Context) {
private var database: SQLiteDatabase? = null
@Throws(SQLException::class)
fun open(): DBManager {
dbHelper = DBHelper(context)
database = dbHelper!!.writableDatabase
this.dbHelper = DBHelper(this.context)
this.database = this.dbHelper!!.writableDatabase
return this
}
fun close() {
dbHelper?.close()
this.dbHelper?.close()
}
// Add ads manually.
fun init() {
open()
insert(
this.open()
this.insert(
AdModel(
"Wood",
"Douai",
......@@ -34,35 +35,35 @@ private constructor(private val context: Context) {
25.0
)
)
insert(
this.insert(
AdModel(
"Steel",
"Lille",
"https://as2.ftcdn.net/v2/jpg/03/91/83/87/1000_F_391838708_4HFADW5beay2VVlnoual6Qi5fWeIaD9V.jpg",25.0
)
)
insert(
this.insert(
AdModel(
"Clay",
"Douai",
"https://constrofacilitator.com/wp-content/uploads/2020/02/clay-in-construction.jpg",25.0
)
)
insert(
this.insert(
AdModel(
"Metal",
"Lyon",
"https://www.meto-constructions.fr/wp-content/uploads/2018/12/IMG_6067.jpg",25.0
)
)
insert(
this.insert(
AdModel(
"Glass",
"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
)
)
insert(
this.insert(
AdModel(
"Wood",
"Orchies",
......@@ -76,13 +77,14 @@ private constructor(private val context: Context) {
contentValue.put(DBHelper.TITLE, ad.title)
contentValue.put(DBHelper.ADDRESS, ad.address)
contentValue.put(DBHelper.IMAGE, ad.image)
database!!.insert(DBHelper.TABLE_NAME, null, contentValue)
this.database!!.insert(DBHelper.TABLE_NAME, null, contentValue)
}
fun fetch(): Cursor? {
val columns =
arrayOf<String>(DBHelper._ID, DBHelper.TITLE, DBHelper.ADDRESS, DBHelper.IMAGE, DBHelper.PRIX.toString())
val cursor = database!!.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null)
val columns: Array<String> =
arrayOf(DBHelper._ID, DBHelper.TITLE, DBHelper.ADDRESS, DBHelper.IMAGE, DBHelper.PRIX)
columns.forEach { e -> Log.i(null, e) }
val cursor = this.database!!.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null)
cursor?.moveToFirst()
return cursor
}
......@@ -92,7 +94,7 @@ private constructor(private val context: Context) {
contentValues.put(DBHelper.TITLE, ad.title)
contentValues.put(DBHelper.ADDRESS, ad.address)
contentValues.put(DBHelper.IMAGE, ad.image)
return database!!.update(
return this.database!!.update(
DBHelper.TABLE_NAME,
contentValues,
DBHelper._ID + " = " + _id,
......@@ -101,20 +103,20 @@ private constructor(private val context: Context) {
}
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? {
return dbHelper?.getById(id)
return this.dbHelper?.getById(id)
}
companion object {
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? {
return if (DBManager == null) {
return if (this.DBManager == null) {
DBManager(context)
} else DBManager
} else this.DBManager
}
}
}
......
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
import com.example.elbuenopeso.databinding.FragmentMarketBinding
import com.example.elbuenopeso.models.AdModel
import com.example.elbuenopeso.R
import com.example.elbuenopeso.adapters.DbAdapter
import com.example.elbuenopeso.database.DBManager
class MarketFragment : Fragment() {
......@@ -37,30 +38,30 @@ class MarketFragment : Fragment() {
val listView: ListView = binding.listView
// seed market items
var annonces: List<AdModel> = listOf<AdModel>(
AdModel("Poutre", "1 rue Jean-Pierre", "drawable/pichu.png", 25.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("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("Oridnateur", "10 rue Jean-Charles", "drawable/pichu.png", 104.98),
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("Cheminée", "33 rue du Gouvernement", "drawable/pichu.png", 800.0),
)
var adAdapter: AdAdapter = AdAdapter(requireContext(), annonces)
listView.adapter = adAdapter
// var annonces: List<AdModel> = listOf<AdModel>(
// AdModel("Poutre", "1 rue Jean-Pierre", "drawable/pichu.png", 25.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("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("Oridnateur", "10 rue Jean-Charles", "drawable/pichu.png", 104.98),
// 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("Cheminée", "33 rue du Gouvernement", "drawable/pichu.png", 800.0),
// )
//
// val adAdapter: AdAdapter = AdAdapter(requireContext(), annonces)
// listView.adapter = adAdapter
/*marketViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}*/
val dbManager: DBManager? = DBManager.getDBManager(requireContext())
dbManager?.open()
val cursor: Cursor? = dbManager?.fetch()
val cursorAdapter: CursorAdapter = AdAdapter(requireContext(), cursor, R.layout.fragment_market)
var dbManager: DBManager? = DBManager.getDBManager(requireContext());
dbManager = dbManager!!.open();
val cursor = dbManager.fetch();
val adapter = DbAdapter(requireContext(), cursor!!, R.layout.fragment_market);
adapter.notifyDataSetChanged();
listView.adapter = adapter;
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