How to create button in a Simpleadapter with event click excluding item

Asked

Viewed 718 times

2

I set up a list in an android app that is a simple list using SimpleAdapter.

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

Follows 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 the ListView and the Button's.

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

What I can’t do is take the position to do the exclusion, what I need to do?

1 answer

0

To catch the clicked element and its position in a List View you must make your List View implement the method onItemClickListener. Suppose you have a List View where each item is one Usuario. To catch 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
    */
  }                 
});

OBS.: Change the method name Excluir() for excluir(), because by convention, Java methods should be written in camelCase, so that the first word is lowercase and each next word has the first uppercase letter. In addition, the methods should express actions and should not have accentuation. Ex.: excluirUsuario(), calcularFrete()

  • Sorry for the delay in answering, where did you link that click would be the button? I didn’t understand where the item click happens only at the click of the button inside the item.

  • Sorry, the code I posted is the solution to get the position of the clicked item, IE, if you click the button, it would not take the position. A solution to your problem would be to implement a Custom Adapter, because in it you could take the position of the item when the button is clicked and delete it.

  • Has any link q show a code like this?

  • Has this link here . Although it is in English it is quite complete, so I think it will help you a lot.

Browser other questions tagged

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