user2405469
user2405469

Reputation: 2003

ListVIew not updating onRestoreSaveInstanceState

Here is my method:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {

    DataBaseHandler handler = new DataBaseHandler(getApplicationContext());

    //set the spinner for measurement type
    Spinner measurementTypeSpinner = (Spinner) findViewById(R.id.MeasurementTypes);
    ArrayAdapter adapter = (ArrayAdapter) measurementTypeSpinner.getAdapter();
    int typePos = adapter.getPosition(savedInstanceState.getString("measurementtype"));
    measurementTypeSpinner.setSelection(typePos);

    //set the spinner for the measurement unit
    Spinner measurementUnitSpinner = (Spinner) findViewById(R.id.MeasurementSubValues);
    ArrayAdapter arrayAdapter = (ArrayAdapter) measurementUnitSpinner.getAdapter();
    int unitPos = arrayAdapter.getPosition(savedInstanceState.getString("measurementunit"));
    measurementUnitSpinner.setSelection(unitPos);

    //set the value
    EditText value = (EditText) findViewById(R.id.unit_value);
    value.setText(savedInstanceState.getString("value"));

    /**
     * The list view stuff
     */
    ListView unitsList = (ListView) findViewById(R.id.units_list);
    unitsList.setItemsCanFocus(true);
    MeasurementType mType = handler.getMeasurementType(savedInstanceState.getString("measurementtype"));

    //create the converter
    Converter converter = new Converter(MeasurementType.getMeasurementType(savedInstanceState.getString("measurementtype")), savedInstanceState.getString("measurementunit"), savedInstanceState.getString("value"));
    //convert the values
    ArrayList<Unit> convertedValues = converter.convert();

    //set the adapter for the list view
    unitAdapter = new UnitListAdapter(this, convertedValues, mType);
    unitsList.setAdapter(unitAdapter);

}

Basically, there is another activity with a list of items and when the user checks one, it updates the database setting an int property to 1, so that when the ArrayAdapter goes through an arraylist it picks up the property as 1 and displays it, instead of 0 in which case it doesn't display it.

Now on pressing the back button, both the spinners are populated with the values I stored, the value for the EditText is restored, but the ListView is not updated, yet when I leave the app and come back in, the value that was checked is there in the list...

This says to me that I might need to do something with onStop() and onRestart() could someone please advice me. The comment saying 'the list view stuff' is where I am trying to update the list view, it just isn't working and when I debug it won't go into the restore method at all, which is confusing.

EDIT

public class UnitListAdapter extends ArrayAdapter<Unit> {
    private Context context;
    private ArrayList<Unit> units;
    private MeasurementType type;

    public UnitListAdapter(Context context, ArrayList<Unit> units, MeasurementType type) {
        super(context, R.layout.unit, R.id.unit_name, units);
        this.context = context;
        this.units = units;
        this.type = type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.unit, parent, false);
        final TextView unitName = (TextView) rowView.findViewById(R.id.unit_name);
        final EditText unitValue = (EditText) rowView.findViewById(R.id.unit_value);
        if(units.get(position) != null) {
            if(units.get(position).getView() == 1) {
                unitName.setText(units.get(position).getUnitName());
                unitValue.setText(units.get(position).getValue().toString());
            } else {
                unitName.setVisibility(View.GONE);
                unitValue.setVisibility(View.GONE);
            }
        }

        return rowView;
    }

    @Override
    public void add(Unit u) {
        units.add(u);
        notifyDataSetChanged();
    }

    @Override
    public void clear() {
        units.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return units.size();
    }

}

As asked for. Sorry about confusion whilst editing.

Upvotes: 0

Views: 57

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83527

onRestoreInstanceState() is not called when the user presses the back button. Most likely you need to move your logic to onResume(). I suggest that you read about the Activity lifecycle to get a better understanding about when each of the onXxx() methods are called.

Upvotes: 1

Dave S
Dave S

Reputation: 3468

After updating the list you need to call notifyDataSetChanged() to repopulate the listview.

http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()

Upvotes: 1

Related Questions