I have a custom listview with three items. One of them is like “Add this to the DB” and when I click to it it inserts something to the DB.
What I want it to do is after doing the insert, change the text to “Delete this from the DB” and also the onClick method to call a method to delete that record instead a method to insert.
Is this possible?
Here is my code:
final String[] opcs = new String[]{"Resultados", "Clasificación", text_fav};
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.list_menutipo_item, opcs);
m_list.setAdapter(aa);
m_list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent newActivity = null;
switch( position )
{
case 0: ...
case 2: if (isConnected(m_context))
{
añadirFavorito();
}
break;
}
}
});
It’s definitely possible. The view parameter to the
onItemClickcallback is the view on which you clicked, you can simply change that view’s content. i.e.view.setText("Delete this from the DB").Also you will want to distinguish whether the next click is “Add this to the DB” or “Delete this from the DB”, doing a string comparison here like
if("Delete this from the DB".equals(view.getText()))might not be of good practice, you can set a flag in the view likeview.setTag(true)to indicate that the current view’s content is “Delete this from the DB”. and later you can useview.getTag()to get back the flag to do the comparison.