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();
}
}
My God, it was such a stupid thing.
– Aline Ramos
No need to apologize! We are here to help! =)
– Thiago Luiz Domacoski
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.
– Aline Ramos
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
– Aline Ramos
pq ñ may have spaces?
– Thiago Luiz Domacoski
So use the Urlencoder! it will transform spaces into +! type: https://www.googleapis.com/books/v1/volumes?q=tomates+verdes_fritos
– Thiago Luiz Domacoski
pq if turn spaces error 400 to 404 :/ tense
– Aline Ramos
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()
– Aline Ramos
Let’s go continue this discussion in chat.
– Thiago Luiz Domacoski