Entering data into the database through a Dialog

Asked

Viewed 134 times

0

I am creating a project that needs to create users, I have a ready method working but I am trying to put a way to display in a dialog.

I created a screen with a Recycler view to display the list of users, and when clicking the button add it calls this dialog with the fields, and when clicking add in the dialog it inserts the values in the database. however in the logs does not point any error, but nothing happens, and the method I implemented is based on what I myself mounted in a separate view.

public class Admin_Usuarios  extends Activity {


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_usuarios);
        EntradaApp.setContext(getApplicationContext());



    }// fim oncreate



  public void adicionarUsuarios(View view){
    CadastroUsuario_dialog.show(getSupportFragmentManager());

  }
}

this and the class that calls the dialog

public class CadastroUsuario_dialog extends DialogFragment{
    private static final String PREF_NAME = "Adicionar_UsuarioPreferences";
    String tipo;
    String senha;
    String nome;
    String usuario;
    String email;
    String id_tipo;
    Button btcriar;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());

        View view = inflater.inflate(R.layout.dialog_adicionar_usuario, null);


        ArrayAdapter adapter = new ArrayAdapter(getActivity() ,android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        List<String> tipos = new ArrayList<String>();
        tipos.add("Administração");
        tipos.add("Comum");


        Spinner spin1 = (Spinner) view.findViewById(R.id.sptipo_usuario);




        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, tipos);
        ArrayAdapter<String> spinnerArrayAdapter = arrayAdapter;
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spin1.setAdapter(spinnerArrayAdapter);
        spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int posicao, long id) {
                //pega nome pela posição
                tipo = parent.getItemAtPosition(posicao).toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



        return new AlertDialog.Builder(getActivity()).setView(view).create();


}


    public static void show(FragmentManager fragmentManager) {
    android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
        Fragment prev = fragmentManager.findFragmentByTag("editar_carro");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        CadastroUsuario_dialog frag = new CadastroUsuario_dialog();
        Bundle args = new Bundle();

        frag.show(ft, "Ajuda");
    }




}

this is the implemented dialog

public void adicionarusuario(View view ) {
        EditText etid_tipo = (EditText) view.findViewById(R.id.etid1);
        EditText etnome = (EditText) view.findViewById(R.id.etnome1);
        EditText etusuario = (EditText) view.findViewById(R.id.etusuario1);
        EditText etsenha = (EditText) view.findViewById(R.id.etsenha1);
        EditText etemail = (EditText) view.findViewById(R.id.etemail1);


        nome = etnome.getText().toString();
        email = etemail.getText().toString();
        usuario = etusuario.getText().toString();
        senha = etsenha.getText().toString();
        id_tipo = etid_tipo.getText().toString();


        Usuarios_Model u = new Usuarios_Model();

        u.setNome(nome);
        u.setEmail(email);
        u.setSenha(senha);
        u.setUsuario(usuario);
        u.setTipo_usuario(tipo);
        u.setId(Integer.parseInt(id_tipo));
        List<Usuarios_Model> ls = new ArrayList<>();
        ls.add(u);
        new DAO_usuario().Insert(ls);



    }

DAO OF THE SYSTEM

public class DAO_usuario extends DAO_Principal<Usuarios_Model> {
    @Override
    protected ContentValues getContentValues(Usuarios_Model object) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(("id"), object.getId());
        contentValues.put(("nome"), object.getNome());
        contentValues.put(("usuario"), object.getUsuario());
        contentValues.put(("senha"), object.getSenha());
        contentValues.put(("email"), object.getEmail());
        contentValues.put(("tipo_usuario"), object.getTipo_usuario());
        return  contentValues;
    }

    @Override
    protected Usuarios_Model FillObject(Cursor cursor) {
        Usuarios_Model usuarios_model = new Usuarios_Model();
        usuarios_model.setId(cursor.getInt(cursor.getColumnIndex("id")));
        usuarios_model.setNome(cursor.getString(cursor.getColumnIndex("nome")));
        usuarios_model.setUsuario(cursor.getString(cursor.getColumnIndex("usuario")));
        usuarios_model.setSenha(cursor.getString(cursor.getColumnIndex("senha")));
        usuarios_model.setEmail(cursor.getString(cursor.getColumnIndex("email")));
        usuarios_model.setTipo_usuario(cursor.getString(cursor.getColumnIndex("tipo_usuario")));
        return null;
    }

    @Override
    protected String getTable() {
        return "Usuario";
    }
    @Override
    public void Insert(List<Usuarios_Model> list) {
        super.Insert(list);
    }

    @Override
    public void Erase(){
        super.Erase();

    }

    public void Erase(int id, String condicao) {
        super.Erase(id, condicao);
    }

    @Override
    public List<Usuarios_Model> ListAll() {
        return super.ListAll();
    }

in a view the part works, however when putting in the dialog some error happens and I am not finding. Thanks in advance!

  • Tell me if I understand, you want to add users from a certain Dialog?

  • this, I would like to make the dialog work in the same way as Activity to enter data

  • I understood how I make it work , before and not in the dialog itself , when calling the method I insert the variables with end, so that when I call the dialog with the values I only receive the values and when I click the add it closes the dialog with a Ismiss and receives all the values in the same way as in the view!! Thank you!

1 answer

0

Answering the question I created.

I set values outside and let only load the layout in the dialog

public void ExibeDialog(){

    final Dialog dialog = new Dialog(this);

    dialog.setContentView(R.layout.dialog_adicionar_usuario);

    final Button btcriar = (Button) dialog.findViewById(R.id.btcriar);

    final EditText etnome = (EditText)dialog.findViewById(R.id.etnomed);

    final EditText etusuario = (EditText) dialog.findViewById(R.id.etusuariod);

    btcriar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            u.setNome(nome);
            u.setEmail(email);

            List<Usuarios_Model> ls = new ArrayList<>();
            ls.add(u);
            new DAO_usuario().Insert(ls);

            dialog.dismiss();

            finish();
            startActivity(getIntent());// " finish(); + startActivity funcionam como um refresh

            Toast.makeText(Admin_Usuarios.this, "Usuario Criado!", Toast.LENGTH_SHORT).show();
        }
    );
}

Browser other questions tagged

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