注意:使用notifyItemRemoved(position)更新数据之前,还必须集合中删除该数据:mDatas.remove(position),否则不更新。
更新数据
这里更新数据集不是用adapter.notifyDataSetChanged()而是 notifyItemInserted(position)与notifyItemRemoved(position) 否则没有动画效果。
向指定位置插入Item
public final void notifyItemInserted(int position)
移除指定位置Item
public final void notifyItemRemoved(int position)
更新指定位置Item
public final void notifyItemChanged(int position)
为adapter中添加两个方法:
public void addData(int position) {
mDatas.add(position, “Insert One”);
notifyItemInserted(position); //
删除
}
public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position); //插入
}
notifyItemChanged(position); //更新
原生的Json解析:
public static JSONObject getJSON(Map map) {
Iterator iter = map.entrySet().iterator();
JSONObject holder = new JSONObject();
while (iter.hasNext()) {
Map.Entry pairs = (Map.Entry) iter.next();
String key = (String) pairs.getKey();
Map m = (Map) pairs.getValue();
JSONObject data = new JSONObject();
try {
Iterator iter2 = m.entrySet().iterator();
while (iter2.hasNext()) {
Map.Entry pairs2 = (Map.Entry) iter2.next();
data.put((String) pairs2.getKey(), (String) pairs2
.getValue());
}
holder.put(key, data);
} catch (JSONException e) {
Log.e(“Transforming”, “There was an error packaging JSON”, e);
}
}
return holder;
}