how to modify or update data of an item in a listview in another activity

0

How to make an item in the Android listview open a "different activity" with all the data of the selected user and then make an update?

public class PersonaFisica {
    public String idPF;
    public String namePF;
    public String rfcPF;
    public String fnPF;
    public String lnPF;
    public String curpPF;
    public String namecalle;
    public int noI;
    public int noE;
    public String coloniaPF;
    public String dm;
    public String state;
    public int codigP;
    public int antigue;
    public String homePF;
    public String tel1;
    public String tel2;
    public String tel;
    public String email;
    public String nacionalidadPF;
    public String sexo;
    public String estadoCivil;
    public String calleConyuge;
    public int noIConyuge;
    public int noECntugue;
    public String coloniaConyuge;
    public String delegacionConyuge;
    public String estadoConyuge;
    public int cpConyuge;
    public int antigueConyuge;


    public PersonaFisica(){

    }

    public PersonaFisica(String idPF, String namePF, String rfcPF, String fnPF, String lnPF, String curpPF, String namecalle, int noI, int noE, String coloniaPF, String dm, String state, int codigP,
                         int antigue, String homePF, String tel1, String tel2, String tel, String email, String nacionalidadPF, String calleConyuge, int noIConyuge, int noECntugue,
                         String coloniaConyuge, String delegacionConyuge, String estadoConyuge, int cpConyuge, int antigueConyuge) {
        this.idPF = idPF;
        this.namePF = namePF;
        this.rfcPF = rfcPF;
        this.fnPF = fnPF;
        this.lnPF = lnPF;
        this.curpPF = curpPF;
        this.namecalle = namecalle;
        this.noI = noI;
        this.noE = noE;
        this.coloniaPF = coloniaPF;
        this.dm = dm;
        this.state = state;
        this.codigP = codigP;
        this.antigue = antigue;
        this.homePF = homePF;
        this.tel1 = tel1;
        this.tel2 = tel2;
        this.tel = tel;
        this.email = email;
        this.nacionalidadPF = nacionalidadPF;
        this.calleConyuge = calleConyuge;
        this.noIConyuge = noIConyuge;
        this.noECntugue = noECntugue;
        this.coloniaConyuge = coloniaConyuge;
        this.delegacionConyuge = delegacionConyuge;
        this.estadoConyuge = estadoConyuge;
        this.cpConyuge = cpConyuge;
        this.antigueConyuge = antigueConyuge;
    }

    public String getIdPF() {
        return idPF;
    }

    public void setIdPF(String idPF) {
        this.idPF = idPF;
    }

    public String getNamePF() {
        return namePF;
    }

    public void setNamePF(String namePF) {
        this.namePF = namePF;
    }

    public String getRfcPF() {
        return rfcPF;
    }

    public void setRfcPF(String rfcPF) {
        this.rfcPF = rfcPF;
    }

    public String getFnPF() {
        return fnPF;
    }

    public void setFnPF(String fnPF) {
        this.fnPF = fnPF;
    }

    public String getLnPF() {
        return lnPF;
    }

    public void setLnPF(String lnPF) {
        this.lnPF = lnPF;
    }

    public String getCurpPF() {
        return curpPF;
    }

    public void setCurpPF(String curpPF) {
        this.curpPF = curpPF;
    }

    public String getNamecalle() {
        return namecalle;
    }

    public void setNamecalle(String namecalle) {
        this.namecalle = namecalle;
    }

    public int getNoI() {
        return noI;
    }

    public void setNoI(int noI) {
        this.noI = noI;
    }

    public int getNoE() {
        return noE;
    }

    public void setNoE(int noE) {
        this.noE = noE;
    }

    public String getColoniaPF() {
        return coloniaPF;
    }

    public void setColoniaPF(String coloniaPF) {
        this.coloniaPF = coloniaPF;
    }

    public String getDm() {
        return dm;
    }

