阿神2017-04-17 15:46:19
This can only mean that your grasp of ListView
is not enough. ListView
relies on the data connection of the data adapter Adapter
. Data modification also exists in the data source or data adapter. It does not mean that you need to regenerate the data adapter.
高洛峰2017-04-17 15:46:19
Use the notifyDataSetChanged() method of the adapter Adapter to update. . .
Give me an example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_add = (Button) findViewById(R.id.button);
// 1.绑定GridView
gridView = (GridView) findViewById(R.id.gridView);
// 2.创建List资源类对象:
list = new ArrayList<Map<String, Object>>();
for (int i=0; i<images.length; i++) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("imageid",images[i]);
map.put("words",words[i]);
list.add(map);
}
// 3.创建一个 SimpleAdapter
simpleAdapter = new SimpleAdapter(
this, //上下文
list, //资源对象
R.layout.simple_adapter_cell,
new String[] {"imageid","words"},
new int[] {R.id.imageView,R.id.textView}
);
// 4.加载到GridView中
gridView.setAdapter(simpleAdapter);
// 5.设置 GridView的点击事件:
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("AdapterView",Integer.toString(parent.getId()));
Log.e("View",Integer.toString(view.getId()));
Log.e("position",Integer.toString(position));
Log.e("id", Long.toString(id));
}
});
// 6.GridView 的动态更新
button_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 6.1 给资源类添加新的 Map
Map<String,Object> temp = new HashMap<String, Object>(); //新建Map
temp.put("imageid",images[0]); //添加新的资源
temp.put("words","P");
list.add(temp);
// 6.2 通知 GridView 更新
simpleAdapter.notifyDataSetChanged();
}
});
}
天蓬老师2017-04-17 15:46:19
Add directly to the data source List, and then notify the Adapter to update the data, adapter.notifyDataSetChanged();
PHP中文网2017-04-17 15:46:19
RecyclerView cooperates with staggergroidlayoutmanager
Inherit adapter yourself
Use adapter.notifydatachanged() to notify updates