How to display Dropbox files in a listview in android?

风流意气都作罢 提交于 2019-12-02 03:56:42

I now see that your code is like in DownloadRandomPicture.java.

String fnames [] = null;

In doOnBackGround():

     ArrayList<String> filenames = new ArrayList<String>();

        for (Entry ent: dirent.contents) {
            if (ent.thumbExists) {
                  filenames.add(ent.path);
             }
        }

     fnames = filenames.toArray(new String[filenames.size()]);

in onPostExecute():

       if ( fnames != null )
        {
            ArrayAdapter<String> array = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, fnames);

            listView1.setAdapter(array);
        }
       else
            showToast ( "fnames==null" );

if you fixed your app by catching a null-pointer exception, you still have a null value that needs to be addressed, if you are trying to add a null array as an adapter nothing will show up. From the code it looks like you are creating an array adapter from fnames, and then trying to reassign values to the array adapter twice with the secondary for loop. So I am willing to bet that fnames is null. I would set a break point on fnames in the doInBackground method and check it. Also the value that is returned from doInBackground is the parameter for onPostExecute so if you are returning a value that you want to use, use the parameter result from onPostExecute.

If you want to show the files and folder of a drop box in List View then simply use this code in your project and it will solve your problem.

private void PopulateList() 
        {
          List<String> filename = new ArrayList<String>();
          String mPath = "/";
          Entry dirent = null;
            try 
            {
                dirent = mApi.metadata(mPath, 1000, null, true, null);
            } catch (DropboxException e)
            {
                System.out.println("Error :  "+e.getMessage());
            }
            for (Entry ent: dirent.contents)
            {
                if(ent.isDir)
                {
                    filename.add(ent.fileName());               
                }  
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1, filename );
             mListView.setAdapter(arrayAdapter); 

        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!