在从另一个活动中梳理回来之后无法刷新片段

时间:2015-03-07 03:36:11

标签: android android-fragments

我正在研究片段,当我点击一个按钮时,它将转向另一个活动,在活动将数据保存到数据库后,它将返回到片段,但是片段无法显示最新的数据,我需要去片段的活动,然后再回到片段,它会显示最新的数据。

这是我的片段代码。我认为,因为我把setAdapter放在onActivityCreated()方法中,我试着把它放在onCreate()和onCreateView()中,两者都会返回nullpointer错误。

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();
}

}

这是我的另一个活动代码的一部分。 它将更新数据库并返回到片段。

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();
    }
  }

});

1 个答案:

答案 0 :(得分:0)

todoList中致电notifyDataSetChanged()之前,您似乎没有更新数据来源onResume()

编辑:首先清除列表,然后重新填充。还要添加日志记录以帮助追踪问题:

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

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

添加完该代码后,尝试添加一些记录,然后观察logcat以查看大小是否增加。您还可以遍历每条记录,如果您发布Todo课程,我可以为您提供一些示例代码。