1

I have a listview in GroupListActivity and I want to add some groups on SearchActivity. I started startActivityForResult on GroupListActivity() and after I press the back button on SearchActivity I get the result. But onActivitySesult call notitfySetChanged() and the list inside GroupListActivity doesn't refresh. This is my code:

public class GroupsListActivity extends Activity {

    private static final int REQUEST = 1;

    String loggedUserId = Model.getInstance().getLoggedUserId();
    ListView list;
    List<Group> data;
    MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set layout for this activity
        setContentView(R.layout.groups_list);

        // Show actionbar in this activity
        getActionBar().show();

        if(loggedUserId != null)
            Log.d("TAG", "My Groups for user ID: " + loggedUserId);

        // Connect between button to layout id
        list = (ListView) findViewById(R.id.my_group_list);

        // Setting adapter and creating group list
        if(loggedUserId != null) {
            Model.getInstance().getAllGroupsByUserId(loggedUserId, new Model.groupListReturnedListener() {
                @Override
                public void groupListReturned(List<Group> groupList) {
                    data = groupList;
                    adapter = new MyAdapter();
                    list.setAdapter(adapter);
                    Collections.sort(data, Group.Comparators.NAME); //Sort array
                }
            });
        }
    }


    private void onSearch() {
        Log.d("TAG", "Search button was pressed");
        Intent i = new
                Intent(getApplicationContext(),
                SearchActivity.class);

        startActivityForResult(i, REQUEST);
        overridePendingTransition(R.animator.slide_out_right, R.animator.slide_in_right);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST) {
            if(resultCode == RESULT_OK) {
                adapter.notifyDataSetChanged();
            }
        }
    }  
}
Alex Tech
  • 123
  • 1
  • 9

1 Answers1

-1

I think you have not altered dataset in your adapter .

call

dataAdapter = new DataAdapter(context, DataList);
dataAdapter.notifyDataSetChanged();
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
  • obviously, this wouldn't help ... first the ListView will not know about new adapter instance, second there is no sens to call `notifyDataSetChanged` right after creation of the adapter ... nice example of [Million Monkeys Programming Style](https://en.wikipedia.org/wiki/Programming_by_permutation) – Selvin May 27 '16 at 09:00