Android fragments 将值从一个片段传递到另一个片段
我在Kotlin工作,正在使用改装。我有一个由卡片组成的回收者视图。每个卡都有一个类型名和一个与之关联的图像,现在我已经使用dataclass静态地添加了它。现在,我只需要在用户单击特定卡后将typeName从CategoryFragment传递到LocationFragment。在LocationFragment,我想从数据库中检查纬度、经度和typeName,如果它已经存在的话 这是我的分类数据类代码: 数据类类别\数据类(val类别\图像:Int,val类型名称:String) 这是我的分类片段代码:Android fragments 将值从一个片段传递到另一个片段,android-fragments,kotlin,retrofit,Android Fragments,Kotlin,Retrofit,我在Kotlin工作,正在使用改装。我有一个由卡片组成的回收者视图。每个卡都有一个类型名和一个与之关联的图像,现在我已经使用dataclass静态地添加了它。现在,我只需要在用户单击特定卡后将typeName从CategoryFragment传递到LocationFragment。在LocationFragment,我想从数据库中检查纬度、经度和typeName,如果它已经存在的话 这是我的分类数据类代码: 数据类类别\数据类(val类别\图像:Int,val类型名称:String) 这是我的分
package com.example.atry.MakeComplaint
import android.content.Context
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.atry.History.Complaints
import com.example.atry.History.MyComplainRecyclerViewAdapter
import com.example.atry.R
import kotlinx.android.synthetic.main.existing_complaint_popup.*
class CategoryFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val v = inflater.inflate(R.layout.fragment_category, container, false)
val recyclerView = v.findViewById(R.id.category_list) as RecyclerView
recyclerView.apply {
layoutManager = GridLayoutManager(context!!, 2)
}
//creating an arraylist to store category using the data class user
val category = ArrayList<Category_dataclass>()
//adding some dummy data to the list of categories
category.add(Category_dataclass((R.drawable.trash) , "Water"))
category.add(Category_dataclass((R.drawable.fire) , "Sewerage"))
category.add(Category_dataclass((R.drawable.dustbin) , "load"))
//creating our adapter
val adapter = CategoryRecyclerViewAdapter(category)
//now adding the adapter to recyclerview
recyclerView.adapter = adapter
// Inflate the layout for this fragment
return v
}
}
package com.example.atry.MakeComplaint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.atry.History.ComplaintDetailsFragment
import com.example.atry.History.Complaints
import com.example.atry.MakeComplaint.Category_dataclass
import com.example.atry.MakeComplaint.CategoryRecyclerViewAdapter
import com.example.atry.R
import java.util.*
import kotlin.collections.ArrayList
class CategoryRecyclerViewAdapter(val categoryList: ArrayList<Category_dataclass>) : RecyclerView.Adapter<CategoryRecyclerViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryRecyclerViewAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.fragment_category_single, parent, false)
return ViewHolder(v)
}
//this method is binding the data on the list
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItems(categoryList[position])
holder.mView.setOnClickListener{view->
val lFragment = LocationFragment()
val oldFragment = CategoryFragment()
val manager = (holder.mView.context as AppCompatActivity).supportFragmentManager
val transaction = manager.beginTransaction()
transaction.replace(
R.id.first_screen_of_makecomplaint,
lFragment
) // give your fragment container id in first parameter
transaction.show(lFragment)
transaction.hide(oldFragment)
transaction.isAddToBackStackAllowed
transaction.addToBackStack(oldFragment.fragmentManager.toString()) // if written, this transaction will be added to backstack
transaction.commit()
}
}
//this method is giving the size of the list
override fun getItemCount(): Int {
return categoryList.size
}
inner class ViewHolder(val mView : View) : RecyclerView.ViewHolder(mView) {
fun bindItems(cat:Category_dataclass) {
val imageViewName = mView.findViewById(R.id.category_image) as ImageView
val textViewtext = mView.findViewById(R.id.category_text) as TextView
imageViewName.setImageResource(cat.category_image)
textViewtext.text = cat.typeName
}
}
}
//to check existing complaint
@GET("api/existingComplain")
@FormUrlEncoded
fun checkExistingComplain(@Query("typeName") typeName:String,
@Query("longitude") longitude:String,
@Query("latitude") latitude:String):Observable<Observables.checkExistingResult>
data class checkExistingResult(val description:String , val Complain:String)
如果数据库中已经存在类型名和位置,我希望在“材质样式”对话框中显示描述和位置。您可以尝试以下操作:
第一步:-
val dashboard = "Dashboard"
像这样获取变量您可以将它的名称作为您的片段名称,这样您就可以像这样检查页面名称MainActivity()。dashboard
现在,第二步:-
创建一个类并在该类中编写如下函数
fun openPage(
activity: Activity,
pagename: String?, model: PaymentDetails
) {
when (pagename) {
MainActivity().dashboard -> {
val mfragment = DashboardFragment()
(activity as MainActivity).fragmentManager.beginTransaction()
.replace(R.id.nav_host, mfragment).commit()
val a = Bundle()
a.putSerializable("model", model)
mfragment.arguments = a
}
}
}
取键值对,就像我取整个模型一样,键是模型,在我的代码中,你可以写任何东西
第三步:-
使您的模型可序列化
val dashboard = "Dashboard"
第四步:-当您将值添加到模型中时,请添加详细信息,并为要使用这些值的方法添加模型,如下所示
val payment = PaymentDetails(
type,
price,
name,
id,
)
paymentData!!.add(payment)
CustomMethods().openPagePaymentDetails(
activity!!,
MainActivity().sessionPaymentFragment, payment
)
第五步:-
使用模型类型声明并初始化对象,如下所示:
//Declare
private var model: PaymentDetails? = null
//initialise
model = arguments!!.getSerializable("model") as PaymentDetails?
现在你们终于可以像这样把上一个片段的值访问到下一个片段中
var type: String
type = model!!.type
注意:-请不要忘记使模型类可序列化
希望它能帮助你