如何从设置中获取数据并发送到此片段

Posted

技术标签:

【中文标题】如何从设置中获取数据并发送到此片段【英文标题】:how to do to get data from settings and send to this fragment 【发布时间】:2021-11-23 19:32:48 【问题描述】:

早上好,我想在 SettingsFragment 中打开一个窗口,可以选择一个 csv 文本文件,选择文件后,此数据将发送到 ChartFragment 并以图表的形式绘制数据。结构 MVVM。到目前为止,我做到了,但我不知道如何结束它,请帮助。这是我的代码

设置片段:

class SettingsFragment : PreferenceFragmentCompat() 
    private val SETTINGS_DEBUG = "SETTINGS_DEBUG"

    private val resolver by lazy  requireContext().contentResolver 

    private val myViewModel by viewModels<ChartViewModel>()
    
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) 
        setPreferencesFromResource(R.xml.root_preferences, rootKey)
        
    val myPref: Preference? = findPreference("load_csv_file") as Preference?
        myPref?.onPreferenceClickListener = Preference.OnPreferenceClickListener 
            try 
                val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
                intent.addCategory(Intent.CATEGORY_OPENABLE)
                intent.type = "text/*"
                startActivity(intent)
            catch (exc: Exception) 
                Log.d(SETTINGS_DEBUG, exc.message.toString())
            
            myViewModel.csvResultLiveData
            true
        
    

图表片段:

class ChartFragment : Fragment() 
    private var _binding: FragmentChartBinding? = null
    private val binding get() = _binding!!
    private val CHART_DEBUG = "CHART_DEBUG"

    private val lineChartVm: ChartViewModel by activityViewModels()
    
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View 
        _binding = FragmentChartBinding.inflate(inflater, container, false)

        Log.d(CHART_DEBUG, "transfer date successfully")

        lineChartVm.csvResultLiveData.observe(viewLifecycleOwner)  csvLines ->
            // do something with csvLines, a List<String>
                Log.d(CHART_DEBUG, "transfer date successful")

                val newEntries = csvLines.map  line -> toEntry(line) .groupingBy  entry -> entry.x 
                    .reduce  _, accumulator, element -> if (accumulator.y > element.y) accumulator else element .values
                val lineChart = binding.lineChart
                val vl = LineDataSet(newEntries.toList().take(4000), "cost")
                vl.setDrawValues(false)
                vl.setDrawFilled(true)
                vl.lineWidth = 1.5f
                vl.fillColor = R.color.gray
                vl.fillAlpha = R.color.red
                vl.setDrawCircles(false)
                lineChart.data = LineData(vl)
                lineChart.notifyDataSetChanged()
                lineChart.animateX(1800, Easing.EaseInExpo)
                lineChart.description.isEnabled = false
                lineChart.isHighlightPerDragEnabled = false
                lineChart.isScaleYEnabled = false
                lineChart.axisRight.isEnabled = false
        
        
        val markerView = CustomMarker(activity?.applicationContext, R.layout.marker_view)
        binding.lineChart.marker = markerView
        
        return binding.root
    
    private fun toEntry(line: String): Entry 
        val split = line.split(";")
        val time = split[1]
        // idx 01234 012345 l:5 lub 6
        // val 84504 165959
        // 0, 3 - 845
        val secondsStartIdx = time.length - 2
        val minutesStartIdx = time.length - 4
        val hoursStartIdx = (time.length - 6).coerceAtLeast(0)

        val hour = time.substring(hoursStartIdx, hoursStartIdx + time.length - 4)
        val minutest = time.substring(minutesStartIdx, minutesStartIdx + 2)
        val seconds = time.substring(secondsStartIdx, secondsStartIdx + 2)

        val newTime =
            hour.toFloat() * 10000 + (minutest.toFloat() * 100 / 60).toInt() * 100 + (seconds.toFloat() * 100 / 60).toInt()
        return Entry(newTime, split[2].toFloat())
    
    
    override fun onDestroy() 
        super.onDestroy()
        _binding = null
    

【问题讨论】:

【参考方案1】:

投票confent///post///materials =////fun

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于如何从设置中获取数据并发送到此片段的主要内容,如果未能解决你的问题,请参考以下文章

从 ListView 项目中获取数据并发送到另一个活动 (Android)

通过 express 从 mongo 获取数据,构建对象并发送到 React

想要在设备离线时将数据存储在本地数据库中,并在在线时从数据库中获取并发送到服务器

如何从片段中的 JSON 响应中的对象获取数据

如何在 Swift 中获取表格视图单元格数据的总和?

无法从片段中的 SQLite 获取数据