在谷歌驱动器中搜索其名称的文件夹,并在android中列出其内容

时间:2016-12-29 05:10:45

标签: android google-drive-api google-drive-android-api google-drive-realtime-api

我想做什么 我在android中通过实用创建了该文件夹,并成功完成了。现在我想显示内容/列出文件和文件夹(我知道在启动它时将是空的)

我正在按名称搜索文件夹,因为the drive id is local id that is not unique through its life time。我们仅在the folder is upload on the server时获得资源ID。

代码:

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());
                }
            };

结果获取:

我收到了空白屏幕并且它一直在进来(我认为这个活动正在反复调用。)

编辑代码:

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;
        }
    }


}

0 个答案:

没有答案