0
我想做文件管理器。这个alghorithm与ArrayAdapter很好地工作,但是当我在SimpleAdapter下重新使用它时,它停止正常工作。主要问题:当我将元素添加到ArrayList时,它被复制。我不知道有什么问题。帮我。 Thaks。SimpleAdapter故障
onActicityCreated方法:
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);
}
}
您每次通过'for'循环使用相同的'Map'实例。您可能的意思是每次在循环的顶部创建一个新的。 –
哦,狗屎。谢谢你。我怎么没有注意到? –