Android:正在刷新来自 UI 线程的视图
Android: Refreshing view from UI thread
基本问题,但我尝试解决它失败了
我的 activity 包含以下
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
但也没有成功。
我需要在这里做什么?
When the user clicks the Delete button I wish to delete the match from
the database and then refresh the ListView
要从 ListView 中删除项目,请创建一个方法 MatchListViewAdapter
,该方法从 getCount
方法中使用的同一数据源实例中删除项目,例如:
public void removeItem(int pos){
this.dataContainer.remove(pos);
this.notifyDataSetChanged();
}
在MatchDao.delete(matchToDelete)
之前调用createDialog
方法中的removeItem
项
基本问题,但我尝试解决它失败了
我的 activity 包含以下
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
但也没有成功。
我需要在这里做什么?
When the user clicks the Delete button I wish to delete the match from the database and then refresh the ListView
要从 ListView 中删除项目,请创建一个方法 MatchListViewAdapter
,该方法从 getCount
方法中使用的同一数据源实例中删除项目,例如:
public void removeItem(int pos){
this.dataContainer.remove(pos);
this.notifyDataSetChanged();
}
在MatchDao.delete(matchToDelete)
createDialog
方法中的removeItem
项