Vinicius Biscolla
Vinicius Biscolla

Reputation: 49

Pass Value from ListView To EditText

I get an activity which one has 3 Clickables EditTexts which are 3 differents Searchables ListViews with MatchCode by ButtonClick starts a new activity

but when i get for example the first value(1° EditText) on ListView it appears in the EditText(MainActivity) how is expected and when i call the 2nd and 3rd EditText which one are other clickable and get the values, erase all the others EditTexts in the activity any ideas why this is happenning ??

I know that i might need to treat the actions with an intent to set and get the values, but i'm in trouble with that so i'm posting the code and the photo to let you guys help me,

probably its some simple solution that i don't know or with i'm not able to do that, tell me how can i get a solution for that...

Here is a photo with whats is happening:

enter image description here

And here is my code in my main activity

public class Formulario_ItemNota extends Formulario_Nota {
private ListView listaProblemas;
private ItemNota_Helper itemnota_helper;
private List<ItemNota> itemnotas;
private ItemNota itemnotaSelecionado;
private Intent int_conjunto;
private Intent int_problema;

private EditText ETconjunto;
private EditText ETproblema;

protected void onCreate(Bundle savedInstanceState) {
    System.out.println("Daniel: onCreate" + new Date());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.form_itemnota);

    this.itemnota_helper = new ItemNota_Helper(this);

    ETconjunto = (EditText) findViewById(R.id.ET_conjunto);
    ETconjunto.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            Intent material = new Intent(Formulario_ItemNota.this, ListaMateriais.class);
            startActivity(material);
        }
    });

    ETproblema = (EditText) findViewById(R.id.ET_problemas);
    ETproblema.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            Intent problema = new Intent(Formulario_ItemNota.this, ListaProblemas.class);
            startActivity(problema);
        }
    });

    final EditText ETprocedencia = (EditText) findViewById(R.id.ET_procedencia);
    ETprocedencia.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            Intent procedencia = new Intent(Formulario_ItemNota.this, ListaProcedencia.class);
            startActivity(procedencia);
        }
    });

    Intent int_conjunto = getIntent();
    String tEXT_conjunto = int_conjunto.getStringExtra("TextMateriais");
    EditText et_conj = (EditText) findViewById(R.id.ET_conjunto);
    System.out.println("conjunto: " + et_conj);
    ETconjunto.setText(tEXT_conjunto, TextView.BufferType.EDITABLE);

    Intent int_problema = getIntent();
    String tEXT_problema = int_problema.getStringExtra("TextProblemas");
    EditText et_problema = (EditText) findViewById(R.id.ET_problemas);
    System.out.println("problema: " + et_problema);
    ETproblema.setText(tEXT_problema, TextView.BufferType.EDITABLE);

    Intent int_procedencia = getIntent();
    String tEXT_procedencia = int_procedencia.getStringExtra("TextProcedencia");
    EditText et_procedencia = (EditText) findViewById(R.id.ET_procedencia);
    et_procedencia.setText(tEXT_procedencia, TextView.BufferType.EDITABLE);

    Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
    itemnotas = ItemNotaDao.getListaIN();
    ItemNotaDao.close();

    ArrayAdapter<ItemNota> adapter = new ArrayAdapter<ItemNota>(this, android.R.layout.simple_list_item_1, itemnotas);
    listaProblemas = (ListView) findViewById(R.id.lista_descr_problemas);
    listaProblemas.setAdapter(adapter);
    registerForContextMenu(listaProblemas);

}

private void carregaListaItemNota() {
    Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
    List<ItemNota> itensnotas = ItemNotaDao.getListaIN();
    ItemNotaDao.close();

    ArrayAdapter<ItemNota> adapter = new ArrayAdapter<ItemNota>(this, android.R.layout.simple_list_item_1, itensnotas);
    this.listaProblemas.setAdapter(adapter);
}

