我在Activity_1上有一個Fragment_A ,每當在Fragment_A中單擊一個按鈕時,我都必須啟動Activity_2女巫有一個Fragment_B
所以我需要從Activity 1中的Fragment_A發送一個user_Id 到activity_2中的Fragment_B。有沒有辦法將資料直接從Activity_1中的Frament_A發送到Activity_2中的Fragment_B?
我是學生,所以我是初學者,我確實在這里找到了這個問題,但我不明白如何解決這個問題我確實找到了一些關于界面的解決方案,但我不知道該怎么做
我不斷得到class_id null
語言:科特林
類:類配接器
class ClassesAdapter(val c:Fragment,val l:android.content.Context? ,val classeslist: ArrayList<Classes>) : RecyclerView.Adapter <ClassesAdapter.ClassesViewHolder> (){
lateinit var auth: FirebaseAuth
lateinit var DataBase : DatabaseReference
lateinit var DataBase2 : DatabaseReference
lateinit var clipboardManager: ClipboardManager
private lateinit var mListener :onItemClickListener
interface onItemClickListener {
fun onItemClick(position :Int)
}
/*fun setOnItemClickListener(listener : onItemClickListener){
// mListener=listener
}*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClassesViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.class_items2,parent,false)
//return ClassesViewHolder(itemView,mListener)
return ClassesViewHolder(itemView,)
}
override fun onBindViewHolder(holder: ClassesViewHolder, position: Int) {
val currentitem = classeslist[position]
holder.classname.text = currentitem.name
holder.classrole.text = currentitem.role
holder.itemView.setOnClickListener {
val intent = Intent(l, DetailedClassActivity::class.java)
intent.putExtra("classId", currentitem.class_id)
l!!.startActivity(intent)
}
類:擁有片段的 Activity2 我想將其發送 class_id `
val intent=getIntent()
var classId= intent.getStringExtra("classId")
val bundle=Bundle()
bundle.putString("classId",classId)
val fragment=DocumentsFragment()
fragment.setArguments(bundle)
類:片段B
enter code here
btnAddcourse.setOnClickListener {
var nameofthecourse = Coursename.text.toString().trim()
var course_id :String?=DataBase3.push().key
var classId = arguments?.getString("classId")
val dateofthecourse: String = formatTimeStamp()
if (TextUtils.isEmpty(nameofthecourse)) {
// No name added
Coursename.error = "No name added !! Please enter the name of the class"
}
else{
courseList.add(Course(course_id,nameofthecourse,dateofthecourse,classId))
coursesAdapter.notifyDataSetChanged()
Toast.makeText(activity,"Adding Course Success", Toast.LENGTH_SHORT).show()
//Toast.makeText(activity,"class_id is null ", Toast.LENGTH_SHORT).show()
if (classId != null) {
addCourseToDataBase(course_id!!,nameofthecourse,dateofthecourse,classId)
}else{
Toast.makeText(activity,"class_id is null ", Toast.LENGTH_SHORT).show()
}
}
}
uj5u.com熱心網友回復:
片段A
button.setOnClickListener {
val intent = Intent(requireContext(), Activity2::class.java)
intent.putExtra("user_Id", user_Id)
startActivity(intent)
}
活動2
val intent = getIntent()
val message = intent.getStringExtra("user_Id")
val bundle = Bundle()
bundle.putString("user_Id", message) //the part i updated
val fragmet= fragment_B()
fragmet.setArguments(bundle)
片段B
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_B, container, false)
val bundle = this.arguments //the part i updated
val myString = bundle!!.getString("user_Id", "defaultValue") //the part i updated
}
uj5u.com熱心網友回復:
1-閱讀關于使用戶 id 在所有應用程式中全域化的單例設計模式
- 閱讀它以了解
2-共享首選項:保存用戶 ID 以供所有應用程式使用
- 共享偏好
3-您可以嘗試 Navigation Args:更好地選擇性能并可以將其用于通過您的應用程式移動
- 導航
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/465178.html
