简体   繁体   English

微调器显示的值(选择)在适配器更新后更改(notifyDataSetChanged)

[英]Spinner Displayed Value (Selection) Changing After Adapter Update (notifyDataSetChanged)

I have a Spinner. 我有一个微调器。 After an item is selected, and the adapter is updated by notifyDataSetChanged, the selected item displayed in the Spinner (Textview) changes. 选择一个项目并通过notifyDataSetChanged更新适配器后,显示在Spinner(Textview)中的所选项目将更改。 This functionality is by design. 此功能是设计使然。 Because the selected position stayed the same but the value of the selected position changed due to the new content from updating the adapter. 因为所选位置保持不变,但是所选位置的值由于更新适配器中的新内容而发生了变化。

I want to continue to display the originally selected item after the adapter updates. 我想在适配器更新后继续显示最初选择的项目。

I was hoping this solution would work, but the event/lister never fires. 我希望此解决方案能起作用,但事件/列表永远不会触发。 Maybe because I am using a Spinner and the list is not dispalyed? 也许是因为我使用的是Spinner而列表没有显示出来?

https://stackoverflow.com/a/29173680/2330272 https://stackoverflow.com/a/29173680/2330272

mListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

  @Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
    mListView.removeOnLayoutChangeListener(this);
    Log.e(TAG, "updated");
  }
});

mAdapter.notifyDataSetChanged();

Update Help with at least two questions: 使用至少两个问题更新帮助:

  1. How to determine when the Spinner has finished updating the view after the notifyDataSetChanged has been called for the adpater? 在为adpater调用notifyDataSetChanged之后,如何确定Spinner何时完成更新视图?

  2. How to find the position (index) of a value (label) in the adapter? 如何在适配器中找到值(标签)的位置(索引)? As the adapter be querued or elements loop thru? 当适配器排队或元素循环通过时?

If you want to maintain the current selected item , as opposed to the current selected position , then you will have to implement the adapter's getItemId(int position) callback such that: 如果要维护当前选定的项目 ,而不是当前选定的位置 ,则必须实现适配器的getItemId(int position)回调,这样:

  • Each item in the spinner has a unique id 微调器中的每个项目都有一个唯一的ID
  • The same item always has the same id 同一项目始终具有相同的ID

Common implementations of getItemId() just return zero, or maybe the item's position . 常见的getItemId()只是返回零,或者返回项目的position Neither of these will work in this situation. 这些都不能在这种情况下工作。 Ideally you'll have an actual logical ID that you can return, but if the only thing you have is a String then you could use the string's hashCode() method: 理想情况下,您将具有可以返回的实际逻辑ID,但是如果您仅有的是String则可以使用字符串的hashCode()方法:

@Override
public String getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return list.get(position).hashCode();
}

Two different strings are unlikely to have the same hash code, but it's not impossible. 两个不同的字符串不太可能具有相同的哈希码,但这并非不可能。

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

相关问题 选择状态后,使用notifyDataSetChanged更新City Spinner - Update City Spinner with notifyDataSetChanged after state is selected 适配器notifyDataSetChanged后Android更新imageview - Android update imageview after adapter notifyDataSetChanged 在adapter.notifyDataSetChanged()之后,Listview不会更新 - Listview does not update after adapter.notifyDataSetChanged() 从微调器选择中获取适配器或对象值 - Get Adapter or Object value from Spinner selection 数据库更新和适配器后,列表视图未更新。notifyDataSetChanged(); - Listview not updating after database update and adapter.notifyDataSetChanged(); 如何在RecyclerView中更新适配器notifydatasetchanged后最后设置滚动位置 - How to set scroll position at last after update adapter notifydatasetchanged in RecyclerView 在通知适配器后,notifyDataSetChanged()不会更新ListView - notifyDataSetChanged() doesn't update ListView after the adapter being notified notifyDataSetChanged()无法更新适配器onClick - notifyDataSetChanged() fails to update adapter onClick 无法使用notifyDataSetChanged更新适配器 - Cannot use notifyDataSetChanged to update adapter notifyDataSetChanged()之后的自定义适配器大小0 - Custom adapter size 0 after notifyDataSetChanged()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM