首页 \ 问答 \ Android:从UI线程刷新视图(Android: Refreshing view from UI thread)

Android:从UI线程刷新视图(Android: Refreshing view from UI thread)

基本问题,但我尝试解决它失败了

我的活动包含以下内容

override def onCreate(savedInstanceState: Bundle) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.match_list_view)
 val listView = findViewById(R.id.matchListView).asInstanceOf[ListView]
 listView.setAdapter(new MatchListViewAdapter(MatchDao.retrieveAllMatches(), this, listView))

}

它显示ListView中的匹配列表

然后我的适配器在其getView方法中包含以下逻辑用于单个匹配:

val deleteButton = updatedView.findViewById(R.id.deleteIndividualMatchButton).asInstanceOf[Button]
    deleteButton.setOnClickListener(new OnClickListener {
      override def onClick(v: View) {
        new DeleteMatchDialogFragment(getItem(position), MatchListViewAdapter.this).show(activity.getFragmentManager, "DeleteFragment"
      }
    })

然后我的DeleteMatchDialogFragment包含以下逻辑:

override def onCreateDialog(savedInstanceState: Bundle): Dialog = {
    createDialog(null, R.string.deleteMatch, R.string.ok, {
      MatchDao.delete(matchToDelete)
      adapter.notifyDataSetInvalidated()
    })
  }

所以基本上在ListView ,我有一个匹配列表,每个匹配都有一个删除按钮。 当用户单击“删除”按钮时,我希望从数据库中删除匹配项,然后刷新ListView

一切都工作正常,除了视图没有刷新。 我也尝试将view.invalidate()添加到DeleteMatchDialogFragment但也没有运气。

我需要做什么?


Basic question, but my attempts to resolve it have failed

My activity contains the following

override def onCreate(savedInstanceState: Bundle) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.match_list_view)
 val listView = findViewById(R.id.matchListView).asInstanceOf[ListView]
 listView.setAdapter(new MatchListViewAdapter(MatchDao.retrieveAllMatches(), this, listView))

}

which displays a list of matches in a ListView

My adapter then contains the following logic in its getView method for an individual match:

