最近自己写了一个遍历SD卡中txt文件并显示,但做到在fragment下让hashmap中的元素依次显示在listview的时候,UI上就是没有listview显示出来啊。。。抓狂中,开始以为是内存堵塞造成的,后来加了异步处理的handler,还是没有UI,万般无奈,求教诸位大神帮忙看下问题出在什么地方,以下是小弟的代码:(写的比较乱,各位凑合凑合哈) public class ScanFragment extends Fragment { private static final String LOG_TAG = "ScanFragment"; ArrayList<String> bookpath = new ArrayList<String>(); ArrayList<String> bookname = new ArrayList<String>(); private File path; private File[] files; private ListView mListView; private SimpleAdapter mAdapter; private List<HashMap<String, Object>> mHashMaps; private HashMap<String, Object> map; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: UpdateAdapter(); break; case 2: mListView.setAdapter(mAdapter); Log.d(LOG_TAG, "refresh!"); mAdapter.notifyDataSetChanged(); break; } } }; public class ScanThread extends Thread { public void run() { File temp = setPath(); scanFile(temp); Log.i(LOG_TAG, "bookname:" + bookname); Message msg = new Message(); msg.what = 1; Log.i(LOG_TAG, "msg.what:" + msg.what); mHandler.sendMessage(msg); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View parentView = inflater.inflate(R.layout.scan, container, false); Button scan = (Button) parentView.findViewById(R.id.scan); Button refresh = (Button) parentView.findViewById(R.id.refresh); mListView = (ListView) parentView.findViewById(R.id.founded_book_list); scan.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mHandler.post(new ScanThread()); // UpdateAdapter(); } }); refresh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Message msg = new Message(); msg.what = 2; Log.i(LOG_TAG, "msg.what:" + msg.what); mHandler.sendMessage(msg); // TODO Auto-generated method stub } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(getActivity(), "Clicked item!", Toast.LENGTH_LONG).show(); } }); return parentView; } private File setPath() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Log.e(LOG_TAG, "media_mounted"); path = Environment.getExternalStorageDirectory(); } Log.e(LOG_TAG, "SDcard directory:" + path); return path; } private ArrayList<String> scanFile(File path) { Log.e(LOG_TAG, "path:" + path); if (path.isDirectory()) { files = null; files = path.listFiles(); if (files != null) { for (File file : files) { Log.i(LOG_TAG, "take file:" + file); if (file.isDirectory()) { selectDir(file); } else { Add2Array(file); } } } } else { Add2Array(path); } return bookpath; } private void selectDir(File file1) { if (file1 != null) { Log.e(LOG_TAG, "Directory path:" + file1); for (File dirpath : file1.listFiles()) { Log.i(LOG_TAG, "take file:" + file1); scanFile(dirpath); } } else { Log.e(LOG_TAG, "empty directory:" + file1.getPath()); } } private void Add2Array(File file2) { if (file2.getName().endsWith(".txta")) { bookpath.add(file2.getAbsolutePath()); bookname.add(file2.getName()); // Log.i(LOG_TAG, "bookname:" + bookname); // Log.i(LOG_TAG, "bookpath:" + bookpath); Log.i(LOG_TAG, "bookpath:" + file2); } else { Log.i(LOG_TAG, file2 + " is not a txt file!"); } } private void UpdateAdapter() { int n = bookname.size() - 1; for (int i = 0; i <= n; i++) { Log.i(LOG_TAG, "i is :" + i); getData(i); } Log.i(LOG_TAG, "getData(0):" + getData(0)); mAdapter = new SimpleAdapter( this.getActivity().getApplicationContext(), getData(), R.layout.activity_list, new String[] { "image", "title", "info" }, new int[] {R.id.img, R.id.title, R.id.info }); mListView.setAdapter(mAdapter); Log.d(LOG_TAG, "the end!"); mAdapter.notifyDataSetChanged(); if (bookpath.equals("")) { Toast.makeText(getActivity(), "No such files !", Toast.LENGTH_SHORT) .show(); } } private List<HashMap<String, Object>> getData(int i) { mHashMaps = new ArrayList<HashMap<String, Object>>(); map = new HashMap<String, Object>(); map.put("image", R.drawable.ic_a); Log.e(LOG_TAG, "bookname.get(i):" + bookname.get(i)); map.put("title", bookname.get(i)); Log.e(LOG_TAG, "bookpath.get(i):" + bookpath.get(i)); map.put("info", bookpath.get(i)); mHashMaps.add(map); return mHashMaps; } private List<HashMap<String, Object>> getData() { mHashMaps = new ArrayList<HashMap<String, Object>>(); return mHashMaps; } } 任何建议,感激不尽~~ |
|
somebody help……….
|
|
40分 |
楼主问题在UpdateAdapter里…… getData应该直接return mHashMaps就可以了。 不过你的getData(int i)写的也有问题……总之代码风格太乱了,需要花时间整理整理~ |
错误的确是在这里。。。。十分感谢帮助,分给你了~~ |
|
的确,getdata(Int i)也有问题,每次都new了一下,结果只能取到其中的一个数组,感谢提醒,我再改改,ths~ |
|
还是有始有终吧,那个getdata(int i)的修改只要把循环改到getdata(int i)方法里面即可,另需要注意声明map = new HashMap<String, Object>();的位置不要放错就可以实现hashmap的遍历功能啦。
|