public void onCreateContextMenu(ContextMenu menuItem, View vItem, ContextMenuInfo menuInfoItem) {

    super.onCreateContextMenu(menuItem, vItem, menuInfoItem);
    listaProblemas = (ListView) findViewById(R.id.lista_descr_problemas);
    MenuItem editar = menuItem.add("Editar");
    editar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            return true;
        }
    });

    MenuItem deletar = menuItem.add("Deletar");
    deletar.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item1) {
            new AlertDialog.Builder(Formulario_ItemNota.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Deletar").setMessage("Deseja mesmo deletar ?")
                    .setPositiveButton("Sim", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
                            ItemNotaDao.deletar(itemnotaSelecionado);
                            ItemNotaDao.close();
                            carregaListaItemNota();
                        }
                    }).setNegativeButton("Não", null).show();
            return true;
        }
    });
}

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

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.addItem:
        ItemNota itemnota = itemnota_helper.pegaItemNotaDoFormulario();

        Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
        itemnotas = ItemNotaDao.getListaIN();
        ItemNotaDao.insereItemNota(itemnota);
        Toast.makeText(Formulario_ItemNota.this, "Dados Salvos com sucesso", Toast.LENGTH_LONG).show();
        carregaListaItemNota();
        ItemNotaDao.close();
        return true;

    case R.id.CriarOrdem:
        Intent intentl = new Intent(Formulario_ItemNota.this, Formulario_OS.class);
        startActivity(intentl);
        return true;

    default:

        return super.onOptionsItemSelected(item);
    }
}

}

And here is one of my 3 ListViews which are the same with only different values;

public class ListaMateriais extends Activity {

    private ListView listamateriais;
    ArrayAdapter<String> adapMatchMaterial;
    EditText inputSearch;
    public EditText ETclient; 
    ArrayList<HashMap<String, String>> productList;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_material_match);

        String products[] = { "Parafuso 3x4 092387",
                          "Cabeçote redpo 09873", 
                          "alavanca de ignição 027625",
                          "Pistão de arranque 093298092", 
                          "Eixo dianteiro 0343232",
                          "Cabeçote parafuseta 093232" };
        listamateriais = (ListView) findViewById(R.id.list_match_material);
        adapMatchMaterial = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        listamateriais.setAdapter(adapMatchMaterial);

        listamateriais.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position,  long arg3) {
                String text= (String) arg0.getItemAtPosition(position);
                Bundle bundle = new Bundle();
                bundle.putString("TextMateriais", text);
                Intent int_material = new Intent(ListaMateriais.this,Formulario_ItemNota.class);
                int_material.putExtras(bundle);
                startActivity(int_material);
                finish();
            }
        });

        inputSearch = (EditText) findViewById(R.id.materialSearch); 
        inputSearch.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                ListaMateriais.this.adapMatchMaterial.getFilter().filter(cs);
            }
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
            public void afterTextChanged(Editable arg0) {
            }
        });
    }

Any helps will be truly appreciate !!!O

Upvotes: 2

Views: 2051

Answers (1)

Rami
Rami

Reputation: 7929

After selecting an item from your list, you are opening another instance of Formulario_ItemNota. This is why you loose your editText data.

