Delete item from Listview

Asked

Viewed 1,004 times

0

I have a ListView that displays the title of each subject. When I click on the item another is displayed Activity that shows the details about the title. In this Activity has a button for rule out this item if the user wants, but I cannot implement the function delete.

Fragment that displays the ListView with the titles:

listView = (ListView) rootview.findViewById(R.id.textViewConteudo);
AdapterCompromissos adapterCompromissos = new AdapterCompromissos(getActivity(),
new BD(getActivity()).lista());
listView.setAdapter(adapterCompromissos);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int posicao,
                                long arg3) {

            Compromissos compromissos = (Compromissos) adapter.getAdapter().getItem(posicao);

            Intent it = new Intent(getActivity(), opcao4.class);

            String titulo = compromissos.getTitulo();
            String data = compromissos.getData();
            String hora = compromissos.getHora();
            String descricao = compromissos.getDescricao();
            Long id = compromissos.getId();

            it.putExtra("ID", id);
            it.putExtra("TITULO", titulo);
            it.putExtra("DATA", data);
            it.putExtra("HORA", hora);
            it.putExtra("DESCRICAO", descricao);
            startActivity(it);

        }

    });

Activity that displays the details of the item clicked on ListView:

titulo = (TextView) findViewById(R.id.titulo);
    data = (TextView) findViewById(R.id.data);
    hora = (TextView) findViewById(R.id.hora);
    descricao = (TextView) findViewById(R.id.descricao);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if(extras != null) {
        getId = extras.getLong("ID");
        getTitulo = extras.getString("TITULO");
        getData = extras.getString("DATA");
        getHora = extras.getString("HORA");
        getDescricao = extras.getString("DESCRICAO");
    }

    titulo.setText(getTitulo);
    data.setText(getData);
    hora.setText(getHora);
    descricao.setText(getDescricao);
}

public static final int DELETAR = 1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    item = menu.add(0,DELETAR,1, "Deletar");
    item.setIcon(R.drawable.delete);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {

        case DELETAR:
            ClasseDeDados dados = new ClasseDeDados ();
            dados.setId(getId);
            BD bd = new BD(opcao4.this);
            bd.deletar(dados);

            return true;
    }
  return false;
}

My problem is deleting the item I don’t know how to proceed.

[This code is up to date and this is the solution to the problem I had].

1 answer

2

I got!

It was missing in the marry DELETAR:

ClasseDeDados dados = new ClasseDeDados ();
dados.setId(getId);
BD bd = new BD(opcao4.this);
bd.deletar(dados);

That one getId receives the id of ListView at the time of click in the item.

  • Where it is declared getId question? I find it interesting that the question has this information as well.

  • I updated the answer with the correct code, with the changes I made to fix my problem.

Browser other questions tagged

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