val deleteButton = updatedView.findViewById(R.id.deleteIndividualMatchButton).asInstanceOf[Button]
    deleteButton.setOnClickListener(new OnClickListener {
      override def onClick(v: View) {
        new DeleteMatchDialogFragment(getItem(position), MatchListViewAdapter.this).show(activity.getFragmentManager, "DeleteFragment"
      }
    })

My DeleteMatchDialogFragment then contains the following logic:

override def onCreateDialog(savedInstanceState: Bundle): Dialog = {
    createDialog(null, R.string.deleteMatch, R.string.ok, {
      MatchDao.delete(matchToDelete)
      adapter.notifyDataSetInvalidated()
    })
  }

So basically in the ListView, I have a list of Matches and each one has a Delete button opposite it. When the user clicks the Delete button I wish to delete the match from the database and then refresh the ListView.

Everything is working fine, except that the view is not refreshed. I also tried adding view.invalidate() to the DeleteMatchDialogFragment but no luck either.

What do I need to do here?


原文:https://stackoverflow.com/questions/28641766
更新时间:2021-10-31 15:10

最满意答案

>>> a = ['a','b','d','c']
>>> b = [1, 2, 4, 3]
>>> c = zip(a, b)
>>> c
[('a', 1), ('b', 2), ('d', 4), ('c', 3)]
>>> c.sort(key=lambda x: x[1])
>>> c
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

>>> a = ['a','b','d','c']
>>> b = [1, 2, 4, 3]
>>> c = zip(a, b)
>>> c
[('a', 1), ('b', 2), ('d', 4), ('c', 3)]
>>> c.sort(key=lambda x: x[1])
>>> c
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

相关问答

更多
  • 您可以组合sort和itertools.chain from operator import itemgetter from itertools import chain l=[(1,2),(2,1),(4,2),(5,1),(6,7),(7,2)] list(chain.from_iterable(sorted(l[i:i+3], key=itemgetter(1)) for i in range(0, len(l), 3))) 使用列表理解,您甚至可以避免使用链: [x for y in (sorte ...
  • 我认为这将是一个collections.Counter的好工作。 collections.Counter : counts = collections.Counter(lst) new_list = sorted(lst, key=lambda x: -counts[x]) 或者,你可以写第二行不使用lambda: counts = collections.Counter(lst) new_list = sorted(lst, key=counts.get, reverse=True) I think ...
  • 它会自动按元组中的第一个元素排序元组列表,然后通过第二个元素等等元组([1,2,3])将在元组([1,2,4])之前进行排序。 如果要覆盖此行为,将可调用作为第二个参数传递给排序方法。 这个callable应该返回1,-1,0。 It automatically sorts a list of tuples by the first elements in the tuples, then by the second elements and so on tuple([1,2,3]) will go bef ...
  • 为了跟进@ bro-grammer的评论(我使用了一个元组作为密钥,并且只用了一次计数器): 有这种方法首先必须通过列表进行计数,然后再进行排序。 from collections import Counter def perseus_sort(l): counter = Counter(l) return sorted(l, key=lambda x: (counter[x], x)) 可能有一些聪明的算法,可以以某种方式结合这两者,但我的直觉认为它会非常复杂,并且比你需要的更多 To ...
  • 实际上,这个问题是有效的,答案在一般情况下并不完全正确。 如果测试材料尚未排序,则无法正确按字母顺序排列,但是会导致列表按错误的顺序排序: >>> l = ["'''b", "a", "a'ab", "aaa"] >>> l.sort() >>> l ["'''b", 'a', "a'ab", 'aaa'] >>> from functools import partial >>> import string >>> keyfunc = partial(string.replace, old="'", ne ...
  • Python 2确实尝试提供一个排序(它对所有类型都这样做),首先是基于长度(首先是shortts dicts),如果长度相等则是按键(一个较小的键先行),然后如果所有键都相等则关于价值(较小的价值先行); 请参阅dictobject.c源代码中的characterize和dict_compare函数 。 简短演示: >>> sorted([{1:2}, {}]) [{}, {1: 2}] >>> sorted([{1:2}, {0:1}]) [{0: 1}, {1: 2}] >>> sorted([{1: ...
  • >>> a = ['a','b','d','c'] >>> b = [1, 2, 4, 3] >>> c = zip(a, b) >>> c [('a', 1), ('b', 2), ('d', 4), ('c', 3)] >>> c.sort(key=lambda x: x[1]) >>> c [('a', 1), ('b', 2), ('c', 3), ('d', 4)] >>> a = ['a','b','d','c'] >>> b = [1, 2, 4, 3] >>> c = zip(a, b) ...
  • 首先,我将colorOrder映射: colorMap = {c: i for i, c in enumerate(colorOrder)} 现在使用colorMap.get更轻松地进行排序 sorted(tupleList, key=lambda tup: colorMap.get(tup[1], -1)) 这使事情不在地图中。 如果您最后要添加它们,只需使用一个非常大的数字: sorted(tupleList, key=lambda tup: colorMap.get(tup[1], float(' ...
  • 您可以在切片上使用sorted() ,然后将结果分配回切片: al[2:] = sorted(al[2:]) You could use sorted() on a slice and then assign the result back to the slice: al[2:] = sorted(al[2:])
  • 最简洁的方法是使用比较方法中包含所需排序行为的对象作为排序键。 Python排序所需的唯一比较方法是__lt__() ,因此这是相当简单的。 例如,这是一个大致实现Python 2排序启发式的类(在可比较的对象组中的值中排序)。 你当然可以实现你喜欢的任何其他规则。 由于排序将为列表中的每个项创建其中一个对象,因此我使用__slots__并通过实习所有类型字符串使每个对象的大小尽可能低。 from sys import intern class Py2Key: __slots__ = ("val ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)