I'm making an Android App with Google Spreadsheets. I'm scanning a barcode from a book, looking the info with the Google Books API and then I need to save this info into a sheet.
The following code is inside MainActivity:
private class GetBookInfo extends AsyncTask <String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// make Call to the url
makeCall("https://www.googleapis.com/books/v1/volumes?" +
"q=isbn:" + ean_content + "&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c");
//print the call in the console
System.out.println("https://www.googleapis.com/books/v1/volumes?" +
"q=isbn:" + ean_content + "&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c");
return null;
}
@Override
protected void onPreExecute() {
// we can start a progress bar here
}
@Override
protected void onPostExecute(String result) {
String ruta = save_cover(getApplicationContext(), title, book_cover);
Intent intent = new Intent(MainActivity.this, Spreadsheets.class);
finish();
startActivity(intent);
}
}
After this code I have public void makeCall(String stringURL)
. It searchs the book's title, author, date, description and category of the book.
I have another Activity:
public class Spreadsheets extends Activity implements EasyPermissions.PermissionCallbacks
In this activity, I have the following code to write into a Google Sheet:
public void setDataToApi (String title, String author, String date, String category, String description) throws IOException {
String spreadsheetId = "1Gg121IjABekfSTKXg_TQzJgg4boxvYIQnmf_K4YDboo";
String range = "Sheet1!A2:E2";
List<List<Object>> values = new ArrayList<>();
List<Object> data1 = new ArrayList<>();
data1.add(title);
data1.add(author);
data1.add(date);
data1.add(category);
data1.add(description);
values.add(data1);
ValueRange valueRange = new ValueRange();
valueRange.setMajorDimension("ROWS");
valueRange.setRange(range);
valueRange.setValues(values);
UpdateValuesResponse response = this.mService.spreadsheets().values().update(spreadsheetId, range, valueRange)
.setValueInputOption("RAW")
.execute();
System.out.printf("%d cells updated.", response.getUpdatedCells());
}
My problem is that I'm trying to call the setDataToApi method from the onPostExecute method, before the intent, but it does not work at all. Thank you very much!
You cannot call the method of another Activity from outside that Activity. Before the Intent
is actually dispatched, the other Activity is not yet running. You either need to
GetBookInfo
to the SpreadSheets
Activity so that it can perform the onPostExecute
directly in the Activity. This requires passing the book's ID via an extra on the Intent
. Or... GetBookInfo
where it is in MainActivity
and pass all the looked up details of the book through extras on the Intent
. If you have a method being called from 2 different activities, I would recommend putting that method in a separate class. So move setDataToApi
into it's own Java class, instantiate an object of your new class and use that method in both of your activities that call it.
Or, as Elan said in their answer, use intents and pass information to your other activity using putExtra
and write your own logic in the onCreate
method to call setDataToApi
. Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.