    public void setDm(String dm) {
        this.dm = dm;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getCodigP() {
        return codigP;
    }

    public void setCodigP(int codigP) {
        this.codigP = codigP;
    }

    public int getAntigue() {
        return antigue;
    }

    public void setAntigue(int antigue) {
        this.antigue = antigue;
    }

    public String getHomePF() {
        return homePF;
    }

    public void setHomePF(String homePF) {
        this.homePF = homePF;
    }

    public String getTel1() {
        return tel1;
    }

    public void setTel1(String tel1) {
        this.tel1 = tel1;
    }

    public String getTel2() {
        return tel2;
    }

    public void setTel2(String tel2) {
        this.tel2 = tel2;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNacionalidadPF() {
        return nacionalidadPF;
    }

    public void setNacionalidadPF(String nacionalidadPF) {
        this.nacionalidadPF = nacionalidadPF;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getEstadoCivil() {
        return estadoCivil;
    }

    public void setEstadoCivil(String estadoCivil) {
        this.estadoCivil = estadoCivil;
    }

    public String getCalleConyuge() {
        return calleConyuge;
    }

    public void setCalleConyuge(String calleConyuge) {
        this.calleConyuge = calleConyuge;
    }

    public int getNoIConyuge() {
        return noIConyuge;
    }

    public void setNoIConyuge(int noIConyuge) {
        this.noIConyuge = noIConyuge;
    }

    public int getNoECntugue() {
        return noECntugue;
    }

    public void setNoECntugue(int noECntugue) {
        this.noECntugue = noECntugue;
    }

    public String getColoniaConyuge() {
        return coloniaConyuge;
    }

    public void setColoniaConyuge(String coloniaConyuge) {
        this.coloniaConyuge = coloniaConyuge;
    }

    public String getDelegacionConyuge() {
        return delegacionConyuge;
    }

    public void setDelegacionConyuge(String delegacionConyuge) {
        this.delegacionConyuge = delegacionConyuge;
    }

    public String getEstadoConyuge() {
        return estadoConyuge;
    }

    public void setEstadoConyuge(String estadoConyuge) {
        this.estadoConyuge = estadoConyuge;
    }

    public int getCpConyuge() {
        return cpConyuge;
    }

    public void setCpConyuge(int cpConyuge) {
        this.cpConyuge = cpConyuge;
    }

    public int getAntigueConyuge() {
        return antigueConyuge;
    }

    public void setAntigueConyuge(int antigueConyuge) {
        this.antigueConyuge = antigueConyuge;
    }

    @Override
    public String toString() {
        return "RFC: " +rfcPF + "\nNombre: "+namePF + "\nDirección: " +namecalle + "\nColonia: " +coloniaPF;
    }

}










package com.example.myfirstapp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.myfirstapp.modelo.PersonaFisica;
import com.example.myfirstapp.modelo.PersonaMoral;
import com.example.myfirstapp.modelo.activityInformation;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class ModificacionesPFActivity extends AppCompatActivity {

    ListView listV_datosPF;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;

    private List<PersonaFisica> listActivi = new ArrayList<PersonaFisica>();
    private ArrayAdapter<PersonaFisica> arrayAdapterActivi;

    PersonaFisica personaFisicaseleccionada;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modificaciones_pf);

        listV_datosPF = (ListView) findViewById(R.id.database_list_viewPF);
        eventoDatabase();
        listV_datosPF.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                personaFisicaseleccionada = (PersonaFisica) parent.getItemAtPosition(position);
                parent.getChildAt(position).setBackgroundColor(Color.parseColor("#CEE3F6"));

            }
        });
    }

    private void eventoDatabase() {
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference();
        databaseReference.child("PersonaFisica").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                listActivi.clear();
                for (DataSnapshot objSnapshot:dataSnapshot.getChildren()){
                    PersonaFisica p = objSnapshot.getValue(PersonaFisica.class);
                    listActivi.add(p);
                }
                arrayAdapterActivi = new ArrayAdapter<PersonaFisica>(ModificacionesPFActivity.this, android.R.layout.simple_list_item_1, listActivi);
                listV_datosPF.setAdapter(arrayAdapterActivi);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

         if(id == R.id.menu_actualiza) {
             startActivity(new Intent(ModificacionesPFActivity.this, 
             MD_PersonaFisicaActivity.class));
             PersonaFisica p = new PersonaFisica();
             MD_PersonaFisicaActivity newFrame = new MD_PersonaFisicaActivity();
             p.setIdPF( personaFisicaseleccionada.getIdPF());
             newFrame.edtNamePF.(p.getNamePF());

             newFrame.edtRfcPF.setText(p.getRfcPF());
             newFrame.edtFN.setText(p.getFnPF());
             newFrame.edtLN.setText(p.getLnPF());
             newFrame.edtCurp.setText(p.getCurpPF());
             newFrame.edtCalle.setText(p.getNamecalle());
             newFrame.edtNI.setText(p.getNoI());
             newFrame.edtNE.setText(p.getNoE());
             newFrame.edtColonia.setText(p.getColoniaPF());
             newFrame.edtDM.setText(p.getDm());
             newFrame.edtState.setText(p.getState());
             newFrame.edtCP.setText(p.getCodigP());
             newFrame.edtAnti.setText(p.getAntigue());
             newFrame.edtHomeTel.setText(p.getHomePF());
             newFrame.edtTel1.setText(p.getTel1());
             newFrame.edtTel2.setText(p.getTel2());
             newFrame.edtTel.setText(p.getTel());
             newFrame.edtEmailPF.setText(p.getEmail());
             newFrame.edtNacion.setText(p.getNacionalidadPF());
             newFrame.edtCalleCony.setText(p.getCalleConyuge());
             newFrame.edtNIConyu.setText(p.getNoIConyuge());
             newFrame.edtNEConyu.setText(p.getNoECntugue());
             newFrame.edtColoniConyu.setText(p.getColoniaConyuge());
             newFrame.edtDelegacionConyu.setText(p.getDelegacionConyuge());
             newFrame.edtEstadoConyu.setText(p.getEstadoConyuge());
             newFrame.edtCPConyu.setText(p.getCpConyuge());
             newFrame.etdAntigueConyugue.setText(p.getAntigueConyuge());

    databaseReference.child("PersonaFisica").child(p.getIdPF()).setValue(p);



        }else if(id == R.id.menu_deleta) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setMessage("Desea eliminar está persona");
            alertDialog.setPositiveButton("Si", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    PersonaFisica p = new PersonaFisica();
                    p.setIdPF(personaFisicaseleccionada.getIdPF());
                    databaseReference.child("PersonaFisica").child(p.getIdPF()).removeValue();
                }
            });

            alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            alertDialog.show();
        }
        //return true;
        return super.onOptionsItemSelected(item);
    }
}

