我有一个AlertDialog
with a ListView
设置为多选就可以了。它还有一个Button
on it.
The Button
打开另一个AlertDialog
如果确定,将从数据集中删除所选项目ListView
,然后告诉列表视图的适配器数据集已随notifyDataSetChanged()
method.
除了一件事之外,这一切都运行良好。这ListView
在我与某些东西交互之前不会更新其内容。然后它会更新为正确的数据。
这不是一个大问题,但我真的很想要ListView
立即显示正确,而不是在焦点改变后才显示。
Code:
Button remove = (Button) view.findViewById(R.id.btn_remove_questions_edit_rack);
final Context con = this;
remove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Builder warnBuild = new Builder(con);
warnBuild.setMessage(R.string.question_deletion_warning);
warnBuild.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
SparseBooleanArray checked = list.getCheckedItemPositions();
for (String s : keys)
{
int i = keys.indexOf(s);
if (checked.get(i))
{
toRemove.add(map.get(s));
map.remove(s);
}
}
keys.clear();
keys.addAll(map.keySet());
((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
list.clearChoices(); //This makes sure the selection is cleared, if it isn't, some of the other items (those that now has the index of the selected items) will be selected when the View refreshes.
dialog.dismiss();
}
});
//Negative button here, not relevant.
}
});
Where map
and keys
are:
final HashMap<String, QualityQuestion> map = new HashMap<>();
//I add items to the map
final ArrayList<String> keys = new ArrayList<>(map.keySet());
And toRemove
是我存储要从原始对象上的“确定”按钮时从其所在的实际对象中删除的项目的位置AlertDialog
被按下。
这就是我填充我的方式ListView
首先:
final ListView list = (ListView) view.findViewById(R.id.list_questions_edit_rack);
list.setAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1,
keys));
我尝试过类似的事情list.invalidateViews()
, list.invalidate
以及我在与我的问题类似的问题中发现的其他内容。但这些都没有任何区别。我怀疑我的问题与他们的不同,因为我的物品显然已更新,只需将焦点转移到原始物品上AlertDialog
使变化可见。
我怎样才能使ListView
立即显示其数据源中的更改,而不是在焦点更改后显示?