简体   繁体   English

从不同的活动更新ListAdapter

[英]Updating ListAdapter from different activity

I am working on my hobby project to learn more about android programming and stuffs. 我正在从事自己的业余项目,以了解有关android编程和内容的更多信息。 So i recently ran into a simple problem but couldn't find a solution for it. 所以我最近遇到一个简单的问题,但找不到解决方案。

My project for now have 2 activity [MainActivity] , [CommentActivity] . 我的项目目前有2个活动[MainActivity][CommentActivity]

In MainActivity, I have a listadapter (cusAdapter) will fetch all the content from a database and display it on a listview. 在MainActivity中,我有一个listadapter (cusAdapter)将从数据库中获取所有内容并将其显示在listview上。 Everything works fine there. 那里一切正常。 It will fetch all the comments and total comments for a certain post based on the post id. 它将根据帖子ID获取某个帖子的所有评论和总评论。 In the same MainActivity, i have a comment button and a onclick listener which when clicked will bring user to the next activity [CommentActivity] and load all the comments and data needed using another custom adapter. 在同一MainActivity中,我有一个注释按钮和一个onclick侦听器,单击它们时会将用户带到下一个活动[CommentActivity],并使用另一个自定义适配器加载所需的所有注释和数据。 I also have a total comment TextView that will show the total comment fetched by the json and display it in my listview. 我还有一个总评论TextView,它将显示json提取的总评论并将其显示在我的listview中。

When a user posted a comment on CommentActivity, it will save into the database via AsyncTask. 当用户在CommentActivity上发布评论时,它将通过AsyncTask保存到数据库中。

Now the problem is, when i press back button and navigate back from CommentActivity to MainActivity, i use intent with flag (intent.flag_activity_reorder_to_front). 现在的问题是,当我按下后退按钮并从CommentActivity导航回到MainActivity时,我将意图与标志(intent.flag_activity_reorder_to_front)一起使用。 So it will never reload the MainActivity again when back button is pressed. 因此,当按下后退按钮时,它将永远不会再次重新加载MainActivity。 Also it will never update the total comment in the MainActivity. 而且,它将永远不会更新MainActivity中的总注释。 What i want to do is when back button is pressed on CommentActivity, i want it to refresh/reload the ListAdapter in MainActivity to fetch the latest data from database. 我想做的是在CommentActivity上按下后退按钮时,我希望它刷新/重新加载MainActivity中的ListAdapter以便从数据库中获取最新数据。

Also i don't want to reload the whole MainActivity, i just wanna update the view without reloading the whole activity. 我也不想重新加载整个MainActivity,我只是想更新视图而不重新加载整个活动。

I believe there must be some easy way to do this. 我相信必须有一些简单的方法可以做到这一点。 I can post codes if you guys need it to assists me. 如果你们需要帮助的话,我可以发布代码。

Thank you. 谢谢。

In your MainActivity you could check in onResume to see if the data has changed, as this will be called when you go back to it from the CommentActivity. 在MainActivity中,您可以签入onResume来查看数据是否已更改,因为当您从CommentActivity返回到数据时,将调用该数据。

If the data has changed, calling notifyDataSetChanged() on the adapter will reload the view with the new data 如果数据已更改,则在适配器上调用notifyDataSetChanged()将使用新数据重新加载视图。

Yes, there is an easy way. 是的,有一个简单的方法。 Start your sencond activity using: 使用以下方法开始您的第二项活动:

  • startActivityForResult(intent, RESULT_CODE) startActivityForResult(意图,RESULT_CODE)
  • use setResult(RESULT_OK) in your second activity after doing the commit of the changes to the database 将更改提交到数据库后,在第二个活动中使用setResult(RESULT_OK)
  • override onActivityResult() in your first activity to check when the second activity returns properly 在您的第一个活动中覆盖onActivityResult()以检查第二个活动何时正确返回
  • update the first adapter as needed. 根据需要更新第一个适配器。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM