2017-07-15 7 views
0

sqliteからデータを更新および削除するアクティビティを作成します。最初のアプリケーションでは、sqliteのデータを削除または更新する警告ダイアログが表示されますが、データベースを更新または削除すると、動作しません。AlertDialog内のSQLite操作が機能しない

これは私のアダプターのコードです:

adapter.setOnClickListener(new OnItemClickListener() { 
      @Override 
      public void onClick(View view, int position) { 
       DataFormNotaPesanan ambil = data.get(position); 
       final String kodebarang = ambil.getKode(); 
       System.out.println("Kode barang: " + kodebarang); 
       CharSequence pilihan[] = new CharSequence[] {"Ubah Jumlah", "Hapus"}; 
       AlertDialog.Builder alert = new AlertDialog.Builder(EditFormNotaPesanan.this); 
       alert.setTitle("Pilih Tindakan"); 
       alert.setItems(pilihan, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         if(which==0){ 
          AlertDialog.Builder ubah = new AlertDialog.Builder(EditFormNotaPesanan.this); 
          View tampil = EditFormNotaPesanan.this.getLayoutInflater().inflate(R.layout.editjumlah, null); 
          ubah.setTitle("Masukkan Jumlah"); 
          ubah.setView(tampil); 
          final EditText jumlah = (EditText)tampil.findViewById(R.id.jumlah); 
          ubah.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            String jumlahpesanan = jumlah.getText().toString(); 
            if(jumlahpesanan.equals("")){ 
             jumlah.setError("Can't blank"); 
            } 
            else{ 
             helper.ubahJumlahNotaPesanan(kodebarang, jumlahpesanan); 
             adapter.notifyDataSetChanged(); 
            } 
           } 
          }); 
          ubah.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 

           } 
          }); 

          ubah.show(); 
         } 
         else if(which==1){ 
          AlertDialog.Builder konfirmasi = new AlertDialog.Builder(EditFormNotaPesanan.this); 
          konfirmasi.setTitle("Delete Data?"); 
          konfirmasi.setMessage("Are You Sure Delete: " + helper.getNamaBarang(kodebarang)); 
          konfirmasi.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            helper.hapusBarangPesanan(kodebarang); 
           } 
          }); 
          konfirmasi.setNegativeButton("Batal", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 

           } 
          }); 
          konfirmasi.show(); 
         } 
        } 
       }); 
       alert.setNegativeButton("Batal", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }); 
       alert.show(); 
      } 
     }); 

そして、これは私のDataBaseHelperです:事前に

public void ubahJumlahNotaPesanan(String kodebarang, String jumlah){ 
     String query = "update tmp_orderd set jumlah='" + jumlah + "' where ref_barang='" + kodebarang + "'"; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.execSQL(query); 
    } 

    public void hapusBarangPesanan(String kodebarang){ 
     String query = "delete from tmp_orderd where ref_barang='" + kodebarang + "'"; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.execSQL(query); 
    } 

おかげ

+0

_ "動作しません" _ – sHOLE

+0

あなたの操作の後で、あなたのアプリを開いた後に何が起こるのですか? –

+0

@intelliJAmiya何も起こりません、データは同じです –

答えて

1

UはnotifyDataSetChangedメソッドを使用していました。このメソッドは、arrylistにオブジェクトを追加または削除するときにのみトリガされます。ここでuを削除するかsqliteに新しいデータを挿入すると、notifyDataSetChangedメソッドをトリガするためにarrylistを追加または削除したり、sqliteからデータを再度取得してデータを取り込んだりする必要があります。

+0

hai、sir。ご協力いただきありがとうございます。その仕事!私の間違いは私のアダプタを更新する必要があります –

+0

notifyDataSetChangedがうまくいけば、urアダプタを更新する必要はありません。それは自動的にUUアダプターを更新します。動作しない場合は、あなたはUUアダプタを更新する必要があります。 –

関連する問題