Commit fd794904 authored by Alutulu's avatar Alutulu

draft db

parent 07e62214
......@@ -9,6 +9,7 @@ import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.elbuenopeso.database.DBManager
import com.example.elbuenopeso.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
......
package com.example.elbuenopeso.adapters
import android.content.Context
import android.database.Cursor
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
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);
}
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.toString()))
val image: String = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.IMAGE))
titleTextView.setText(title)
addressTextView.setText(address)
prixView.setText(prix.toString())
//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 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
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);"
}
override fun onCreate(db: SQLiteDatabase) {
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()))
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()))
data.close()
return AdModel(title, address, image, prix)
} else {
data.close()
return 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 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 {
dbHelper = DBHelper(context)
database = dbHelper!!.writableDatabase
return this
}
fun close() {
dbHelper?.close()
}
// Add ads manually.
fun init() {
open()
insert(
AdModel(
"Wood",
"Douai",
"https://media.istockphoto.com/id/134253640/photo/construction-of-a-wooden-roof-frame-underway.jpg?s=612x612&w=0&k=20&c=e5gUkic9LGQWahIdHozOsEzHKy_HtsmvmtOHmYsejSU=",
25.0
)
)
insert(
AdModel(
"Steel",
"Lille",
"https://as2.ftcdn.net/v2/jpg/03/91/83/87/1000_F_391838708_4HFADW5beay2VVlnoual6Qi5fWeIaD9V.jpg",25.0
)
)
insert(
AdModel(
"Clay",
"Douai",
"https://constrofacilitator.com/wp-content/uploads/2020/02/clay-in-construction.jpg",25.0
)
)
insert(
AdModel(
"Metal",
"Lyon",
"https://www.meto-constructions.fr/wp-content/uploads/2018/12/IMG_6067.jpg",25.0
)
)
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(
AdModel(
"Wood",
"Orchies",
"https://yieldpro.com/wp-content/uploads/2020/08/lumber1.jpg",25.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)
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)
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 database!!.update(
DBHelper.TABLE_NAME,
contentValues,
DBHelper._ID + " = " + _id,
null
)
}
fun delete(_id: Long) {
database!!.delete(DBHelper.TABLE_NAME, DBHelper._ID + "=" + _id, null)
}
fun getById(id: Int): AdModel? {
return dbHelper?.getById(id)
}
companion object {
var DBManager: DBManager? = null
// normalement pas de fuite, puisque 1 seule activité
fun getDBManager(context: Context): DBManager? {
return if (DBManager == null) {
DBManager(context)
} else DBManager
}
}
}
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
import AdAdapter
import android.database.Cursor
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CursorAdapter
import android.widget.ListView
import androidx.fragment.app.Fragment
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.database.DBManager
class MarketFragment : Fragment() {
......@@ -25,6 +28,7 @@ class MarketFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val marketViewModel =
ViewModelProvider(this)[MarketViewModel::class.java]
......@@ -34,15 +38,15 @@ class MarketFragment : Fragment() {
val listView: ListView = binding.listView
// seed market items
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", "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)
......@@ -51,6 +55,12 @@ class MarketFragment : Fragment() {
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)
return root
}
......
......@@ -8,8 +8,9 @@
<ListView
android:id="@+id/listView"
android:layout_width="409dp"
android:layout_height="681dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
......
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