简体   繁体   English

显示文件的ListFragment问题

[英]Problem with ListFragment which display files

I have the problem that I said in title.I want to display only txt files from internal storage of my phone to process it on click.My problem is that it isn't recognize the ListView. 我有标题中提到的问题。我只想显示手机内部存储中的txt文件以在单击时进行处理。我的问题是它无法识别ListView。 I have tried many different solutions but it didn't work.Have anyone any idea?Below it is the code from class. 我尝试了许多不同的解决方案,但是没有用,有人知道吗?下面是类中的代码。 Thank you 谢谢

public class Tab3 extends ListFragment {

private ListView listView;
    private String fileNames[];
    View mView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = getActivity().findViewById(R.id.listView);
        ArrayList<File> files = readFiles(Environment.getExternalStorageDirectory());

        fileNames = new String[files.size()];
        for (int i = 0; i < files.size(); ++i) {
            fileNames[i] = files.get(i).getName().toString().replace(".txt", "");
        }
        ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, fileNames);
        setListAdapter(listAdapter);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_tab3, container, false);
        return mView;
    }


    private ArrayList<File> readFiles(File root) {
        ArrayList<File> arrayList = new ArrayList<File>();
        File files[] = root.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                arrayList.addAll(readFiles(file));
            } else {
                if (file.getName().endsWith(".txt")) {
                    arrayList.add(file);
                }
            }
        }
        return arrayList;
    }
}

UPDATE UPDATE

public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment=null;
            switch (position){
                case 0:
                    Tab1Values tab1=new Tab1Values();
                    return tab1;
                case 1:
                    Tab2Map tab2=new Tab2Map();
                    return tab2;
                case 2:
                    Tab3 tab3=new Tab3();
                    return tab3;
            }
            return fragment;
        }
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    }

我认为应该是listView.setAdapter(listAdapter);

I think I got the problem. 我想我有问题。 onCreateView() is called after onCreate() . onCreateView()之后调用onCreate() So basically you are using findViewById() before inflating the layout. 因此,基本上,您在扩大布局之前使用findViewById() Move the code in onCreate() to onCreateView() . onCreate()的代码移动到onCreateView() It will fix the problem. 它将解决问题。

EDIT: Also replace getActivity().findViewById with mView.findViewById() 编辑: mView.findViewById() getActivity().findViewById替换为mView.findViewById()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM