I am developing an Android application that uses Google Sheets as a database. The operation, basically, is that I scan a bar code from a book, it keeps all the information about this book in the sheet of Google Sheets, and what I want is that this information shows it to me in ListView
.
When I scan two books, I keep them correctly on the sheet, but when I want to show this information in the ListView
, it shows me the last one I inserted, and I do not understand why.
Next I am showing the code and explaining what it returns to me:
The following method reads the information from the spreadsheet and inserts it into the List<Strings>results
:
private List<String> getDataFromApi() throws IOException {
String range = "Sheet1!A2:I";
List<String> results = new ArrayList<String>();
ValueRange response = CreateSpreadsheets.mService.spreadsheets().values()
//.get(CreateSpreadsheets.spreadsheet_id, range)
.get(sheet_id, range)
.execute();
List<List<Object>> values = response.getValues();
System.out.println(values);
if (values != null) {
for (List row : values) {
results.add(row.get(0) + "," + row.get(6));
}
}
return results;
}
Next, the postExecute
@Override
protected void onPostExecute(List<String> results) {
mProgress.hide();
if (results == null || results.size() == 0) {
// emptyText.setText(R.string.empty);
} else {
if(read_only.equals("no")) {
Intent intent = new Intent(Spreadsheets.this, MainActivity.class);
startActivity(intent);
// mOutputText.setText("Se ha añadido un libro a su lista");
}
else {
resultados = new ArrayList<BookItem>();
System.out.println(results);
int length = results.size();
System.out.println(length);
int i;
for (i = 0; i < length; i++) {
String string = results.get(i);
System.out.println("string results get (i)" + string);
String[] partes = string.split(",");
String titulo = partes[0];
String portada = partes[1];
System.out.println(titulo + "," + portada);
book = new BookItem(titulo, portada);
resultados.add(book);
}
read_only = "no";
rellenar();
}
}
}
Here I have put a couple of prints to check that it is really giving me back all the books I have saved, and indeed, that's the way it is:
I/System.out: string results get (i)Todo esto te daré,No portada
I/System.out: Todo esto te daré,No portada
I/System.out: string results get (i)La elegancia del erizo,http://books.google.com/books/content?id=od5GRQAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api
I/System.out: La elegancia del erizo,http://books.google.com/books/content?id=od5GRQAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api
Next, the rellenar()
function does the following:
public void rellenar(){
adapter = new BookAdapter(context, resultados, sheet_id);
bookList.setAdapter(adapter);
}
Then I leave the code of my BookAdapter
:
public class BookAdapter extends BaseAdapter {
List<BookItem> items;
private Context context;
LayoutInflater inflater;
public BookAdapter(Context context, List<BookItem> items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public BookItem getItem(int position) {
return this.items.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.fila_lista_miestanteria, viewGroup, false);
holder = new ViewHolder();
holder.title_item = (TextView) convertView.findViewById(R.id.book_title_item);
holder.favorite = (ImageButton) convertView.findViewById(R.id.favorite);
holder.cover_item = (ImageView) convertView.findViewById(R.id.book_cover_item);
holder.title_item.setText(this.items.get(position).getTitle());
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
BookItem bookItem = getItem(position);
System.out.println("position en bookdapter " + getItem(position));
holder.title_item.setText(bookItem.getTitle());
System.out.println("Titulo: " + bookItem.getTitle());
holder.favorite.setImageResource(R.drawable.ic_favorite_black_24dp);
final String url = bookItem.getCover();
Picasso.with(context).load(url).into(holder.cover_item);
return convertView; }
The prints that I have put (with the for, now commented) it seems that they are giving me back two different books:
I/System.out: size 2
I/System.out: position com.example.apptfg.BookItem@aba768a
I/System.out: 2
I/System.out: position com.example.apptfg.BookItem@aba768a
I/System.out: size 2
I/System.out: position com.example.apptfg.BookItem@d17156
I/System.out: 2
I/System.out: position com.example.apptfg.BookItem@d17156
But finally the result in my ListView
is the last book I have inserted, and not the two that I have in the list:
Could someone help me? Thank you very much in advance!