Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Fetching Your Google Documents and Displaying Them in a ListView Using SL4A

Author: Rachee Singh -- Published? true -- FormatLanguage: W

Problem:

You need to get the details of your Google documents after logging in with your Google ID and password.

Solution:

Using the library gdata.docs.service, we can login (getting the username and password from the user) and then get the Google documents feeds.

Discussion:

Fire up the Android Scripting Environment on your device (or emulator). Open a new Python script and add this code to the script. If you have not worked in Python before, be aware that indentation, rather than braces, are used for statement grouping, so you must be very consistent about leading spaces.

import android
import gdata.docs.service

client =  gdata.docs.service.DocsService()

username = droid.dialogGetInput('Username').result
password = droid.dialogGetPassword('Password', 'For ' _username).result

def truncate(content, length=15, suffix='...'):
	if len(content) <=length:
		return content
	else:
		return content[:length] + suffix
try:
	client.ClientLogin(username, password)
except:
	droid.makeToast("Login Failed")
	
docs_feed = client.GetDocumentListFeed()

documentEntries = []

for entry in docs_feed.entry:
	documentEntries.append('%-18s %-12s %s' % (truncate(entry.title.text.encode('UTF-8')), entry.GetDocumentType(), entry.resourceId.text))
	
droid.dialogCreateAlert('Documents:')
droid.dialogSetItems(documentEntries)
droid.dialogShow()

This is how the editor looks after you have finished entering the code:

In this python code, we use the gdata.docs.service.DocsService() to connect to the Google account of a user. The username and password is taken from the user. Once the login is done successfully, the GetDocumentListFeed() method is used to get the feed list of the Google documents. We format the details of each entry and append it to a list named documentEntries. This list is then passed as argument to the Alert dialog which displays all these entries in a list.

This is how the list with my own document list looks:

No records found.