0

I'm stuck for few days, and unfortunately dont know how to fix it. I have DetailScreen.java that show data from MainScreen (one of recyclerview's list).

  1. I tried this code but there is no effect, when i try to change use COLUMN_NOMOR in my update function, i can change the name but getting error when change the nomor. I guess, its right to use COLUMN_ID in my update function because id is my PK-AI. So whether because I didnt call COLUMN_ID in my DetailScreen? But how i can call my id in my DetailsScreen also in my UpdateScreen?

  2. If the data success updated, the screen will be automatically go to the DetailScreen again, but still showing old data. I had to go to MainActivity first to get the new data. Where code should i modified?

Here my Update function in my DBHandler.java

public int updateDataSiswa(Siswa siswa) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COLUMN_NOMOR, siswa.getNomor());
    values.put(COLUMN_NAMA, siswa.getNama());

    return db.update(TABLE_SISWA, values, COLUMN_ID + " = ?",
            new String[]{String.valueOf(siswa.getId())});
}

Here DetailScreen.java

public class DetailScreen extends AppCompatActivity {
        private TextView txt_resultnomor, txt_resultnama;

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

        //put data
        Intent i = getIntent();
        final String id = i.getExtras().getString("id");
        final String nomor = i.getExtras().getString("nomor");
        final String nama = i.getExtras().getString("nama");

        //inisialisasi
        txt_resultnomor = (TextView) findViewById(R.id.txt_resultnomor);
        txt_resultnama = (TextView) findViewById(R.id.txt_resultnama);

        //set 
        txt_resultnomor.setText(nomor);
        txt_resultnama.setText(nama);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.menu_detail, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
       int id = item.getItemId();

        if (id == R.id.action_edit) {
            Intent in = new Intent(this, UpdateScreen.class);
            in.putExtra("nomor", txt_resultnomor.getText().toString());
            in.putExtra("nama", txt_resultnama.getText().toString());
            startActivity(in);
        }
        if (id == R.id.action_delete) {
            return true;
        }
        if (id == R.id.action_settings) {
            // Intent i = new Intent(this, About.class);
            //startActivity(i);
        }

        return super.onOptionsItemSelected(item);
    }



}

Then here my UpdateScreen.java

public class UpdateScreen extends AppCompatActivity {

    private EditText et_nomor;
    private EditText et_nama;

    private Button button_updatedata;

    private DBHandler dbHandler;
    private SiswaAdapter adapter;

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

        dbHandler = new DBHandler(this);
        initComponents();
    }
    private void initComponents(){
        et_nomor = (EditText) findViewById(R.id.et_nomor);
        et_nama = (EditText) findViewById(R.id.et_nama);

        //ambil data
        Intent in=getIntent();
        final String id=in.getExtras().getString("id_siswa");
        final String nomor=in.getExtras().getString("nomor");
        final String nama=in.getExtras().getString("nama");

//set inisial 
        et_nomor.setText(nomor);
        et_nama.setText(nama);

        button_updatedata = (Button) findViewById(R.id.button_updatedata);
        button_updatedata.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                validasiForm();
            }
        });
    }

    private void validasiForm() {
        String form_nomor = et_nomor.getText().toString();
        String form_nama = et_nama.getText().toString();

        if (form_nomor.isEmpty()){
            et_nomor.setError("Nomor belum diisi");
            et_nomor.requestFocus();
        }
        else if (form_nama.isEmpty()){
            et_nama.setError("Nama belum diisi");
            et_nama.requestFocus();
        }
        else {
            dbHandler.updateDataSiswa(new Siswa(form_nomor, form_nama));

            List<Siswa> siswaList = dbHandler.getSemuaSiswa();
            adapter = new SiswaAdapter(siswaList);
            adapter.notifyDataSetChanged();
            Toast.makeText(UpdateScreen.this, "Berhasil Memperbarui Data Siswa", Toast.LENGTH_SHORT).show();
            MainSiswaActivity.mma.refresh();
            finish();
        }
    }
}

I hope i tell clearly, help me please. Thanks.

Riris
  • 23
  • 5

1 Answers1

1

where is your call to the update method you given public int updateDataSiswa(Siswa siswa)..? Also based on what you have provided...your notifyDataSetChanged() for adapter just tries to update the listview of the RecyclerView.. but it does not understand what underlying data has changed for it..you need to create a method for your Database Handler where you can fetch a List of new records..and then use this method to update your adapter with it and then set the new adapter to your listView in validasiForm() method.. please check this for more details...Android ListView not refreshing after notifyDataSetChanged

Community
  • 1
  • 1
saurabh169
  • 499
  • 1
  • 4
  • 12
  • sorry, i edited my code. I call `updateDataSiswa` in my UpdateScreen. Can u lead me first to fix my update code, pls? where i make the mistake :( @saurabh – Riris Jan 10 '17 at 10:08