SimpleAdapter 的问题
Trouble with SimpleAdapter
我想做文件管理器。该算法与 ArrayAdapter 配合使用效果很好,但是当我在 SimpleAdapter 下重新编写它时,它停止正常工作。主要问题:当我将元素添加到 ArrayList 时,它被复制了。我不知道出了什么问题。帮我。塔克斯
onActivityCreated 方法:
private final String ATTRIBUTE_NAME_TEXT = "text";
private final String ATTRIBUTE_NAME_IMAGE = "image";
private String[] from = {ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE};
private int[] to = {R.id.currPath, R.id.imageView};
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "Tab created");
if (savedInstanceState == null) {
Log.d(TAG, "SIS == null");
paths.add("/storage/emulated/0/");
paths.add("/storage/extSdCard/");
Map<String, Object> m = new HashMap<>();
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(int i = 0; i < paths.size(); i++){
m.put("text", paths.get(i));
m.put(ATTRIBUTE_NAME_IMAGE, R.mipmap.ic_launcher);
data.add(m);
}
MySimpleAdapter adapter = new MySimpleAdapter(getActivity(), data, R.layout.item, from, to);
setListAdapter(adapter);//Creating a primary form of application
}
else{
Log.d(TAG, "SIS != null");
currentPath = savedInstanceState.get(KEY_PATH).toString();
paths = savedInstanceState.getStringArrayList(KEY_LIST);
Map<String, Object> m = new HashMap<>();
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(int i = 0; i < paths.size(); i++){
m.put(ATTRIBUTE_NAME_TEXT, paths.get(i));
m.put(ATTRIBUTE_NAME_IMAGE, R.mipmap.ic_launcher);
data.add(m);
}
MySimpleAdapter adapter = new MySimpleAdapter(getActivity(), data, R.layout.item, from, to);
setListAdapter(adapter);
}
}
为了解决这个问题,正如 Mike 在评论中提到的,您需要为循环的每次迭代创建一个新的 Map 实例。
for(int i = 0; i < paths.size(); i++){
Map<String, Object> m = new HashMap<>();
m.put(ATTRIBUTE_NAME_TEXT, paths.get(i));
m.put(ATTRIBUTE_NAME_IMAGE, R.mipmap.ic_launcher);
data.add(m);
}
我想做文件管理器。该算法与 ArrayAdapter 配合使用效果很好,但是当我在 SimpleAdapter 下重新编写它时,它停止正常工作。主要问题:当我将元素添加到 ArrayList 时,它被复制了。我不知道出了什么问题。帮我。塔克斯
onActivityCreated 方法:
private final String ATTRIBUTE_NAME_TEXT = "text";
private final String ATTRIBUTE_NAME_IMAGE = "image";
private String[] from = {ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE};
private int[] to = {R.id.currPath, R.id.imageView};
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "Tab created");
if (savedInstanceState == null) {
Log.d(TAG, "SIS == null");
paths.add("/storage/emulated/0/");
paths.add("/storage/extSdCard/");
Map<String, Object> m = new HashMap<>();
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(int i = 0; i < paths.size(); i++){
m.put("text", paths.get(i));
m.put(ATTRIBUTE_NAME_IMAGE, R.mipmap.ic_launcher);
data.add(m);
}
MySimpleAdapter adapter = new MySimpleAdapter(getActivity(), data, R.layout.item, from, to);
setListAdapter(adapter);//Creating a primary form of application
}
else{
Log.d(TAG, "SIS != null");
currentPath = savedInstanceState.get(KEY_PATH).toString();
paths = savedInstanceState.getStringArrayList(KEY_LIST);
Map<String, Object> m = new HashMap<>();
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(int i = 0; i < paths.size(); i++){
m.put(ATTRIBUTE_NAME_TEXT, paths.get(i));
m.put(ATTRIBUTE_NAME_IMAGE, R.mipmap.ic_launcher);
data.add(m);
}
MySimpleAdapter adapter = new MySimpleAdapter(getActivity(), data, R.layout.item, from, to);
setListAdapter(adapter);
}
}
为了解决这个问题,正如 Mike 在评论中提到的,您需要为循环的每次迭代创建一个新的 Map 实例。
for(int i = 0; i < paths.size(); i++){
Map<String, Object> m = new HashMap<>();
m.put(ATTRIBUTE_NAME_TEXT, paths.get(i));
m.put(ATTRIBUTE_NAME_IMAGE, R.mipmap.ic_launcher);
data.add(m);
}