How to call initLoader inside Onclicklistener

Asked

Viewed 64 times

2

Hi, it is possible to call an initLoader inside an Onclicklistener?

I need it to be started after the editText data entry, because the network request returns error 400, URL enters without the user query, because the variable was not passed in the Onclick method, the parameters of the initLoader shows error in this, says Loadercallbacks is required and not Onclicklistener.

package com.example.android.listadelivros;

import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
        implements LoaderCallbacks<List<DadosLivro>> {

    private static String pesquisarDadosLivro;
    private DadosLivrosAdapter mAdapter;

    private static final String GOOGLE_LIVROS_URL =
            "https://www.googleapis.com/books/v1/volumes?q= " + pesquisarDadosLivro;
    ;

    private static final int DADOSLIVROS_ID_LOADER = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button pesquisarLivro = (Button) findViewById(R.id.pesquisar);
        pesquisarLivro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditText dadosLivro = (EditText) findViewById(R.id.dados_livro);
                pesquisarDadosLivro = dadosLivro.getText().toString();

                final LoaderManager loaderManager = getLoaderManager();
                loaderManager.initLoader(DADOSLIVROS_ID_LOADER, null,
                        this);
            }
        });

        ListView listView = (ListView) findViewById(R.id.lista);

        mAdapter = new DadosLivrosAdapter(this, new ArrayList<DadosLivro>());

        listView.setAdapter(mAdapter);

    }

    @Override
    public android.content.Loader<List<DadosLivro>> onCreateLoader(int i, Bundle bundle) {

        return new DadosLivrosLoader(this, GOOGLE_LIVROS_URL);
    }

    @Override
    public void onLoadFinished(android.content.Loader<List<DadosLivro>> loader, List<DadosLivro>
            informacoesLivros) {

        if (informacoesLivros != null && !informacoesLivros.isEmpty()) {
            mAdapter.addAll(informacoesLivros);
        }
    }

    @Override
    public void onLoaderReset(android.content.Loader<List<DadosLivro>> loader) {
        mAdapter.clear();
    }
}

1 answer

1


The problem occurs because within the OnClickListener, the context is another!

Within the onclick method, your this is an Onclicklistener and not a LoaderCallbacks!

Try it this way:

pesquisarLivro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            EditText dadosLivro = (EditText) findViewById(R.id.dados_livro);
            pesquisarDadosLivro = dadosLivro.getText().toString();

            final LoaderManager loaderManager = getLoaderManager();
            loaderManager.initLoader(DADOSLIVROS_ID_LOADER, null,
              //Vamos usar o this de MainActivity!!!
                    MainActivity.this);
        }
    });

There’s one more problem with your code:

private static final String GOOGLE_LIVROS_URL =
            "https://www.googleapis.com/books/v1/volumes?q= " + pesquisarDadosLivro;

This url will always add null because it is created before updating the variable pesquisarDadosLivro

Try the shape sequinte:

// Vamos transformar %s em o que o usuário digitou no campo!
    private static final String GOOGLE_LIVROS_URL =
                "https://www.googleapis.com/books/v1/volumes?q=%s" ;

When invoking your Loadermanager, we will pass the text q the user added!

// Vamos encodar a String para adicionar na url 


 final String encodeParam = URLEncoder.encode(pesquisarDadosLivro, "utf-8"));
// Alteramos o %s por encodeParam;
    final String _url = String.format(DADOSLIVROS_ID_LOADER, encodeParam);

loaderManager.initLoader(_url, null,
 //Vamos usar o this de MainActivity!!!
    MainActivity.this);
  • My God, it was such a stupid thing.

  • No need to apologize! We are here to help! =)

  • Good, the problem is that the URL keeps going without the variable included in the query, arrives as null and comes back with error 400.

  • hey buddy, I got it, the problem was the white space in the url, I did a test and it worked, now I need to solve if the user enters more than one word so that there are no blanks and the error returns

  • pq ñ may have spaces?

  • So use the Urlencoder! it will transform spaces into +! type: https://www.googleapis.com/books/v1/volumes?q=tomates+verdes_fritos

  • pq if turn spaces error 400 to 404 :/ tense

  • Cool, I’ll see about it yes, it worked on some words I tested and others presented this error: Object has been collected Cannot evaluate org.json.Jsonobject.toString()

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.