Delete ListView item

0

I have a ListView that displays the title of each subject. When I click on the item it displays another Activity that shows the details about the title. In this Activity has a button to delete this item if the user wants, but I can not implement the delete function.

Fragment that displays ListView with 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 item details 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 to delete the item that I do not know how to proceed.

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

    
asked by anonymous 26.05.2015 / 03:34

1 answer

2

I did it!

This was missing in case DELETAR :

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

This getId gets id from ListView at click time on item.

    
26.05.2015 / 03:55