try this:

 public class Formulario_ItemNota extends Formulario_Nota {
    private ListView listaProblemas;
    private ItemNota_Helper itemnota_helper;
    private List<ItemNota> itemnotas;
    private ItemNota itemnotaSelecionado;
    private Intent int_conjunto;
    private Intent int_problema;

    private EditText ETconjunto;
    private EditText ETproblema;
    private EditText ETprocedencia;

    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("Daniel: onCreate" + new Date());

        super.onCreate(savedInstanceState);
        setContentView(R.layout.form_itemnota);

        this.itemnota_helper = new ItemNota_Helper(this);

        ETconjunto = (EditText) findViewById(R.id.ET_conjunto);
        ETconjunto.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Intent material = new Intent(Formulario_ItemNota.this, ListaMateriais.class);
                startActivityForResult(material, 1);
            }
        });

        ETproblema = (EditText) findViewById(R.id.ET_problemas);
        ETproblema.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Intent problema = new Intent(Formulario_ItemNota.this, ListaProblemas.class);
                startActivityForResult(problema , 2);
            }
        });

        ETprocedencia = (EditText) findViewById(R.id.ET_procedencia);
        ETprocedencia.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Intent procedencia = new Intent(Formulario_ItemNota.this, ListaProcedencia.class);
                startActivityForResult(procedencia , 3);
            }
        });

        Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
        itemnotas = ItemNotaDao.getListaIN();
        ItemNotaDao.close();

        ArrayAdapter<ItemNota> adapter = new ArrayAdapter<ItemNota>(this, android.R.layout.simple_list_item_1, itemnotas);
        listaProblemas = (ListView) findViewById(R.id.lista_descr_problemas);
        listaProblemas.setAdapter(adapter);
        registerForContextMenu(listaProblemas);

    }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {// back from ListaMateriais.class
        if(resultCode == RESULT_OK){
            String resultFromMateriais=data.getStringExtra("TextMateriais");
            ETconjunto.setText(resultFromMateriais);
        }
    } else if (requestCode == 2) { // back from ListaProblemas.class
        if(resultCode == RESULT_OK){
            String resultFromProblemas=data.getStringExtra("TextProblemas");
            ETproblema.setText(resultFromProblemas);
        }
    } else if (requestCode == 3) { // back from  ListaProcedencia.class
        if(resultCode == RESULT_OK){
            String resultFromProcedencia=data.getStringExtra("TextProcedencia");
            ETprocedencia.setText(resultFromProcedencia);
        }
    }
  }

    private void carregaListaItemNota() {
        Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
        List<ItemNota> itensnotas = ItemNotaDao.getListaIN();
        ItemNotaDao.close();

        ArrayAdapter<ItemNota> adapter = new ArrayAdapter<ItemNota>(this, android.R.layout.simple_list_item_1, itensnotas);
        this.listaProblemas.setAdapter(adapter);
    }

    public void onCreateContextMenu(ContextMenu menuItem, View vItem, ContextMenuInfo menuInfoItem) {

        super.onCreateContextMenu(menuItem, vItem, menuInfoItem);
        listaProblemas = (ListView) findViewById(R.id.lista_descr_problemas);
        MenuItem editar = menuItem.add("Editar");
        editar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                return true;
            }
        });

        MenuItem deletar = menuItem.add("Deletar");
        deletar.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item1) {
                new AlertDialog.Builder(Formulario_ItemNota.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Deletar").setMessage("Deseja mesmo deletar ?")
                        .setPositiveButton("Sim", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
                                ItemNotaDao.deletar(itemnotaSelecionado);
                                ItemNotaDao.close();
                                carregaListaItemNota();
                            }
                        }).setNegativeButton("Não", null).show();
                return true;
            }
        });
    }

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

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.addItem:
            ItemNota itemnota = itemnota_helper.pegaItemNotaDoFormulario();

            Stara_DB ItemNotaDao = new Stara_DB(Formulario_ItemNota.this);
            itemnotas = ItemNotaDao.getListaIN();
            ItemNotaDao.insereItemNota(itemnota);
            Toast.makeText(Formulario_ItemNota.this, "Dados Salvos com sucesso", Toast.LENGTH_LONG).show();
            carregaListaItemNota();
            ItemNotaDao.close();
            return true;

        case R.id.CriarOrdem:
            Intent intentl = new Intent(Formulario_ItemNota.this, Formulario_OS.class);
            startActivity(intentl);
            return true;

        default:

            return super.onOptionsItemSelected(item);
        }
    }
}

and in your 3 ListActivities, you must change them like that:

listamateriais.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position,  long arg3) {
                String text= (String) arg0.getItemAtPosition(position);

                Intent returnIntent = new Intent();
                returnIntent.putExtra("result",result);
                setResult(RESULT_OK, returnIntent);
                finish();
            }
        });

Upvotes: 2

Related Questions