У меня есть List
автомобилей, которые я пытаюсь показать. Необходимые данные получены от почтальона, и для доступа к информации об автомобиле мне нужно сначала получить доступ к списку, но я не уверен, как это сделать. Я также не уверен, как вызвать функцию автомобиля, которую я сделал, я искал пути, но не сработало.
вот как выглядят данные в почтальоне:
{
"listCar": [
{
"id": 204,
"adTitle": "AdTitlecar",
"adDate": "2019-11-07T11:52:40.0156875",
"adPrice": 25.0,
"category": "4X4",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "مرسيدس",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300.0,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "بنزين",
"carFeatureFuelId": 3,
"gearType": "اوتوماتك",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 203,
"adTitle": "AdTitlecar",
"adDate": "2019-11-07T11:52:37.7771547",
"adPrice": 25.0,
"category": "4X4",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "مرسيدس",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300.0,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "بنزين",
"carFeatureFuelId": 3,
"gearType": "اوتوماتك",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 202,
"adTitle": "AdTitlecar",
"adDate": "2019-11-07T11:52:35.5569602",
"adPrice": 25.0,
"category": "4X4",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "مرسيدس",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300.0,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "بنزين",
"carFeatureFuelId": 3,
"gearType": "اوتوماتك",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
],
}
Вот Мой автомобиль Объект, который я сделал:
object CarsListData {
var id = 0
var title = ""
var date = ""
var price = 0.0
var category = ""
var brand = ""
var model = ""
var distance = 0.0
var year = ""
var fuel = ""
var gear = ""
}
вот как я извлекаю данные как GET
:
fun carsList(context: Context, complete: (Boolean) -> Unit) {
val carList = object : JsonObjectRequest(Method.GET, URL_CAR_LIST, null, Response.Listener { response ->
try {
CarsListData.id = response.getInt("id")
CarsListData.title = response.getString("adTitle")
CarsListData.date = response.getString("adDate")
CarsListData.price = response.getDouble("adPrice")
CarsListData.category = response.getString("category")
CarsListData.brand = response.getString("brand")
CarsListData.model = response.getString("brandModel")
CarsListData.distance = response.getDouble("kilometer")
CarsListData.year = response.getString("modelYear")
CarsListData.fuel = response.getString("fuelType")
CarsListData.gear = response.getString("gearType")
// UserDataService.image = response.getString("image")
val userDataChange = Intent(BROADCAST_USER_DATA_CHANGE)
LocalBroadcastManager.getInstance(context).sendBroadcast(userDataChange)
complete(true)
} catch (e: JSONException) {
Log.d("JSON", "EXC" + e.localizedMessage)
}
}, Response.ErrorListener {error ->
Log.d("ERROR", "Could not login user: $error")
complete(false)
}) {
override fun getBodyContentType(): String {
return "application/json; charset=utf-8"
}
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers.put("Authorization", "Bearer $authToken")
return headers
}
}
Volley.newRequestQueue(context).add(carList)
}
вот как я транслирую данные в listCarPage:
LocalBroadcastManager.getInstance(this).registerReceiver(userDataChangeReciever,
IntentFilter(BROADCAST_USER_DATA_CHANGE)
)
}
private val userDataChangeReciever = object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
carDetailsDate.text = CarsListData.date
carDetailsTitle.text = CarsListData.title
carDetailsCategory.text = CarsListData.category
carDetailsPrice.text = CarsListData.price.toString()
carDetailsDistance.text = CarsListData.distance.toString()
cardetailsGear.text = CarsListData.gear
carDetailsOil.text = CarsListData.fuel
carDetailsYear.text = CarsListData.year
}
}
Я попытался получить доступ к «listCar», но ничего не работает, и я просто не чувствую, что я делаю это правильно, поэтому буду признателен за любые предложения!
Edit-
CarAdapter код:
class CarAdapter(context: Context, cars: List<Cars>) : BaseAdapter() {
val context = context
val cars = cars
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val carsView: View
carsView = LayoutInflater.from(context).inflate(R.layout.cars_list, null)
val carImage: ImageView = carsView.findViewById(R.id.carDetailsImage)
val carTitle: TextView = carsView.findViewById(R.id.carDetailsTitle)
val carCategory: TextView = carsView.findViewById(R.id.carDetailsCategory)
val carPrice: TextView = carsView.findViewById(R.id.carDetailsPrice)
val carDistance: TextView = carsView.findViewById(R.id.carDetailsDistance)
val carDate: TextView = carsView.findViewById(R.id.carDetailsDate)
val carGear: TextView = carsView.findViewById(R.id.cardetailsGear)
val carYear: TextView = carsView.findViewById(R.id.carDetailsYear)
val carOil: TextView = carsView.findViewById(R.id.carDetailsOil)
val car = cars[position]
carTitle.text = car.title
carCategory.text = car.category
carPrice.text = car.price
carDistance.text = car.distance
carDate.text = car.date
carGear.text = car.gear
carYear.text = car.year
carOil.text = car.oil
val resourceId = context.resources.getIdentifier(car.image, "drawable", context.packageName)
carImage.setImageResource(resourceId)
return carsView
}
override fun getItem(position: Int): Any {
return cars[position]
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getCount(): Int {
return cars.count()
}
}
вот список данных автомобилей:
class Cars(val date: String, val id: Int, val title: String, val image: String, val category: String, val price: String,val distance: String,val gear: String,val oil: String,val year: String) {
override fun toString(): String {
return title
}
}