第二次API调用未显示详细信息

时间:2019-05-24 18:50:29

标签: android kotlin android-recyclerview retrofit2

新的Android开发人员,对我轻松一点!我有2级API调用,无法正常工作。我的第一级(设备列表)工作正常,但是当我单击设备以查看详细信息时,什么也没有显示。在日志中,我完全看不到设备详细信息类,并且我不确定为什么。任何帮助表示赞赏。

在设备详细信息类中添加了日志记录,但是在单击时看不到它被调用。 当我从主列表中单击该设备时,我会看到API返回的详细信息,具体取决于我单击的是哪个设备,这样才能正常工作,但我无法获取它来显示结果:

D/MainActivity2: LOOK HERE!!! DeviceDetails(attributes=[Attribute(currentValue=present, dataType=ENUM, name=presence, values=[present, not present])], capabilities=null, commands=[arrived, departed], id=98, name=Presence - Matt)

MainActivity2

class MainActivity2 : AppCompatActivity() {
    private val tag : String = MainActivity2::class.java.simpleName
    private lateinit var adapterDetails: DeviceDetailsAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        rv_devices_details.layoutManager = LinearLayoutManager(this)
        rv_devices_details.hasFixedSize()
        adapterDetails = DeviceDetailsAdapter(listOf()) {deviceDetails -> deviceClicked(deviceDetails) }
        rv_devices_details.adapter = adapterDetails
        loadDeviceDetails()
    }

        private fun loadDeviceDetails() {
            GlobalScope.launch(Dispatchers.Main) {
                try {
                    var deviceID: String = intent.getStringExtra("deviceID")
                    val webResponseDetails = DeviceDetailsWebAccess.deviceDetailsApi.getDevicesDetailsAsync(deviceID = deviceID, access_token = "5dbbeea3-91bd-4dbc-983b-af8e2c18e18f").await()
                    if (webResponseDetails.isSuccessful) {
                        val deviceDetails: DeviceDetails? = webResponseDetails.body()
                        Log.d(tag, "LOOK HERE!!! $deviceDetails")
                        adapterDetails.notifyDataSetChanged()
                    } else {
                        Log.e(tag, "Error ${webResponseDetails.code()}")
                        Toast.makeText(this@MainActivity2, "Error ${webResponseDetails.code()}", Toast.LENGTH_LONG).show()
                    }
                } catch (e: IOException) {
                    Log.e(tag, "Exception " + e.printStackTrace())
                    Toast.makeText(this@MainActivity2, "Exception ${e.message}", Toast.LENGTH_LONG).show()
                }
            }
        }
    private fun deviceClicked(deviceDetails: DeviceDetails) {
        Toast.makeText(this, "Clicked: ${deviceDetails.name}", Toast.LENGTH_LONG).show()
        val showDetailActivityIntent = Intent(this, MainActivity2::class.java)
        showDetailActivityIntent.putExtra("deviceID", deviceDetails.id)
        startActivity(showDetailActivityIntent)
    }
    }

DeviceDetailsAdapter

class DeviceDetailsAdapter (private var deviceDetails: List<DeviceDetails>, private val clickDetailsListener: (DeviceDetails) -> Unit) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val viewDetails = inflater.inflate(R.layout.device_detail, parent, false)
        return DeviceDetailHolder(viewDetails)
    }

    override fun onBindViewHolder(holderDetails: RecyclerView.ViewHolder, position: Int) {
        (holderDetails as DeviceDetailHolder).bindDetails(deviceDetails[position], clickDetailsListener)
    }

    override fun getItemCount() = deviceDetails.size

    class DeviceDetailHolder(devicedetailView: View) : RecyclerView.ViewHolder(devicedetailView) {
        fun bindDetails(deviceDetails: DeviceDetails, clickDetailsListener: (DeviceDetails) -> Unit) {
            itemView.device_detail_name.text = deviceDetails.name
            Logger.getLogger(DeviceDetailsAdapter::class.java.name).warning("LOOK HERE NOW!!! $deviceDetails.name")
            itemView.device_detail_id.text = deviceDetails.id
            Logger.getLogger(DeviceDetailsAdapter::class.java.name).warning("LOOK HERE NOW!!! $deviceDetails.id")
        }

    }
}

Activitymain.xml

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.TestAPI.MainActivity2">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_devices_details"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

device_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.TestAPI.MainActivity2">

    <TextView
            android:id="@+id/device_detail_id"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"

            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Device ID" android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"/>
    <TextView
            android:id="@+id/device_detail_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"

            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            app:layout_constraintStart_toStartOf="parent"
            tools:text="Device Name" android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/device_detail_id"/>

</android.support.constraint.ConstraintLayout>

我感觉细节部分试图以某种方式将结果发布到第一个视图,或者不是以某种方式从API调用中获取数据。

0 个答案:

没有答案