从另一个类

时间:2018-05-16 10:31:40

标签: android kotlin

我正在kotlin开发我的第一个应用程序,我遇到了以下问题:

我的活动中有一个“RecyclerView”,有一个更新方法

class GroupsActivity : AppCompatActivity() {

val TAG = "GroupsActivity"


companion object {
    var active: Boolean = false

    var tableModel: List<GroupModel>? = null

}

fun updateList() {

    rv_list.adapter = GroupsListAdapter(this, tableModel)

}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_groups)

    if (WebSocketConnection.connected) {
        print(1)
    }

    rv_list.layoutManager = LinearLayoutManager(this)

    rv_list.hasFixedSize()

    rv_list.adapter = GroupsListAdapter(this, tableModel)


}
}

列表将随时从另一个类异步更新。这是方法:

fun jsonActionCheck(action : String, jsonString: String){

    val gson = Gson()

    when (action){
        "LOGIN_OK" -> {}
        "LOGOUT" -> {}
        "GET_GROUPS" -> {

            val jsonModel : JsonGroups = gson.fromJson(jsonString, JsonGroups::class.java)

            if(GroupsActivity.active){

                GroupsActivity.tableModel = jsonModel.data

                GroupsActivity.updateList()
            }
        }
        "GET_CHAT_LIST" -> {}
        "SEND_MESSAGE" -> {
            //var grp = model.data.get(0).group
            //var msg = model.data.get(0).message
        }
        "GET_MESSAGE" -> {}
        else -> {}
    }
}

我知道该方法不是静态的,不在同伴对象之外,但从内部我无法引用该列表。

2 个答案:

答案 0 :(得分:1)

接口

编写接口并在GroupsActivity上实现它 像这样的东西

/**
 * View Call Back interface for Activity
 */
interface ViewCallBackOperations : BaseViewOpration {

    fun onDataReceived(randomDialog: List<MovieModel>)

    fun onError(error: String)
}

在Activity上实现接口并覆盖回调方法,类似这样

class PopularFragment : Activity(), PopularMVPContract.ViewCallBackOperations,

接口的覆盖方法,这些方法将从另一个类异步调用,因为我从Web API为我的应用程序从Internet获取数据

    override fun onDataReceived(randomDialog: List<MovieModel>) {
    adapter!!.updateData(randomDialog)
}

override fun onError(error: String) {
    this.showSnackBar("Oops !! Something went wrong please try again !!")
}

另一个班级

这是另一个来自这些方法的类

    private fun handlePopularResponse(data: JSONObject?, error: String?) {
    if (null == error && null != mViewCallBack) {
        val gson = Gson()
        val recentMoviesResponse: RecentAPIResponse = gson.fromJson(data.toString(), RecentAPIResponse::class.java)
        mViewCallBack!!.get()!!.onDataReceived(recentMoviesResponse.data)
    } else if (null != mViewCallBack){
        mViewCallBack!!.get()!!.onError(error!!)
    }
}

mViewCallBack - 这是什么?

这只是活动的弱引用对象,使用构造函数初始化,类似这样

private var mViewCallBack: WeakReference<PopularMVPContract.ViewCallBackOperations>? = null

init {
    mViewCallBack = WeakReference(viewCallBackOperations)
}

适配器类

列表对象声明:

private var movieList: MutableList<MovieModel> = mutableListOf<MovieModel>()

在适配器类中添加方法,如下所示

 fun updateData(data: List<MovieModel>) {
    movieList.clear()
    movieList.addAll(data)
    notifyDataSetChanged()
}

答案 1 :(得分:0)

您可以按照以下步骤实现您的目标:

-take list from adapter object. ( e.g. adapterObj.list)
-clear list. (e.g. adapterObj.list.clear)
-add new list in adapter list. (e.g. adapterObj.list.addAll(newlistObj))