Passing parameter to another Android activity

0

I created a QRCode and made it as soon as it generated the value, it was shown on the screen and I would have the option to say ok or just quit, I would like it when I clicked on Ok it was thrown to another activity where inside of this activity has a EditText to search, which in case it is already working if I write what I am looking for, now I want it as soon as I pull the value in QRCode it takes that value and look for me what I want ...

QRCODE result code:

@Override
public void handleResult(Result result) {
    final String myResult = result.getText();
    Log.d("QRCodeScanner", result.getText());
    Log.d("QRCodeScanner", result.getBarcodeFormat().toString());

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Resultado: ");
    builder.setNegativeButton("Sair", null);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //scannerView.resumeCameraPreview(BarCodeActivity.this);
            Intent scan = new Intent(BarCodeActivity.this, BuscaActivity.class);
            startActivity(scan);

        }
    });
    builder.setMessage(result.getText());
    AlertDialog alert1 = builder.create();
    alert1.show();
}

Activity codes I want the search to do:

editText = (EditText) findViewById(R.id.editTextBuscar);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            //Toast.makeText(getApplicationContext(),"Buscando: " + editText.getText().toString(), Toast.LENGTH_SHORT).show();
            pbar.setVisibility(textView.VISIBLE);
            String userid = editText.getText().toString();
            if(userid.trim().equals("")){
                pbar.setVisibility(View.INVISIBLE);
                Toast.makeText(BuscaActivity.this, "Insira algum dado para busca.", Toast.LENGTH_LONG).show();
            }else if(listView != null){
                listView.setAdapter(null);
                pbar.setVisibility(textView.VISIBLE);
                loadJson(userid);
            }else{
                loadJson(userid);
                pbar.setVisibility(textView.VISIBLE);
            }
            try  {
                InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            } catch (Exception e) {
            }
            return true;
        }
    });



    pbar = (ProgressBar)findViewById(R.id.progressBar1);
    pbar.setVisibility(View.INVISIBLE);
    editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
    listView = (ListView) findViewById(R.id.listView2);
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
            AlertDialog.Builder builder = new AlertDialog.Builder(BuscaActivity.this);
            builder.setTitle("Deseja inserir na lista?");
            builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(getApplicationContext(), "Produto inserido na lista.", Toast.LENGTH_LONG).show();
                }
            });
            builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    alerta.cancel();
                }
            });
            alerta = builder.create();
            alerta.show();

            return true;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    String VendasOnline = null;
    String ListaProdutos = null;
    switch (item.getItemId()){
        case R.id.login:
            onBackPressed();
            break;
        case R.id.lista:
            SharedPreferences lista = getSharedPreferences(PREFS_USER, 0);
            ListaProdutos = new String(lista.getString("PrefListaProdutos", ""));

            if (ListaProdutos.equals("N")) {
                Toast.makeText(getApplicationContext(), "Acesso negado!", Toast.LENGTH_SHORT).show();
            }else {
                Intent listaprodutos = new Intent(BuscaActivity.this, ListaActivity.class);
                startActivity(listaprodutos);
            }
            break;
        case R.id.vendas:
            SharedPreferences vendas = getSharedPreferences(PREFS_USER, 0);
            VendasOnline = new String(vendas.getString("PrefVendasOnline", ""));

            if (VendasOnline.equals("N")) {
               Toast.makeText(getApplicationContext(), "Acesso negado!", Toast.LENGTH_SHORT).show();
            }else {
                Intent venda = new Intent(this, VendasActivity.class);
                startActivity(venda);
            }
            break;
        case R.id.refresh:
            recreate();
            break;
        case R.id.scan:
            Intent scan = new Intent(BuscaActivity.this, BarCodeActivity.class);
            startActivity(scan);
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}

public void loadJson(String busca){

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://"+getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString("PrefHost", "") +":8080/FazendaWebservice/webresources/fazenda/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    ProdutoClient client = retrofit.create(ProdutoClient.class);
    Call<List<Produto>> call = client.reposForUser(busca);

    call.enqueue(new Callback<List<Produto>>() {
        @Override
        public void onResponse(Call<List<Produto>> call, Response<List<Produto>> response) {
            pbar.setVisibility(View.GONE);
            List<Produto> repos = response.body();
            listView.setAdapter(new ProdutoAdapter(BuscaActivity.this, repos));
            if(listView.getCount() == 0){
                Toast.makeText(getApplicationContext(), "Nenhum produto localizado.", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<List<Produto>> call, Throwable t) {
            pbar.setVisibility(View.INVISIBLE);
            Toast.makeText(BuscaActivity.this, "         Erro ao estabelecer conexão"+ "\n"+"            Verifique o host inserido"+"\n"+"Por favor tente novamente mais tarde!", Toast.LENGTH_SHORT).show();
        }
    });
}
    
asked by anonymous 18.09.2017 / 15:42

1 answer

1

Friend, pass values to other activitys using link for example:

Main activity:

//MainActivity.java
Intent it = new Intent(this, SegundaActivity.class);
it.putExtra("Informacao", "Olá sou uma informação passada de outra activity");
startActivity(it);

Activity that will receive the information:

//SegundaActivity.java

@Override
protected void onCreate(Bundle saveInstancestate){
    super.onCreate(saveInstanceState);
    setContentView(R.layout.main_activity);

    //Pega a intent de outra activity
    Intent it = getIntent();

    //Recuperei a string da outra activity
    String informacao = it.getStringExtra("Informacao");

    Log.i("Informacao: ", informacao);
}

I hope I have helped

    
18.09.2017 / 19:03