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?
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.
– Ricardo
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.
– regmoraes
Has any link q show a code like this?
– Ricardo
Has this link here . Although it is in English it is quite complete, so I think it will help you a lot.
– regmoraes