AND SEND ME THIS ERROR

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.example.myfirstapp.ModificacionesPFActivity.onOptionsItemSelected(ModificacionesPFActivity.java:97)
    at android.app.Activity.onMenuItemSelected(Activity.java:3383)
    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
    at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
    at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
    at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:674)
    at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
    at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
    at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
    at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
    at android.view.View.performClick(View.java:6261)
    at android.widget.TextView.performClick(TextView.java:11159)
    at android.view.View$PerformClick.run(View.java:23748)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
    
asked by Kar 25.07.2018 в 00:16
source

1 answer

0

By clicking on the item in the list and get the position where you click, you only take your user data with putExtra , and then within the other Activity with the user's data, you can already modify what you want To modify the user that you are clicking, you will have to take the user id of that user and then pass it to the reference that will allow you to do a setValue() or updateValue() in Firebase

For example the following snippet

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                try{
                                    mDatabase = mAdapter.getRef(position);
                                    Intent intent = new Intent(MainActivity.this, UserEdit.class);
                                    intent.putExtra("uid",mDatabase.getKey());
                                    intent.putExtra("Nombre", mAdapter.getItem(position).getNombre());
                                    intent.putExtra("Email", mAdapter.getItem(position).getEmail());
                                    intent.putExtra("Pago", mAdapter.getItem(position).getPago());
                                    intent.putExtra("UConexion", mAdapter.getItem(position).getUConexion());
                                    intent.putExtra("PConexion", mAdapter.getItem(position).getPConexion());
                                    startActivity(intent);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        });

and then in your other Activity you get that extra and you work with the data of that user, you are going through extra, it is the uid of the user that you want to modify the data in the other Activity

 Intent iin= getIntent();
        Bundle b = iin.getExtras();

        if(b!=null)
        {

            String n =(String) b.get("Nombre");
            final String e =(String) b.get("Email");
            String p =(String) b.get("Pago");
            String Uconexion =(String) b.get("UConexion");
            String PConexion =(String) b.get("PConexion");
            String Dispositivo =(String) b.get("Dispositivo");
            uid =(String) b.get("uid");


            mDatabase.child("Usuarios").child(uid).child("turef").addValueEventListener(new ValueEventListener() {
                final List<String> LstIdioma = new ArrayList<String>();

                @Override
                public void onDataChange(final DataSnapshot dataSnapshot) {

                  //obtene los datos del usuario por ej

                    }

This is a snippet of a code of mine, but it serves you in the same way as you need

    
answered by 25.07.2018 в 14:08