我有一个小应用。 1个活动(MainActivity.kt)和两个片段(MainFragment&CreateNewJobFragment)。
Mainfragment有一个Recyclerview。 CreateNewJobFragment包含6个微调器和一个edittext。我正在尝试从微调器和edittext中进行选择,以在用户做出选择并单击“创建作业”按钮时填充RecyclerView。
问题是,当单击此按钮时,应用程序将返回到BackFragment到MainFragment,但是它没有通过新项目填充recyclerview,而是再次通过onCreate运行,并且没有任何反应。
MainFragment.kt
class MainFragment : Fragment() ,ItemClickedCustomListener{
override fun onCustomSpinnerItemSelected(selectedItems: JobData) {
Log.v("MainFragment","onCustomSpinnerItemSelected")
Log.v("MainFragment","selectedItems --> "
+ selectedItems.companyName + " "
+ selectedItems.location + " "
+ selectedItems.pumpTruck + " "
+ selectedItems.smartPiggers)
jobs.add(selectedItems)
adapter = recyclerView.adapter as JobAdapter
adapter.data(jobs)
}
var jobs = ArrayList<JobData>()
lateinit var adapter : JobAdapter
private lateinit var binding: FragmentMainBinding
lateinit var recyclerView : RecyclerView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
if (savedInstanceState == null) {
binding = FragmentMainBinding.inflate(inflater)
//getting recyclerview from xml and binding it
recyclerView = binding.jobRecyclerView
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
//Arraylist to store jobs using the data class JobData.
// TODO: Change this to a user created list from CreateNewJobFragment.kt
jobs = ArrayList()
Log.v("MainFragment", "onCreateView --> ")
//creating adapter
adapter = JobAdapter(jobs)
//add adapter to recyclerView
recyclerView.adapter = adapter
//Setting onClickListener for FAB(floating action button) using Navigation
binding.createNewJobFAB.setOnClickListener { v: View ->
v.findNavController().navigate(R.id.action_mainFragment_to_createNewJobFragment)
}
}
return binding.root
}
}
CreateNewJobFragment.kt
class CreateNewJobFragment : Fragment() {
private lateinit var binding: FragmentCreateNewJobBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentCreateNewJobBinding.inflate(inflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var addJobToRecyclerview = JobData("","","","")
//String array.
//TODO: Move this to res/strings
val companyNames = arrayOf("Company A", "Company B", "Company C", "Company D", "Company E")
var nameSpinner = binding.spinnerCustomerName
//Adapter for spinner
nameSpinner.adapter = ArrayAdapter(activity, android.R.layout.simple_spinner_dropdown_item, companyNames)
//item selected listener for spinner
nameSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(p0: AdapterView<*>?) {
TODO("not implemented yet")
}
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
//user selected spinner choice added
addJobToRecyclerview.companyName = companyNames[p2]
}
}
//String array.
//TODO: Move this to res/strings
val refineryTown = arrayOf("Long Beach", "Houston", "Cherry Point", "Wood River", "Bismark")
var townSpinner = binding.spinnerLocation
//Adapter for spinner
townSpinner.adapter = ArrayAdapter(activity, android.R.layout.simple_spinner_dropdown_item, refineryTown)
//item selected listener for spinner
townSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(p0: AdapterView<*>?) {
TODO("not implemented yet")
}
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
addJobToRecyclerview.location = refineryTown[p2]
}
}
<SNIP>Cutting out 5 repeated spinners and editText to save some space</SNIP>
//Setting onClickListener for 'Create Job' button using Navigation
binding.buttonCreateJob.setOnClickListener { v: View ->
(activity as MainActivity).itemClickedCustomListener.onCustomSpinnerItemSelected(addJobToRecyclerview)
Log.v("CreateNewJobFragment", "Job data added -->$addJobToRecyclerview")
Log.v("CreateNewJobFragment", "Create job button --> clicked")
v.findNavController().navigate(R.id.action_createNewJobFragment_to_mainFragment)
}
}
}
我使用以下Android文档作为指南:Implementing Navigation
特别是对于导航回MainFragment的部分:Tie destinations to UI widgets
单击“创建作业”按钮后。应用程序确实可以返回MainFragment。但是如上所述,Recyclerview中没有任何显示。
我有一些日志记录。这来自日志:
日志样本
2018-12-07 22:03:17.959 5806-5806 / com.palarran.pigtimer V / MainActivity:onCustomSpinnerItemSelected
2018-12-07 22:03:17.959 5806-5806 / com.palarran.pigtimer V / MainActivity:selectedItems-> Company A Long Beach Company 1 Company 4
2018-12-07 22:03:17.960 5806-5806 / com.palarran.pigtimer V / CreateNewJobFragment:添加的工作数据-> JobData(companyName = Company A,location = Long Beach,pumpTruck = Company 1,smartPiggers = Company 4 )
2018-12-07 22:03:17.960 5806-5806 / com.palarran.pigtimer V / CreateNewJobFragment:创建作业按钮->单击
2018-12-07 22:03:18.026 5806-5806 / com.palarran.pigtimer V / MainFragment:onCreateView->
2018-12-07 22:03:18.146 5806-5806 / com.palarran.pigtimer V / JobAdapter:getItemCount 0
2018-12-07 22:03:18.146 5806-5806 / com.palarran.pigtimer V / JobAdapter:getItemCount 0
如您所见,在“创建作业按钮->单击”的日志条目之后,它跳转到MainFragment中的“ onCreateView”,并且自定义适配器(JobAdapter.kt)中的项目数不会增加。
JobAdapter.kt
class JobAdapter(private var jobList: ArrayList<JobData>) : RecyclerView.Adapter<JobAdapter.ViewHolder>() {
//Returning view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JobAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.job_list_item, parent, false)
Log.v("JobAdapter","onCreateViewHolder")
return ViewHolder(v)
}
//Binding the data on the list
override fun onBindViewHolder(holder: JobAdapter.ViewHolder, position: Int) {
Log.v("JobAdapter","onBindViewHolder")
holder.bindItems(jobList[position])
}
override fun getItemCount(): Int {
Log.v("JobAdapter","getItemCount " + jobList.size)
return jobList.size
}
fun data(jobs: ArrayList<JobData>) {
Log.v("JobAdapter","DataSetChanged")
jobList = jobs
notifyDataSetChanged()
}
//Class holds the job list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(job: JobData) {
val textViewCompanyName = itemView.findViewById(R.id.tv_companyName) as TextView
val textViewLocation = itemView.findViewById(R.id.tv_job_location) as TextView
textViewCompanyName.text = job.companyName
Log.v("JobAdapter", "bindItems" + textViewCompanyName.text)
textViewLocation.text = job.location
Log.v("JobAdapter", "bindItems" + textViewLocation.text)
}
}
}
我是否在文档中缺少某些内容或阅读不正确?我肯定是误会了。
最佳答案
对此作出回应。
我能够弄清楚这一点。通过使用Sugar ORM数据库。在我的MainFragment中的onResume()中调用它。
在发布之前,我应该进一步研究这是一个愚蠢的问题。