问题描述
我仍在做应用程序开发的第一步,所以我很感激对我的代码的任何反馈。 我的问题。我得到了两个片段,“ListFragment.kt”和“SetListFilters.kt”。我的目的是在 SetListFilters 中创建一个游标实例,然后当我按下“应用”按钮时,我在将游标实例发送到 ListFrament 的同时执行 popBackStack。据我了解,ListFragment 将调用 OnCreateView() 并且我想将新光标传递给我的适配器,以便对 Recyclerview 进行过滤。我尝试了很多东西,但没有任何效果对我有用。我希望你能帮助我。这是我的代码:
class SetListFilters(var transactionDB: sqliteDatabase,var adapter: TransactionAdapter):Fragment(R.layout.fragment_set_list_filters),DataPassListener {
private lateinit var gridView : GridView
private lateinit var btnYearFilter : Button
private lateinit var btnMonthFilter : Button
private lateinit var btnWeekFilter : Button
private lateinit var btnRevenueFilter : Button
private lateinit var btnExpeneseFilter : Button
private lateinit var btnNeutralFilter : Button
private lateinit var btnSbmtFilter : Button
private lateinit var tvShowAmountFilter : TextView
private lateinit var sbAmountFilters : SeekBar
private val viewmodel: Listviewmodel by activityviewmodels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_set_list_filters,container,false)
setUpViews(view)
setUpButtonOnclick()
setUpSeekbar()
val arrayAdapter: ArrayAdapter<CategoryEnum> = ArrayAdapter<CategoryEnum>(activity!!,android.R.layout.simple_list_item_multiple_choice,CategoryEnum.values())
gridView.setAdapter(arrayAdapter)
// When the user clicks on the GridItem
/*gridView.setonItemClickListener(OnItemClickListener { a,v,position,id ->
val o: Any = gridView.getItemAtPosition(position)
})*/
return view
}
override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
super.onViewCreated(view,savedInstanceState)
viewmodel.filters.observe(viewLifecycleOwner,Observer { set ->
// Update the selected filters UI
}
}
fun onFilterSelected(filter: Filter) = viewmodel.addFilter(filter)
fun onFilterdeselected(filter: Filter) = viewmodel.removeFilter(filter)
private fun upDateQueryForDB(){
}
private fun getAllItemsNeu(): Cursor {
val selArgs = arrayOf("r")
return transactionDB!!.query(
TransactionList.TransactionEntry.TABLE_NAME,null,"type = ?",selArgs,TransactionList.TransactionEntry.COLUMN_CREATEDAT + " DESC")
}
private fun setUpViews(view: View){
gridView = view.findViewById(R.id.gvCategoryFilter )
btnExpeneseFilter = view.findViewById(R.id.btnExpenseFilter)
btnMonthFilter = view.findViewById(R.id.btnMonthFilter)
btnWeekFilter = view.findViewById(R.id.btnWeekFilter)
btnNeutralFilter = view.findViewById(R.id.btnNeutralFilter)
btnRevenueFilter = view.findViewById(R.id.btnRevenueFilter)
btnSbmtFilter = view.findViewById(R.id.btnSbmtFilter)
btnYearFilter = view.findViewById(R.id.btnYearFilter)
sbAmountFilters = view.findViewById(R.id.seekBar)
tvShowAmountFilter = view.findViewById(R.id.tvFilteramount)
}
private fun setUpButtonOnclick(){
btnExpeneseFilter.setonClickListener { changeButtonAppearance(btnExpeneseFilter) }
btnMonthFilter.setonClickListener { changeButtonAppearance(btnMonthFilter) }
btnWeekFilter.setonClickListener { changeButtonAppearance(btnWeekFilter) }
btnNeutralFilter.setonClickListener { changeButtonAppearance(btnNeutralFilter) }
btnRevenueFilter.setonClickListener { changeButtonAppearance(btnRevenueFilter) }
btnYearFilter.setonClickListener { changeButtonAppearance(btnYearFilter) }
btnSbmtFilter.setonClickListener{
mainActivity.passDataCom(getAllItemsNeu())
activity!!.supportFragmentManager.popBackStack()
}
}
private fun changeButtonAppearance(button:Button) {
if(button.tag == "notpressed"){
button.setBackgroundResource(R.drawable.rounded_select_button_pressed)
button.setTextColor(Color.WHITE)
button.tag = "pressed"
}else{
button.setBackgroundResource(R.drawable.rounded_select_button)
button.setTextColor(ContextCompat.getColor(context!!,R.color.mainBlue))
button.tag ="notpressed"
}
}
private fun setUpSeekbar(){
sbAmountFilters.setonSeekBarchangelistener(object : SeekBar.OnSeekBarchangelistener {
override fun onProgressChanged(seekBar: SeekBar,i: Int,b: Boolean) {
// display the current progress of SeekBar
tvShowAmountFilter.text = "< $i"
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
// Do something
//Toast.makeText(context!!,"start tracking",Toast.LENGTH_SHORT).show()
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
// Do something
//Toast.makeText(context!!,"stop tracking",Toast.LENGTH_SHORT).show()
if(tvShowAmountFilter.text.toString()=="< 0"){
tvShowAmountFilter.text = "none"
}
}
})
}
override fun onAttach(context: Context) {
super.onAttach(context)
try{
mCallback = (OnImageClickListener) //folled the tutorial,dont kNow what to replace it with
}
}
}
class ListFragment:Fragment(R.layout.fragment_list) {
lateinit var transactionDB : sqliteDatabase
lateinit var adapter : TransactionAdapter
private lateinit var filterBtn : ImageButton
private lateinit var flActBtn : FloatingActionButton
var mCursorFilter : Cursor? = null
private val viewmodel: Listviewmodel by activityviewmodels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dbHelper = TransactionDBHelper(activity)
transactionDB = dbHelper.writableDatabase
adapter = TransactionAdapter(activity!!,getAllItems())
}
override fun onViewCreated(view: View,savedInstanceState)
viewmodel.filteredList.observe(viewLifecycleOwner,Observer { list ->
//Todo
}
}
private fun addRandomTransactionItem() {
var a = 1234.56
for (i in 0..10) {
if(i%2==0){
transactionDB.execsql("INSERT INTO transactionList (type,amount,category,createdAt) VALUES('r',${a},'K',CURRENT_TIMESTAMP)")
adapter.swapCursor(getAllItems())
}else if(i%5==0){
transactionDB.execsql("INSERT INTO transactionList (type,createdAt) VALUES('ne',CURRENT_TIMESTAMP)")
adapter.swapCursor(getAllItems())
}else if(i%6==0){
transactionDB.execsql("INSERT INTO transactionList (type,CURRENT_TIMESTAMP)")
adapter.swapCursor(getAllItems())
}else{
transactionDB.execsql("INSERT INTO transactionList (type,createdAt) VALUES('e',CURRENT_TIMESTAMP)")
adapter.swapCursor(getAllItems())
}
}
}
private fun getAllItems(): Cursor {
return transactionDB!!.query(
TransactionList.TransactionEntry.TABLE_NAME,TransactionList.TransactionEntry.COLUMN_CREATEDAT + " DESC")
}
override fun onCreateView(inflater: LayoutInflater,savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_list,false)
val rvRecyclerView = view.findViewById<RecyclerView>(R.id.rvRecyclerView)
rvRecyclerView.layoutManager = linearlayoutmanager(activity)
if(mCursorFilter!=null){
adapter.swapCursor(mCursorFilter!!)
}
rvRecyclerView.adapter = this.adapter
rvRecyclerView.addItemdecoration(MarginItemdecoration(10))
flActBtn = view.findViewById(R.id.floatingActionButton)
flActBtn.setonClickListener {
var ft:FragmentTransaction = activity!!.supportFragmentManager.beginTransaction()
ft.replace(R.id.flFrameLayout,AddItemFragment(transactionDB,adapter))
ft.addToBackStack(null)
ft.commit()
}
filterBtn=view.findViewById(R.id.ibFilterBtn)
filterBtn.setonClickListener {
var ft:FragmentTransaction = activity!!.supportFragmentManager.beginTransaction()
ft.replace(R.id.flFrameLayout,SetListFilters(transactionDB,adapter))
ft.addToBackStack(null)
ft.commit()
}
addRandomTransactionItem()
return view
}
}
class MainActivity() : AppCompatActivity() {
var mCursorFilters: Cursor? = null
private val viewmodel: Itemviewmodel by viewmodels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewmodel.selectedItem.observe(this,Observer { item ->
// Todo
})
val homeFragment = HomeFragment()
val listFragment = ListFragment()
val profileFragment = ProfileFragment()
setCurrentFragment(homeFragment)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
bottomNavigationView.setonNavigationItemSelectedListener {
var ft: FragmentTransaction = this.supportFragmentManager.beginTransaction()
when (it.itemId) {
R.id.pageHome -> ft.replace(R.id.flFrameLayout,homeFragment) // setCurrentFragment(homeFragment)
R.id.Pagelist -> ft.replace(R.id.flFrameLayout,listFragment) //setCurrentFragment(listFragment)
R.id.pageProfile -> ft.replace(R.id.flFrameLayout,profileFragment) //setCurrentFragment(profileFragment)
}
// ft.replace(R.id.flFrameLayout,AddItemFragment())
ft.addToBackStack(null)
ft.commit()
true
}
}
private fun setCurrentFragment(fragment: Fragment){
supportFragmentManager.beginTransaction().apply {
replace(R.id.flFrameLayout,fragment)
commit()
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)