Firebase removeValue()

时间:2018-07-23 18:11:18

标签: java android firebase firebase-realtime-database

我在编写代码以删除数据库时遇到问题。 我想删除它:

image1

在这种情况下,我不知道如何使用.child()。我现在的代码是:

    private void deleteKryterium(String kryteriumId) {
    DatabaseReference drKryteria = FirebaseDatabase.getInstance().getReference().child(kryteriumId);

    drKryteria.child(kryteriumId).removeValue();

    Toast.makeText(this,"Kryterium zostało usunięte", Toast.LENGTH_LONG).show();

}

但这是错误的。你能帮助我吗?我从教程中编写的大部分代码都是绿色的。 在另一端,用于更新此child的代码可以正常工作:

private boolean updateKryterium(String id, String name, int rating){

    Kryterium kryterium = new Kryterium(id, name, rating);

    databaseKryteria.child(id).setValue(kryterium);

    Toast.makeText(this, "Nazwa kryterium zaktualizowana", Toast.LENGTH_LONG).show();

    return true;

2 个答案:

答案 0 :(得分:0)

您未指向要删除的正确参考。

该孩子的路径应始于:

DatabaseReference drKryteria = FirebaseDatabase.getInstance().getReference().child("piwo_kryteria");

然后在那一点上,您应将键-LI785bPiLU9Z2qEe35o移至子级,然后最后移至要删除的子级(ID为kryteriumId)。 您应该像对kyteriumId一样知道中间键的值。

请注意,您还可以使用1个单个child()函数向其传递完整路径,在该路径中,每个节点都用正斜杠分隔,例如:

.child("piwo_kryteria/" + intermediate_key + "/" + kryteriumId)

在该引用上,您将调用removeValue()或setValue(null)。

答案 1 :(得分:0)

这是目前的代码,当然,在删除项目时无法正常工作:

public class Piwo_AddKryterium extends AppCompatActivity {


TextView textViewAlkoholName, textViewRating;
SeekBar seekBarRating;
Spinner spinnerKryteria;
Button buttonAddKryterium;
ListView listViewKryteria;
DatabaseReference databaseKryteria;
List<Kryterium> kryteria;

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

    textViewAlkoholName = (TextView) findViewById(R.id.textViewAlkoholName);
    seekBarRating = (SeekBar) findViewById(R.id.seekBarRating);
    textViewRating = (TextView) findViewById(R.id.textViewRating);
    buttonAddKryterium = (Button) findViewById(R.id.buttonAddKryterium);
    listViewKryteria = (ListView) findViewById(R.id.listViewKryteria);
    spinnerKryteria = (Spinner) findViewById(R.id.spinnerKryteria);

    Intent intent = getIntent();

    kryteria = new ArrayList<>();

    String id = intent.getStringExtra(Piwo_Program.ALKOHOL_ID);
    String name = intent.getStringExtra(Piwo_Program.ALKOHOL_NAME);

    textViewAlkoholName.setText(name);

    databaseKryteria = FirebaseDatabase.getInstance().getReference("piwo_kryteria").child(id);

    buttonAddKryterium.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            saveKryterium();
        }
    });
    seekBarRating.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            textViewRating.setText(String.valueOf(i));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    listViewKryteria.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        }
    });

    listViewKryteria.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

            Kryterium kryterium = kryteria.get(i);

            showUpdateDialog(kryterium.getKryteriumId(), kryterium.getKryteriumName());

            return true;
        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    databaseKryteria.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            kryteria.clear();

            for(DataSnapshot kryteriumSnapshot : dataSnapshot.getChildren()){
                Kryterium kryterium = kryteriumSnapshot.getValue(Kryterium.class);
                kryteria.add(kryterium);
            }

            KryteriumList kryteriumListAdapter = new KryteriumList(Piwo_AddKryterium.this, kryteria);
            listViewKryteria.setAdapter(kryteriumListAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void saveKryterium(){
    String kryteriumName = spinnerKryteria.getSelectedItem().toString();
    int rating = seekBarRating.getProgress();

    if(!TextUtils.isEmpty(kryteriumName)){
        String id = databaseKryteria.push().getKey();

        Kryterium kryterium = new Kryterium(id, kryteriumName, rating);

        databaseKryteria.child(id).setValue(kryterium);

        Toast.makeText(this, "Kryterium zapisane poprawnie", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this, "Nazwa kryterium nie może pozostać pusta", Toast.LENGTH_SHORT).show();
    }
}

private void showUpdateDialog(final String kryteriumId, String kryteriumName){

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();

    final View dialogView = inflater.inflate(R.layout.piwo_update_dialog_kryterium, null);

    dialogBuilder.setView(dialogView);


    final Spinner spinnerKryteria = (Spinner) dialogView.findViewById(R.id.spinnerKryteria);
    final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdate);
    final SeekBar seekBarRating = (SeekBar) dialogView.findViewById(R.id.seekBarRating);
    final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDelete);

    dialogBuilder.setTitle("Aktualizacja Kryterium: " + kryteriumName);

    final AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String kryteriumName = spinnerKryteria.getSelectedItem().toString();
            int rating = seekBarRating.getProgress();


            updateKryterium(kryteriumId, kryteriumName, rating);
            alertDialog.dismiss();
        }
    });

    buttonDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            deleteKryterium(kryteriumId);
            alertDialog.dismiss();
        }
    });


}

private void deleteKryterium(String kryteriumId) {
    DatabaseReference drKryteria = FirebaseDatabase.getInstance().getReference().child(kryteriumId);

    drKryteria.child(kryteriumId).removeValue();

    Toast.makeText(this,"Kryterium zostało usunięte", Toast.LENGTH_LONG).show();

}

private boolean updateKryterium(String id, String name, int rating){

    Kryterium kryterium = new Kryterium(id, name, rating);

    databaseKryteria.child(id).setValue(kryterium);

    Toast.makeText(this, "Nazwa kryterium zaktualizowana", Toast.LENGTH_LONG).show();

    return true;
}

}