在一个ArrayAdapter更新项目 [英] Update an item in the arrayadapter
问题描述
我有一个arrayadapter.I可以将项目添加到这个arrayadapter,但有时我需要在更新这个adapter.I项目研究了互联网,并在适配器然后我做了这样无法找到有关更新特定项目什么
I have an arrayadapter.I can add items to this arrayadapter,but sometimes I need the update an item in this adapter.I researched the internet and can't find anything about update specific item in the adapter then I did like this:
Integer test=randomAdapter.getPosition(user);
randomAdapter.remove(user);
randomAdapter.insert(updated_user,test);
randomAdapter.notifyDataSetChanged();
在此code 用户
是jsonobject.I我得到那么我删除它并添加uptated用户数据到旧的对象position.This这个项目的位置似乎works.But我不知道这是更新项目,所以我要问你一个好主意。
In this code user
is a jsonobject.I am getting position of this item then I am removing it and adding uptated user data into old object position.This seems works.But I don't know this is a good idea for update an item so I am asking to you.
如果这是一个坏主意,应该如何?
If this is a bad idea,how should be ?
推荐答案
这是 ArrayAdapter
保持对数组的引用或列表
,它使用的对象。任何的方法,如添加(...)
,删除(...)
或直接插入阵列/列表(...)
行为,也可用于同步
使他们陈述线程安全的。
An ArrayAdapter
maintains a reference to the array or List
of objects that it uses. Any of the methods such as add(...)
, remove(...)
or insert(...)
act directly on the array / list and also used synchronized
statements making them thread-safe.
您的做法可能会稍微显得啰嗦,但它是安全的处事任何其他方式的一种方式。你当然可以,操纵原始数组/列出自己,但如果你需要线程安全,你必须同步添加到您自己的code。为什么要重新发明轮子?
Your approach may seem slightly long-winded but it's as safe a way of doing things as any other way. You could, of course, manipulate the original array / list yourself but if you need thread-safety you'd have to add synchronization to your own code. Why re-invent the wheel?
如果你想看看源$ C $ C为 ArrayAdapter
是<一个href=\"https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ArrayAdapter.java\"相对=nofollow>这里
If you want to look at the source code for ArrayAdapter
it's here
这篇关于在一个ArrayAdapter更新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!