Android ListView how to populate with dynamic information retrieved from a google spreadsheet -
i'm working on android app takes info google spreadsheet , puts in on list view. however, i'm unable come solution this. help appreciated. spreadsheet integration utilize google sheets api. know client login not secure, test app project purposes :)
here code looks -
package com.bish.test; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.widget.toast; import android.app.activity; import android.app.progressdialog; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.gridview; import android.widget.listview; import android.widget.toast; import com.google.gdata.client.authn.oauth.*; import com.google.gdata.client.spreadsheet.*; import com.google.gdata.data.*; import com.google.gdata.data.batch.*; import com.google.gdata.data.spreadsheet.*; import com.google.gdata.util.*; import java.io.ioexception; import java.net.*; import java.util.*; public class mainactivity extends actionbaractivity { spreadsheetservice service = new spreadsheetservice("myspreadsheetintegration-v1"); string username = "cccc@gmail.com"; string password = "xxx"; listview list; string[] web; integer[] imageid; customlist adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); login(); seek { refreshdata(); } grab (ioexception e) { e.printstacktrace(); } grab (serviceexception e) { e.printstacktrace(); } adapter = new customlist(mainactivity.this, web, imageid); list=(listview)findviewbyid(r.id.list); list.setadapter(adapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } public void login() { final progressdialog ringprogressdialog = progressdialog.show(mainactivity.this, "please wait ...", "logging in...", true); ringprogressdialog.setcancelable(true); new thread(new runnable() { @override public void run() { seek { service.setusercredentials(username, password); thread.sleep(2200); } grab (exception e) { toast.maketext(getapplicationcontext(), "error: check net connection!", toast.length_long).show(); e.printstacktrace(); system.exit(0); } ringprogressdialog.dismiss(); } }).start(); } public void refreshdata() throws authenticationexception, malformedurlexception, ioexception, serviceexception { } }
i hope help you. code:
1) creates new spreadsheet
2) inserts new worksheet
3) inserts values in cells
4) displays values 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; new asynctask<void, void, void>() { @override protected void doinbackground(void... params) { final string scope = "https://docs.google.com/feeds/default/private/full"; final string credentials = sessionmanager.getcredentials().getselectedaccountname(); // create new google spreadsheet corresponding name spreadsheetentry newentry = new spreadsheetentry(); newentry.settitle(new plaintextconstruct(constants.file_prefix + mfilename)); url url = null; seek { log.d(tag, "cred: " + credentials); url = new url (scope + "?xoauth_requestor_id=" + credentials); // insert file spreadsheetservice.insert(url, newentry); } grab (malformedurlexception e) { log.d(tag, "malformedurlexception: " + e.getmessage()); } grab (ioexception e) { log.d(tag, "ioexception: " + e.getmessage()); } grab (serviceexception e) { log.d(tag, "serviceexception: " + e.getmessage()); } grab (illegalargumentexception e) { // receive media not supported exception, file created!!!! log.d(tag, "illegalargumentexception: " + e.getmessage()); } grab (exception e) { log.d(tag, "exception: " + e.getmessage()); } seek { // files user's google drive spreadsheetfeed feed = spreadsheetservice.getfeed( feedurlfactory.getdefault() .getspreadsheetsfeedurl(), spreadsheetfeed.class); // creating list of spreasheets in gdrive list<com.google.gdata.data.spreadsheet.spreadsheetentry> spreadsheets = feed.getentries(); // parsing trough feed entries (int = 0; < spreadsheets.size(); i++) { com.google.gdata.data.spreadsheet.spreadsheetentry e = (com.google.gdata.data.spreadsheet.spreadsheetentry) spreadsheets.get(i); // if locate file based on filename if( e.gettitle().getplaintext().equals(constants.file_prefix + mfilename)) { log.d(tag, "entry: " + e.gettitle().getplaintext()); url worksheetfeedurl = e.getworksheetfeedurl(); log.d(tag, "worksheetfeedurl: " + worksheetfeedurl); // first time feed used create new worksheet worksheetfeed worksheetfeed = spreadsheetservice.getfeed (worksheetfeedurl, worksheetfeed.class); log.d(tag, "worksheetfeed ok !"); // create sec worksheet worksheetentry newworksheet = new worksheetentry(15, 5); newworksheet.settitle(new plaintextconstruct("sheet2")); worksheetfeed.insert(newworksheet); // sec time feed used worksheets worksheetfeed = spreadsheetservice.getfeed (worksheetfeedurl, worksheetfeed.class); // first worksheet , insert titles list <worksheetentry> worksheetentrys = worksheetfeed.getentries (); worksheetentry sheet1 = worksheetentrys.get(0); worksheetentry sheet2 = worksheetentrys.get(1); url sheet1cellfeedurl = sheet1.getcellfeedurl (); cellfeed sheet1cellfeed = spreadsheetservice.getfeed (sheet1cellfeedurl, cellfeed.class); sheet1cellfeed.insert (new cellentry (1, 1, getresources().getstring(r.string.cell_title_name))); sheet1cellfeed.insert (new cellentry (1, 2, getresources().getstring(r.string.cell_title_description))); sheet1cellfeed.insert (new cellentry (3, 2, getresources().getstring(r.string.some_string))); sheet1cellfeed.insert (new cellentry (13, 2, "=countif(sheet1!f2:f,b3)")); sheet1cellfeed.insert (new cellentry (14, 2, "=b9 - today()")); // content cells sec worksheet url sheet2cellfeedurl = sheet2.getcellfeedurl (); cellfeed sheet2cellfeed = spreadsheetservice.getfeed (sheet2cellfeedurl, cellfeed.class); list<cellentry> cellentrylist = sheet2cellfeed.getentries(); if(cellentrylist != null && !cellentrylist.isempty()) { (cellentry cellentry : cellentrylist) { cell cell = cellentry.getcell(); log.d(tag, "value:" + cell.getvalue()); } } break; } } } grab (exception e) { log.d(tag, "exception: " + e.getmessage()); runonuithread(new runnable() { @override public void run() { showmessage("error!"); } }); } homecoming null; } }.execute();
android listview google-apps-script
No comments:
Post a Comment