How to create button in a SimpleAdapter with the click event excluding the item

1

I've assembled a list in an android application which is a simple list using SimpleAdapter .

What I need now is to delete the item from the list, but I do not know how I would do it because I could not get the position of the button clicked.

Follow the code:

public class MostrarTodasTarefas extends AppCompatActivity {

    ListView list = null;

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

        String[] de = { "url", "status", "lastexecute", "btnExcluir" };
        int[] para = { R.id.lblURL, R.id.lblStatus, R.id.lblLastExecute, R.id.btnExcluir};

        SimpleAdapter adapter = new SimpleAdapter(this, convertToMap(),
                R.layout.layout_listaurls, de, para);

        list = (ListView) findViewById(R.id.listTarefas);

        list.setAdapter(adapter);

        adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
                  @Override
                  public boolean setViewValue(View view, Object data, String textRepresentation) {

                      if (view.getId() == R.id.btnExcluir) {

                          Button b = (Button) view;
                          b.setOnClickListener(new View.OnClickListener() {

                              @Override
                              public void onClick(View v) {

                                  Excluir(v);
                              }
                          });

                          return true;
                      }

                      return false;
                  }
              }
        );
    }

    public void Excluir(View view){
        EventBus.getDefault().post(new MessageEvent("Excluido"));
    }
}

It creates ListView and Button 's.

Where I call Excluir(v) works, it executes this click on all the buttons in the list.

What I can not do is take the position to do the deletion, what do I need to do?

    
asked by anonymous 08.08.2015 / 17:21

1 answer

0

To get the clicked element and its position in a List View you must have your List View implement the onItemClickListener method. Suppose you have a List View where each item is Usuario . To get a particular user, you would do the following:

/*
    Declaração e inicialização da List View e dos Adapters.
*/

minhaListView.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> meuAdapter, View minhaView, int posicaoSelecionada, long lng) {

    Usuario usuarioSelecionado =(Usuario) (minhaListView.getItemAtPosition(posicaoSelecionada));

    /*
         Realiza operações com o usuário obtido
    */
  }                 
});

NOTE: Modify the name of the method Excluir() to excluir() , because by convention, Java methods must be written in camelCase, so the first word is lowercase and each next word has the first capital letter. In addition, methods should express actions and should not have accentuation. Ex: excluirUsuario() , calcularFrete()

    
10.08.2015 / 00:45