2

What I want to do I have created the folder through pragmatically in android and done it successfully. Now I want to show the content/ list the files and folders in it(I know in starting it will be empty)

I am searching the folder by its name because the drive id is local id that is not unique through its life time. We get resource Id only when the folder is upload on the server.

Code:

public class ListFilesDrive extends GoogleDrive {

    private static DriveId folderId;
    private ListView mResultsListView;
    private ResultsAdapter mResultsAdapter;
    protected static String bcPlusfolderId;
    private static int ListFileFolder=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listfiles);

        Bundle bundle=getIntent().getExtras();
        bcPlusfolderId=bundle.getString("Folder Id");

        System.out.println("Folder is "+bcPlusfolderId);


        mResultsListView = (ListView) findViewById(R.id.listViewResults);
        mResultsAdapter = new ResultsAdapter(this);
        mResultsListView.setAdapter(mResultsAdapter);
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);

        if (ListFileFolder==1){
            ListFileFolder++;

            Query query = new Query.Builder().addFilter(Filters.and(
                    Filters.eq(SearchableField.TITLE, "folder_name"),
                    Filters.eq(SearchableField.TRASHED, false))).build();

            Drive.DriveApi.query(getGoogleApiClient(), query)
                    .setResultCallback(metadataCallback);
        }


    }

    final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback =
            new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(DriveApi.MetadataBufferResult result) {
                    if (!result.getStatus().isSuccess()) {
                        showMessage("Problem while retrieving results");
                        return;
                    }
                    showMessage("Search Done");

                    mResultsAdapter.clear();
                    mResultsAdapter.append(result.getMetadataBuffer());
                }
            };

Result Getting:

I am getting the blank screen and it keeps coming(I think this activity is being calling repeatedly. )

edited Code:

public class ListFilesDrive extends GoogleDrive {

    private static DriveId folderId;
    private ListView mResultsListView;
    protected static String bcPlusfolderId;
    private static int ListFileFolder=1;
    private static String TAG="List FIle Drive";
    private ListView mListViewFiles;
    private MetadataBuffer mMetadataBuffer;
    private ResultsAdapter mResultsAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listfiles);

        mListViewFiles = (ListView) findViewById(R.id.listViewFiles);

        mResultsAdapter=new ResultsAdapter(ListFilesDrive.this,1);
        mListViewFiles.setAdapter(mResultsAdapter);
        mListViewFiles.setEmptyView(findViewById(R.id.viewEmpty));


/*
        Bundle bundle=getIntent().getExtras();
        bcPlusfolderId=bundle.getString("Folder Id");

        System.out.println("Folder is "+bcPlusfolderId);


        mResultsListView = (ListView) findViewById(R.id.listViewResults);
        mResultsAdapter = new ResultsAdapter(this);
        mResultsListView.setAdapter(mResultsAdapter);
*/
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);

        Log.i(TAG,"On Connected");

            Query query = new Query.Builder().addFilter(Filters.and(
                    Filters.eq(SearchableField.TITLE, "BetterConnectPlusTest"),
                    Filters.eq(SearchableField.TRASHED, false))).build();

            Drive.DriveApi.query(getGoogleApiClient(), query)
                    .setResultCallback(metadataCallback);

    }

    final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback =
            new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(DriveApi.MetadataBufferResult result) {
                    if (!result.getStatus().isSuccess()) {
                        showMessage("Problem while retrieving results");
                        return;
                    }

                    try {
                        Log.i(TAG, " "+result.getStatus().toString());
                        mMetadataBuffer = result.getMetadataBuffer();
                        Log.d(TAG, "Retrieved Folder count: " + result.getMetadataBuffer().getCount());
                        showMessage("Search Done");
                    }catch (NullPointerException n){
                        n.printStackTrace();
                    }

                    mResultsAdapter=new ResultsAdapter(ListFilesDrive.this,2);

                    mResultsAdapter.notifyDataSetChanged();
                    mListViewFiles.setAdapter(mResultsAdapter);

                    //((ResultsAdapter) mListViewFiles.getAdapter()).notifyDataSetChanged();

                }
            };


    private class ResultsAdapter extends DataBufferAdapter<MetadataBuffer> {

        /**
         * Constructor.
         */
        public ResultsAdapter(Context context, int i) {
            super(context, R.layout.row_file);
            Log.i("In COntext ",Integer.toString(i));
            Log.d("In COntext ","Results Adapter");
        }

        /**
         * Inflates the row view for the item at the ith position, renders it
         * with the corresponding item.
         */
        @Override
        public View getView(int i, View convertView, ViewGroup arg2) {
            if (convertView == null) {
                convertView = View.inflate(getBaseContext(), R.layout.row_file, null);
            }
            TextView titleTextView = (TextView) convertView.findViewById(R.id.textViewTitle);
            TextView descTextView = (TextView) convertView.findViewById(R.id.textViewDescription);
            Metadata metadata = mMetadataBuffer.get(i);

            try{
                Log.i("Resource id is ",metadata.getDriveId().getResourceId());
            }catch (NullPointerException e){
                Log.i("Resouce ","Panga");
                e.printStackTrace();
            }

            Log.d("Title is ","" +metadata.getTitle());

            titleTextView.setText(metadata.getTitle());
            descTextView.setText(metadata.getModifiedDate().toString());
            return convertView;
        }
    }


}
Community
  • 1
  • 1
Ankur Khandelwal
  • 1,042
  • 3
  • 19
  • 40
  • Try removing it inside the if statement to check where you are encountering the error logically. Also check their samples - [Querying Demo](https://developers.google.com/drive/android/examples/querying) -An expanded sample of querying in Drive, including several common queries. This will guide you to check your coding structure and code implementation. You can also check this related [SO post](http://stackoverflow.com/a/22205460/5995040) about getting the DriveId of created folder in Google Drive Android API. Hope this information helps. – Mr.Rebot Dec 30 '16 at 14:40
  • By removing the if statement i am getting the response whether the folder present or not. Next Step instead of listing the children of the folder I have try to list the no of folder who is satisfying my query.. I am getting how many folder are there but not able to show their information. I have follows the [Querying article](https://github.com/googledrive/android-querying) and figure out that in the callback function , ResultsAdaptor class is not called. I don't know why? I have edited the post and added the new code. – Ankur Khandelwal Dec 31 '16 at 05:16

0 Answers0