Commit fa1d8dd1 authored by m-spi's avatar m-spi

Merge branch 'db_creation'

# Conflicts:
#	app/src/main/java/com/example/elbuenopeso/ui/market/MarketFragment.kt
#	app/src/main/res/layout/fragment_market.xml
parents 1286f639 e0c03ed1
...@@ -10,6 +10,7 @@ import androidx.navigation.findNavController ...@@ -10,6 +10,7 @@ import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import com.example.elbuenopeso.database.DBManager
import com.example.elbuenopeso.databinding.ActivityMainBinding import com.example.elbuenopeso.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
......
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)} €"
......
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
import android.widget.CursorAdapter
import android.widget.ImageView
import android.widget.TextView
import com.example.elbuenopeso.R
import com.example.elbuenopeso.database.DBHelper
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)
}
override fun bindView(view: View, context: Context, cursor: Cursor) {
val titleTextView: TextView = view.findViewById(R.id.itemListViewTitleView)
val addressTextView: TextView = view.findViewById(R.id.itemListViewTextView)
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 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))
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 !!!
}
}
\ No newline at end of file
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 = "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
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)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME")
onCreate(db)
}
fun getById(id: Int): AdModel? {
val db = this.writableDatabase
val query = "SELECT * FROM $TABLE_NAME where $_ID=?"
val data = db.rawQuery(query, arrayOf(id.toString()))
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))
data.close()
AdModel(title, address, image, prix)
} else {
data.close()
null
}
}
}
package com.example.elbuenopeso.database
import android.content.ContentValues
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
class DBManager //init(); // Useful for adding ads for the first time.
private constructor(private val context: Context) {
private var dbHelper: DBHelper? = null
private var database: SQLiteDatabase? = null
@Throws(SQLException::class)
fun open(): DBManager {
this.dbHelper = DBHelper(this.context)
this.database = this.dbHelper!!.writableDatabase
return this
}
fun close() {
this.dbHelper?.close()
}
// Add ads manually.
fun init() {
this.open()
this.insert(
AdModel("Poutre", "1 rue Jean-Pierre", "drawable/pichu.png", 25.0)
)
this.insert(
AdModel("Briques", "2 rue Jean-Michel", "drawable/pichu.png", 16.0)
)
this.insert(
AdModel("Maison de pierre", "7 rue Joris Belhomme", "drawable/pichu.png", 3.99)
)
this.insert(
AdModel("Mur en bois", "4 rue Alain-Juju", "drawable/pichu.png", 7.2)
)
this.insert(
AdModel("Téléphone de Timothé", "8 rue de Timothé", "drawable/pichu.png", 0.85)
)
this.insert(
AdModel("Oridnateur", "10 rue Jean-Charles", "drawable/pichu.png", 104.98)
)
this.insert(
AdModel("Charnières", "22 rue Jeanne-Marie", "drawable/pichu.png", 2648.97)
)
this.insert(
AdModel("Porte en marbre", "1 rue Jean-Pierre", "drawable/pichu.png", 480.0)
)
this.insert(
AdModel("Cheminée", "33 rue du Gouvernement", "drawable/pichu.png", 800.0)
)
}
fun insert(ad: AdModel) {
val contentValue = ContentValues()
contentValue.put(DBHelper.TITLE, ad.title)
contentValue.put(DBHelper.ADDRESS, ad.address)
contentValue.put(DBHelper.IMAGE, ad.image)
contentValue.put(DBHelper.PRIX, ad.prix)
this.database!!.insert(DBHelper.TABLE_NAME, null, contentValue)
}
fun fetch(): Cursor? {
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
}
fun update(_id: Long, ad: AdModel): Int {
val contentValues = ContentValues()
contentValues.put(DBHelper.TITLE, ad.title)
contentValues.put(DBHelper.ADDRESS, ad.address)
contentValues.put(DBHelper.IMAGE, ad.image)
return this.database!!.update(
DBHelper.TABLE_NAME,
contentValues,
DBHelper._ID + " = " + _id,
null
)
}
fun delete(_id: Long) {
this.database!!.delete(DBHelper.TABLE_NAME, DBHelper._ID + "=" + _id, null)
}
fun getById(id: Int): AdModel? {
return this.dbHelper?.getById(id)
}
companion object {
var DBManager: DBManager? = null
// normalement pas de fuite, puisque 1 seul contexte
fun getDBManager(context: Context): DBManager? {
return if (this.DBManager == null) {
DBManager(context)
} else this.DBManager
}
}
}
package com.example.elbuenopeso.models package com.example.elbuenopeso.models
public class AdModel(var title: String, var address: String, var image: Int, var prix: Double) { public class AdModel(var title: String, var address: String, var image: String, var prix: Double) {
} }
\ No newline at end of file
package com.example.elbuenopeso.models
class DbAdModel(var _id: Int, var title: String, var address: String, var image: String, var prix: Double) {
}
\ No newline at end of file
package com.example.elbuenopeso.ui.market package com.example.elbuenopeso.ui.market
import AdAdapter import AdAdapter
import android.database.Cursor
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.GridView import android.widget.GridView
import android.widget.CursorAdapter
import android.widget.ListView import android.widget.ListView
import android.widget.ToggleButton import android.widget.ToggleButton
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
...@@ -13,10 +16,15 @@ import androidx.lifecycle.ViewModelProvider ...@@ -13,10 +16,15 @@ 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
class MarketFragment : Fragment() { class MarketFragment : Fragment() {
private var _binding: FragmentMarketBinding? = null private var _binding: FragmentMarketBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!! private val binding get() = _binding!!
private lateinit var listView: ListView private lateinit var listView: ListView
...@@ -31,25 +39,18 @@ class MarketFragment : Fragment() { ...@@ -31,25 +39,18 @@ class MarketFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
val marketViewModel =
ViewModelProvider(this)[MarketViewModel::class.java]
_binding = FragmentMarketBinding.inflate(inflater, container, false) _binding = FragmentMarketBinding.inflate(inflater, container, false)
val root: View = binding.root val root: View = binding.root
listView = binding.listView listView = binding.listView
gridView = binding.gridView gridView = binding.gridView
toggleButton = binding.toggleButton toggleButton = binding.toggleButton
val annonces: List<AdModel> = listOf( val listView: ListView = binding.listView
AdModel("Poutre", "1 rue Jean-Pierre", R.drawable.pichu, 25.0), // seed market items
AdModel("Briques", "2 rue Jean-Michel", R.drawable.pichu, 16.0), var annonces: List<AdModel> = listOf<AdModel>(
AdModel("Poutre", "1 rue Jean-Pierre", R.drawable.pichu, 25.0),
AdModel("Briques", "2 rue Jean-Michel", R.drawable.pichu, 16.0),
AdModel("Mur en bois", "4 rue Alain-Juju", R.drawable.pichu, 7.2),
AdModel("Maison de pierre", "7 rue Joris Belhomme", R.drawable.pichu, 3.99),
AdModel("Téléphone de Timothé", "8 rue de Timothé", R.drawable.pichu, 0.85),
AdModel("Oridnateur", "10 rue Jean-Charles", R.drawable.pichu, 104.98),
AdModel("Charnières", "22 rue Jeanne-Marie", R.drawable.pichu, 2648.97),
AdModel("Porte en marbre", "1 rue Jean-Pierre", R.drawable.pichu, 480.0),
AdModel("Cheminée", "33 rue du Gouvernement", R.drawable.pichu, 800.0),
AdModel("Poutre", "1 rue Jean-Pierre", R.drawable.pichu, 25.0), AdModel("Poutre", "1 rue Jean-Pierre", R.drawable.pichu, 25.0),
AdModel("Briques", "2 rue Jean-Michel", R.drawable.pichu, 16.0), AdModel("Briques", "2 rue Jean-Michel", R.drawable.pichu, 16.0),
AdModel("Mur en bois", "4 rue Alain-Juju", R.drawable.pichu, 7.2), AdModel("Mur en bois", "4 rue Alain-Juju", R.drawable.pichu, 7.2),
...@@ -59,17 +60,20 @@ class MarketFragment : Fragment() { ...@@ -59,17 +60,20 @@ class MarketFragment : Fragment() {
AdModel("Charnières", "22 rue Jeanne-Marie", R.drawable.pichu, 2648.97), AdModel("Charnières", "22 rue Jeanne-Marie", R.drawable.pichu, 2648.97),
AdModel("Porte en marbre", "1 rue Jean-Pierre", R.drawable.pichu, 480.0), AdModel("Porte en marbre", "1 rue Jean-Pierre", R.drawable.pichu, 480.0),
AdModel("Cheminée", "33 rue du Gouvernement", R.drawable.pichu, 800.0), AdModel("Cheminée", "33 rue du Gouvernement", R.drawable.pichu, 800.0),
// Add more AdModel items as needed
) )
adAdapter = AdAdapter(requireContext(), annonces)
listView.adapter = adAdapter
toggleButton.setOnCheckedChangeListener { _, isChecked -> toggleButton.setOnCheckedChangeListener { _, isChecked ->
isGridViewEnabled = isChecked isGridViewEnabled = isChecked
updateView() updateView()
} }
val dbManager: DBManager? = DBManager.getDBManager(requireContext())
dbManager!!.init()
val cursor = dbManager.fetch()
Log.i(null, cursor?.getColumnName(0).toString())
val adapter = DbAdapter(requireContext(), cursor!!, R.layout.item_listview_ad)
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