I have a listview, it shows up great, however when I click on my “edit” action button, I want the listview to use a different layout resource so that buttons show up beside each item in the list.
layout before: listitem_routine
layout I want after "edit" action button clicked: listitem_routine_edit
heres my code that works:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routines);
datasource = new RoutinesDataSource(this);
datasource.open();
List<Routine> values = datasource.getAllRoutines();
ArrayAdapter<Routine> adapter = new ArrayAdapter<Routine>(this,
R.layout.listitem_routine, R.id.listitem_routine_name, values);
setListAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
ArrayAdapter<Routine> adapter = (ArrayAdapter<Routine>) getListAdapter();
// Handle item selection
switch (item.getItemId()) {
case R.id.button_routines_edit:
// change the list view adapter resource layout to *_edit
// hide this button and show the "done editing (checkmark)" button
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I know that the case for R.id.button_routines_edit works because I made toast when it was clicked. and it was delicious. I since removed that code…
but any ways, how would I go about changing the layout resource when R.id.button_routines_edit is clicked? all the code is there, I just don’t know how to go about changing the layout resource and then obviously updating the listview to display the new row layout…
Reload the listview with the new resource after clicking the action button as follows.