Searching for parameter by editText

1

I'm having trouble fetching some data from a specific parameter in my android app, in webservice I already search and using GET already with the data I want it brings me the data, but I need it when I insert a text inside my edittext and hit the button it just searches for the data of the parameter I searched, could anyone help me?

GET that looks for specific data when clicking the button.

@GET("Produto/get/CHICLETES")
Call<List<GitHubRepoBuscar>> reposForUser();

GET that is to bring the data written in edittext.

@GET("Produto/get/{busca}")
    Call<List<GitHubRepoBuscar>> reposForUser(
            @Path("busca") String busca
    );

Main2Activity class:

public class Main2Activity extends AppCompatActivity {
    private ListView listView;
    private EditText editText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        editText = (EditText) findViewById(R.id.editTextBuscar);
        listView = (ListView) findViewById(R.id.listView2);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),"Buscando: " + editText.getText().toString(), Toast.LENGTH_SHORT).show();
                loadJson();
            }
        });

    }
    public void loadJson(){

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("http://192.168.25.237:8080/FazendaWebservice/webresources/fazenda/")
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit = builder.build();

        GitHubClientBuscar client = retrofit.create(GitHubClientBuscar.class);
        Call<List<GitHubRepoBuscar>> call = client.reposForUser("descricaocompleta");

        call.enqueue(new Callback<List<GitHubRepoBuscar>>() {
            @Override
            public void onResponse(Call<List<GitHubRepoBuscar>> call, Response<List<GitHubRepoBuscar>> response) {
                List<GitHubRepoBuscar> repos = response.body();
                listView.setAdapter(new GitHubRepoBuscarAdapter(Main2Activity.this, repos));
            }

            @Override
            public void onFailure(Call<List<GitHubRepoBuscar>> call, Throwable t) {
                Toast.makeText(Main2Activity.this, "         Erro ao estabelecer conexão" +"\n"+"Por favor tente novamente mais tarde!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
    
asked by anonymous 28.08.2017 / 16:13

1 answer

1

Enter a parameter of type String into method loadJson() that will receive the value entered by the user:

public void loadJson(String busca){
// aqui o restante do código.
}

Once this is done, enter the busca variable in the reposForUser() method. See:

Call<List<GitHubRepoBuscar>> call = client.reposForUser(busca);

To use, just put the result of EditText . See:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // método recebe o resultado digitado no EditText.
        loadJson(editText.getText().toString());
    }
});
    
28.08.2017 / 16:30