Search code examples
androidlistviewhashmapbackendless

Using onListItemClick with Backendless


in my application it is possible to retrieve a list of books saved on the server. I show the books in a Listview and now I try to give the users of my application a detailed overview of the selected (by onListItemClick) book with name, releasedate, author,... But in this case I don't know how to transfer the selected book and its data to my BookDetailActivity. I can't handle the Map BackendlessCollection in onListItemClick...

This is my BookAdapter:

public BooksAdapter(Context context, int resource, List<Books> book_list )
{
    super( context, resource, book_list );
    mResource = resource;
    mInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
}

@Override
 public View getView(int position, View convertView, ViewGroup parent ){
    View view = convertView == null ? mInflater.inflate( mResource, parent, false ) : convertView;    
    TextView booksNameView = (TextView) view.findViewById( R.id.bookName );
    TextView booksReleaseView = (TextView) view.findViewById( R.id.bookRelease );
    TextView booksAuthorView = (TextView) view.findViewById( R.id.bookAuthor );
    Books item = getItem( position );
    booksNameView.setText( item.getName() );
    objectIdView.setText( item.getObjectId() );
    booksReleaseView.setText( item.getRelease() );
    booksAuthorView.setText( item.getAuthor() );
    return view;
}

This is the main code of my BooksListActivity:

//inside the onCreate
adapter = new BooksAdapter( BooksListActivity.this, R.layout.list_item_books, totalBooks );
setListAdapter( adapter );

Backendless.Data.of( Books.class ).find( query, new LoadingCallback<BackendlessCollection<Books>>( this , getString( R.string.loading_books ), true )
    {
        @Override
        public void handleResponse( BackendlessCollection<Books> booksBackendlessCollection )
        {   book_list = booksBackendlessCollection;
            addMoreItems( booksBackendlessCollection );
            super.handleResponse( booksBackendlessCollection );}
    } );
    ListView list = (ListView) findViewById( android.R.id.list );

//outside the onCreate
@Override
protected void onListItemClick(ListView l, View v, int position, long ObjectId) {
    super.onListItemClick(l, v, position, ObjectId);
    Intent i = new Intent(this, BookDetailActivity.class);
    startActivity(i);
}

At last I would show the details of the selected book in my BookDetailActivity as typical TextViews.

I really hope you can help me! Many thanks in advance!


Solution

  • You can pass data to the next activity by using Intent extras. All you need to do is use intent.putExtra() to pass the data.

    In your case, inside your onListItemClick here's what you need to do:

    Intent i = new Intent(this, BookDetailActivity.class);
    Book book = totalBooks.get(i);
    i.putExtra("title", book.title);
    i.putExtra("content", book.content);
    startActivity(i);
    

    And eventually in the BookDetailActivity activity, you can read the data by calling getIntent().getExtras()

    Or,

    you can pass the entire Book object using a Bundle:

    Intent i = new Intent(this, BookDetailActivity.class);
    Book book = totalBooks.get(i);
    Bundle bundle = new Bundle();
    bundle.putSerializable("book", book);
    i.putExtras(bundle);
    startActivity(i);