Android Studio 如何在按下对话框中的按钮时执行Activity中的功能

y4ekin9u  于 2023-10-23  发布在  Android
关注(0)|答案(1)|浏览(148)

[Dialog.kt]

binding.addFriendBtn.setOnClickListener{}

[SearchPeopleActivity]

Thread{
    fun setFriend() {
        try {
            val userName = getName()
            val friendName = getFriendName()

            val jsonObject = JSONObject() //json 초기화
            jsonObject.put("user_name", userName)
            jsonObject.put("friend", friendName)
            Request().reqpost("http://dmumars.kro.kr/api/setfriend", jsonObject)

        } catch (e: UnknownServiceException) {
            println(e.message)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    setFriend()
}.start()

我想使用uuid查找我的应用程序用户。所以我使用SearchPeopleActivity和Dialog,当我找到另一个用户并单击对话框按钮向另一个用户发送好友请求时。
但是当我得到uuid并只在SerchPeopleActivity.kt中使用它时。所以我试图解决这个问题的意图,但我不能得到用户uuid和得到错误,我找不到用户。和ER_NO_REFERENCED_ROW_2错误.
enter image description here
我想使用这个线程时,我点击add_friend_btn在Android的对话框.kt
我的对话框.kt代码是

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.example.marsproject.databinding.ActivityFriendDialogBinding

class FriendDialog(id: String) : DialogFragment() {

    private lateinit var binding : ActivityFriendDialogBinding

    private var id: String? = null

    init {
        this.id = id
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = ActivityFriendDialogBinding.inflate(inflater, container, false)
        val view = binding.root

        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        binding.addFriendBtn.setOnClickListener{
        }

        binding.finishBtn.setOnClickListener{
            dismiss()
        }
        return view
    }
}

我的SearchPeopleActivity.kt代码在这里。

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import com.example.marsproject.databinding.ActivityFriendDialogBinding
import com.example.marsproject.databinding.ActivitySearchPeopleBinding
import com.kakao.sdk.user.UserApiClient
import org.json.JSONObject
import org.w3c.dom.Text
import java.net.UnknownServiceException
import kotlin.random.Random

@SuppressLint("MissingPermission")
class SearchPeopleActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySearchPeopleBinding
    private lateinit var bluetoothManager: BluetoothManager
    private lateinit var bluetoothsearch: BluetoothSearch

    private lateinit var statusTextView: TextView
    private lateinit var findUser: ImageView
    private lateinit var fadeInAnimation: Animation
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySearchPeopleBinding.inflate(layoutInflater)
        setContentView(binding.root)

    
        setSupportActionBar(binding.toolbar) 
        supportActionBar?.setDisplayHomeAsUpEnabled(true) 
        supportActionBar?.setHomeAsUpIndicator(R.drawable.icon_left_resize) 
        supportActionBar?.title = "Find Friend Around you" 

        bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
        bluetoothsearch = BluetoothSearch(bluetoothManager)

        statusTextView = binding.searchingText
        fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in)

        statusTextView.startAnimation(fadeInAnimation)

        statusTextView.visibility = TextView.VISIBLE



        if (!bluetoothsearch.bluetoothAdapter.isEnabled) {
           
            val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
            resultLaunch.launch(enableBtIntent) 
        } else {
           
            if (bluetoothsearch.startbluetoothSearch(bluetoothSearchCallback, 2)) {
            }
        }
    }

    
    val resultLaunch = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
       
        if (result.resultCode == -1) {
           
            if (bluetoothsearch.startbluetoothSearch(bluetoothSearchCallback, 2)) {
                statusTextView = binding.searchingText
                fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in)

                
                statusTextView.startAnimation(fadeInAnimation)
                statusTextView.visibility = TextView.VISIBLE

            }
        }
    }

    val bluetoothSearchCallback = object : ScanCallback() {
        private val isdup = ArrayList<String>() 

       
        override fun onScanResult(callbackType: Int, result: ScanResult?) {
            super.onScanResult(callbackType, result)

            /*val devicename = result?.device?.name
            val devicemac = result?.device?.address*/
            val deviceuuid = result?.scanRecord?.serviceUuids?.get(0)

            fun getFriendName(): String{
                var FriendName = ""
                try {
                   
                    val jsonObject =
                        Request().reqget("http://dmumars.kro.kr/api/getbtuserdata/${deviceuuid}")

                    
                    FriendName = jsonObject.getString("user_name")
                } catch (e: UnknownServiceException) {
                    Log.d("bluetooth", "this uuid user is not in our App")
                }
                return FriendName
            }

            Thread{
                runOnUiThread {
                    findUser = binding.friend1
                    findUser.startAnimation(fadeInAnimation) 

                    val screenWidth = 409
                    val screenHeight = 673

                    val randomX = (0..screenWidth).random()
                    val randomY = (0..screenHeight).random()

                    findUser.x = randomX.toFloat()
                    findUser.y = randomY.toFloat()

                    findUser.visibility = ImageView.VISIBLE

                    findUser.setOnClickListener{
                        UserApiClient.instance.me { user, error ->
                            if(user != null){

                                val dialog = FriendDialog("${user.id}")
                         
                                dialog.isCancelable = false
                                dialog.show(supportFragmentManager, "FriendDialog")
                            }
                        }
                    }
                }
            }

            val btnId = intent.getIntExtra("friend",R.id.add_friend_btn)
                if(btnId == R.id.add_friend_btn){
                    Thread{
                        fun setFriend() {
                            try {
                                val userName = getName()
                                val friendName = getFriendName()

                                val jsonObject = JSONObject() 
                                jsonObject.put("user_name", userName)
                                jsonObject.put("friend", friendName)
                                Request().reqpost("http://dmumars.kro.kr/api/setfriend", jsonObject)

                            } catch (e: UnknownServiceException) {
                                println(e.message)
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                        setFriend()
                    }.start()
                }
        }
    }

    fun getName(): String {
        val pref = getSharedPreferences("userName", 0)
        return pref.getString("name", "").toString()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item?.itemId) {
            android.R.id.home -> { 
                bluetoothsearch.stopbluetoothSearch(bluetoothSearchCallback)
                finish() 
            }
        }
        return super.onOptionsItemSelected(item)
    }
}
dphi5xsq

dphi5xsq1#

This可能不完全适合你的问题,但我认为你可以用它来解决你的问题。

相关问题