Search code examples
javaandroidlistviewsimpleadapter

Android: SimpleAdapter notifyOnDataSetChanged does not fire getView


The title pretty much says it all. I have a listview being populated by a simple adapter and an xml layout that changes based on how many rows are present in the listview item. When I add data to one of the items in the activity, calling the notifyondatasetchanged does not update the list, but if I back out of the activity and go back in everything looks the way it should. What do I need to do so when I refresh the adapter the getview gets updated as well?

    public AlarmListAdapter(Context context, ArrayList<HashMap<String, String>> list, int textViewResourceId, String[] fields, int[] textViewId) {
    super(context, list, textViewResourceId, fields, textViewId);
    this.list = list;
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;
if (v == null) {
    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.alarm_list_item, null);
}


if(list.get(position).get("alert") == null){    
    v.findViewById(R.id.ListViewItemSub).setVisibility(View.GONE);
}

return super.getView(position, convertView, parent);

}


public void forceReload(){
    notifyDataSetChanged();
}

The notifyondatasetchanged is being called from a popup within my activity. The code for that button is listed below:

        addCustomAlertButton = (Button)pView.findViewById(R.id.AddCustomAlertButton);
    addCustomAlertButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            arrayList.get((int) customAlertId).setAlertDialog(customAlertInput.getText().toString());
            Toast.makeText(EditAlarmsActivity.this, "Custom Alert Added", Toast.LENGTH_SHORT).show();
            updateListArray();
            pw.dismiss();
        }

Solution

  • You could try setListAdapter(yourAdapter);

    or listview.requestLayout();

    If those didn't work then you could also try refreshing the listview through scrolling, since the getView() function is called once the listview is scrolled.