简体   繁体   中英

Can't refresh the fragment after combing back from another activity

I'm working on the fragment, when I click a button, it will turn to another activity, after the activity saved data to database, it will return back to the fragment, however, the fragment can't show the newest data, I need to go to the activity of the fragment, then come back to the fragment again, it will show newest data.

Here is my code of fragment. I thought that because I put the setAdapter in the onActivityCreated() method, I tried to put it in onCreate() and onCreateView(), both will return nullpointer error.

public class TodoFragment extends ListFragment {

private TodoDataSource datasource;
private Todo item;
List<Todo> todoList;
MyCustomAdapter adapt;
ListView listTask;
EditText t;

public TodoFragment(){  
}   

/*@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //setHasOptionsMenu(true);

    datasource = new TodoDataSource(getActivity());
    datasource.open();

    todoList = datasource.getAllTodos();
    adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
    //ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
    listTask.setAdapter(adapt);
}*/

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);         
   // if (activity != null) {
        // Create an instance of the custom adapter for the GridView. A static array of location data
        // is stored in the Application sub-class for this app. This data would normally come
        // from a database or a web service.
        datasource = new TodoDataSource(getActivity());
        datasource.open();

        todoList = datasource.getAllTodos();
        adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
        //ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
        listTask.setAdapter(adapt);

        Button button = (Button) getActivity().findViewById(R.id.simpleadd);
        button.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
            //String summary = t.getText().toString();                
            Intent i = new Intent(getActivity(), TodoDetailActivity.class);

            //Bundle bundle = new Bundle();
           // bundle.putString("Summary", summary);
            //i.putExtras(bundle);
            startActivity(i);
        }
    });

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main, menu);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_todo, container, false);

    t = (EditText)  rootView.findViewById(R.id.simple);
    listTask = (ListView) rootView.findViewById(android.R.id.list);

    return rootView;
}

@Override
public void onResume() {
  datasource.open();
  super.onResume();
  adapt.notifyDataSetChanged();
}

@Override
public void onPause() {
  datasource.close();
  super.onPause();
}

}

Here is part of my another activity code. it will update the database and return to the fragment.

confirmButton.setOnClickListener(new View.OnClickListener() {
  public void onClick(View view) {
    if (TextUtils.isEmpty(mTitleText.getText().toString())) {
      makeToast();
    } else {
        final String category = mCategory.getSelectedItem().toString();
        final String summary = mTitleText.getText().toString();
        final String description = mBodyText.getText().toString();
        datasource.createTodo(category, summary, description);
        setResult(RESULT_OK);
        finish();
    }
  }

});

It looks like you aren't updating your data source todoList before calling notifyDataSetChanged() in onResume() .

Edit: Clear the list first, and then re-populate. Also add logging to help track down the issue:

@Override
public void onResume() {
  super.onResume();
  datasource.open();
  todoList.clear();
  todoList.addAll(datasource.getAllTodos());
  adapt.notifyDataSetChanged();

  Log.d("MyApp", "Size of data: " + todoList.size() );
}

Once you have added that code, try adding a few records, and watch your logcat to see if the size goes up. You can also cycle through each record, if you post your Todo class I can give you some example code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM