Passing parameter to another Android Activity

Asked

Viewed 5,530 times

0

I created a QRCode and I 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 leave, I would like that when I clicked on Ok it was thrown to another Activity where inside this activity have a EditText for search, which in the case is already working if I write what I seek, now I want as soon as I pull the value in QRCode he takes this amount and looks for me what I want...

QRCODE output 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 you to search:

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();
        }
    });
}

1 answer

1

Friend, pass values to other activitys using https://developer.android.com/reference/android/content/Intent.html 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’ve helped

  • Man thanks for the help, but I managed to solve another way, I ended up using getSharedPreference, so every time my qrcode reads the value I write it and play in edittext so I click on search and it searches.

Browser other questions tagged

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