Skip to content Skip to sidebar Skip to footer

Android Listview How To Populate With Dynamic Information Retrieved From A Google Spreadsheet

I'm working on an android app that takes information from a google spreadsheet and puts in on a list view. However, I'm unable to come up with a solution for this. Any help would g

Solution 1:

I hope this will help you. This code:

1) Creates a new Spreadsheet

2) Inserts a new Worksheet

3) Inserts some values in the cells

4) Displays the values from the cells

import com.google.gdata.client.spreadsheet.FeedURLFactory;
import com.google.gdata.data.spreadsheet.Cell;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.spreadsheet.CustomElementCollection;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.util.ServiceException;


newAsyncTask<Void, Void, Void>() {

@Overrideprotected Void doInBackground(Void... params) {                                             
    finalStringSCOPE="https://docs.google.com/feeds/default/private/full";
    finalStringCREDENTIALS= sessionManager.getCredentials().getSelectedAccountName();

    // CREATE A NEW GOOGLE SPREADSHEET WITH THE CORRESPONDING NAMESpreadsheetEntrynewEntry=newSpreadsheetEntry();
    newEntry.setTitle(newPlainTextConstruct(Constants.FILE_PREFIX + mFileName));

    URLurl=null;
    try {
        Log.d(TAG, "CRED: " + CREDENTIALS);
        url = newURL (SCOPE + "?xoauth_requestor_id=" + CREDENTIALS);

        // INSERT THE FILE
        spreadsheetService.insert(url, newEntry);
    } catch (MalformedURLException e) {
        Log.d(TAG, "MalformedURLException: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "IOException: " + e.getMessage());
    } catch (ServiceException e) {
        Log.d(TAG, "ServiceException: " + e.getMessage());
    } catch (IllegalArgumentException e) {
        // I RECEIVE MEDIA NOT SUPPORTED EXCEPTION, BUT THE FILE IS CREATED!!!!
        Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
    } catch (Exception e) {
        Log.d(TAG, "Exception: " + e.getMessage());
    }
    try {
        // GET ALL THE FILES FROM THE USER'S GOOGLE DRIVESpreadsheetFeedfeed= spreadsheetService.getFeed(
                FeedURLFactory.getDefault()
                        .getSpreadsheetsFeedUrl(),
                SpreadsheetFeed.class); 
        // Creating the list of spreasheets in GDrive
        List<com.google.gdata.data.spreadsheet.SpreadsheetEntry> spreadsheets = feed.getEntries();

            // parsing trough the feed entriesfor (inti=0; i < spreadsheets.size(); i++) {
            com.google.gdata.data.spreadsheet.SpreadsheetEntrye= (com.google.gdata.data.spreadsheet.SpreadsheetEntry) spreadsheets.get(i);
            // IF WE LOCATE THE FILE BASED ON THE FILENAMEif( e.getTitle().getPlainText().equals(Constants.FILE_PREFIX + mFileName)) {

                Log.d(TAG, "ENTRY: " + e.getTitle().getPlainText());

                URLworksheetFeedUrl= e.getWorksheetFeedUrl();

                Log.d(TAG, "worksheetFeedUrl: " + worksheetFeedUrl);

                // The first time this feed is used to create the new worksheetWorksheetFeedworksheetFeed= spreadsheetService.getFeed (worksheetFeedUrl, WorksheetFeed.class);

                Log.d(TAG, "worksheetFeed OK !");

                // Create the second worksheet WorksheetEntrynewWorksheet=newWorksheetEntry(15, 5);
                newWorksheet.setTitle(newPlainTextConstruct("Sheet2"));
                worksheetFeed.insert(newWorksheet);

                // The second time this feed is used to get the worksheets
                worksheetFeed = spreadsheetService.getFeed (worksheetFeedUrl, WorksheetFeed.class);

                // Get the first worksheet and insert the titles
                List <WorksheetEntry> worksheetEntrys = worksheetFeed.getEntries ();
                WorksheetEntrysheet1= worksheetEntrys.get(0);
                WorksheetEntrysheet2= worksheetEntrys.get(1);

                URLsheet1CellFeedUrl= sheet1.getCellFeedUrl ();
                CellFeedsheet1CellFeed= spreadsheetService.getFeed (sheet1CellFeedUrl, CellFeed.class);

                sheet1CellFeed.insert (newCellEntry (1, 1, getResources().getString(R.string.cell_title_name)));
                sheet1CellFeed.insert (newCellEntry (1, 2, getResources().getString(R.string.cell_title_description)));
                sheet1CellFeed.insert (newCellEntry (3, 2, getResources().getString(R.string.some_string)));                   
                sheet1CellFeed.insert (newCellEntry (13, 2, "=COUNTIF(Sheet1!F2:F,B3)"));
                sheet1CellFeed.insert (newCellEntry (14, 2, "=B9 - TODAY()"));


                // GET THE CONTENT FROM THE CELLS FROM THE SECOND WORKSHEETURLsheet2CellFeedUrl= sheet2.getCellFeedUrl ();
                CellFeedsheet2CellFeed= spreadsheetService.getFeed (sheet2CellFeedUrl, CellFeed.class);

                List<CellEntry> cellEntryList = sheet2CellFeed.getEntries();
                if(cellEntryList != null && !cellEntryList.isEmpty()) {
                    for (CellEntry cellEntry : cellEntryList) {
                        Cellcell= cellEntry.getCell();
                        Log.d(TAG, "VALUE:" + cell.getValue());
                    }
                }

                break;
            }
        }
    } catch (Exception e) {
        Log.d(TAG, "Exception: " + e.getMessage());
        runOnUiThread(newRunnable() {

            @Overridepublicvoidrun() {
                showMessage("Error!");
            }
        });
    }

    returnnull;
  }
}.execute();

Post a Comment for "Android Listview How To Populate With Dynamic Information Retrieved From A Google Spreadsheet"