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 theListView
.Everything is working fine, except that the view is not refreshed. I also tried adding
view.invalidate()
to theDeleteMatchDialogFragment
but no luck either.What do I need to do here?
原文:https://stackoverflow.com/questions/28641766
最满意答案
>>> 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)]
相关问答
更多-
在python中按块排序列表(Sort list by chunks in python)[2023-11-26]
您可以组合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 ... -
在Python中按频率排序列表(Sort list by frequency)[2023-02-08]
我认为这将是一个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 ...
-
python列表排序(python list sort)[2022-05-27]
实际上,这个问题是有效的,答案在一般情况下并不完全正确。 如果测试材料尚未排序,则无法正确按字母顺序排列,但是会导致列表按错误的顺序排序: >>> 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: ...
-
Python:对列表进行排序(Python: sort the list)[2023-06-02]
>>> 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 ...