gpt4 book ai didi

android - 如何使用 kotlin android 更新我的 Recyclerview?

转载 作者:行者123 更新时间:2023-12-02 12:29:45 28 4
gpt4 key购买 nike

我有一个显示数据的 Recyclerview Activity 。现在我想更新我的RecyclerView一旦获得新数据。现在每次我关闭并重新打开我的应用程序时,都会显示新数据。但我希望它不关闭更新我的观点。

我试过这个,

但什么都行不通,

fun setupViewPager(viewPager: ViewPager, it: List<TransactionEntity>, incoming: TransactionAdapterDirection, mainActivity: MainActivity) {

val cc: Context = mainActivity.applicationContext

if(adapter.count < 2) {
if (incoming.equals(OUTGOING)) {
val gson = Gson()
val gson1 = GsonBuilder().create()
val model = it
val IT = gson.toJson(model)

val pref = cc.applicationContext.getSharedPreferences("MyPrefSend", 0)
val editor = pref.edit()
editor.putString("NEWIT_SEND", IT)
editor.apply()

adapter.addFragment(SendingFragment(),"SEND")
adapter.notifyDataSetChanged()
} else if (incoming.equals(INCOMING)) {
val gson = Gson()
val gson1 = GsonBuilder().create()
val model = it
val IT = gson.toJson(model)

val pref = cc.applicationContext.getSharedPreferences("MyPrefRec", 0)
val editor = pref.edit()
editor.putString("NEWIT_REC", IT)
editor.apply()
adapter.addFragment(ReceiveFragment(), "RECEIVE")
adapter.notifyDataSetChanged()
}
viewPager.adapter = adapter
}
}


class ViewPagerAdapter(manager: FragmentManager) : FragmentStatePagerAdapter(manager)
{
private val mFragmentList: ArrayList<Fragment> = ArrayList<Fragment>()
private val mFragmentTitleList: ArrayList<String> = ArrayList<String>()

override fun getCount(): Int {
return mFragmentList.size
}


override fun getItem(position: Int): Fragment? {
var fragment: Fragment? = null
if (position == 0) {
fragment = SendingFragment()
} else if (position == 1) {
fragment = ReceiveFragment()
}
return fragment
}


fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}

override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}

override fun getItemPosition(fragItem: Any): Int {
var position = 0
if (fragItem is ReceiveFragment) {
position = 0
} else if (fragItem is SendingFragment) {
position = 1
}
return if (position >= 0) position else PagerAdapter.POSITION_NONE
}
}

**Framgnet.kt**





class ReceiveFragment: Fragment()
{

private var linearLayoutManager: LinearLayoutManager? = null

fun fromJson(jsonString: String, type: Type): Any {
return Gson().fromJson(jsonString, type)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

val pref = context!!.getSharedPreferences("MyPrefRec", 0)
val mFragIT = pref.getString("NEWIT_REC", "")

val NewIT = fromJson(mFragIT,
object : TypeToken<List<TransactionEntity>>() {

}.type) as List<TransactionEntity>

val activity = activity as MainActivity
val myAppDatabaseData = activity.getAppDatabaseData()
val myNetwrk = activity.getNwtwrkData()

// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.receive_fragment, container, false)
val recyclerView = rootView.findViewById<RecyclerView>(R.id.transaction_recycler_in) as RecyclerView
linearLayoutManager = LinearLayoutManager(activity, LinearLayout.VERTICAL, false)
recyclerView.layoutManager = linearLayoutManager
recyclerView.adapter = TransactionRecyclerAdapter(NewIT,myAppDatabaseData,TransactionAdapterDirection.INCOMING,myNetwrk)
recyclerView.setHasFixedSize(true)
return rootView
}

}

我试过这个, mAdapter.notifyDataSetChanged();

最佳答案

如果您想从 Activity/fragment 更新数据,则可以在适配器内创建一个函数,如下所示:

 public void update(ArrayList<String> modelList){
//string arraylist is for example pass your data
// replace your adapter data with argument data
mAdapter.notifyDataSetChanged();
}

对于 Kotlin :
fun update(modelList:ArrayList<String){
myList = modelList
myAdapter!!.notifyDataSetChanged()
}

从 Activity/fragment 调用此函数,如下所示:
 mAdapter.update(response.getList());

关于android - 如何使用 kotlin android 更新我的 Recyclerview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53005135/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com