I have a program with fills a listview from a SQLLiteDB. The initialload works, but when I edit a entry the program crashes and the simplecursorAdapter = null.
The Fragment:
public class TaskListFragment extends ListFragment
{
DBHelper dbHelper;
SimpleCursorAdapter simpleCursorAdapter;
Cursor cursor;
ListView listView;
static final int UPDATE_TASK = 1;
String[] from = {dbHelper.COL_2,dbHelper.COL_3};
int[] to = new int[]{R.id.taskName , R.id.taskDescription};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.tasklistlayout, container, false);
listView = (ListView)rootView.findViewById(android.R.id.list);
setRetainInstance(true);
retu rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
dbHelper = new DBHelper(getActivity());
prepareCursor();
}
public void prepareCursor()
{
new Handler().post(new Ruable()
{
@Override
public void run()
{
cursor = dbHelper.getTasksForListView();
simpleCursorAdapter = new SimpleCursorAdapter (getActivity(),R.layout.taskrowlayout,cursor,from,to,0);
fillList();
}
});
}
public void updateList()
{
simpleCursorAdapter.notifyDataSetChanged();
fillList();
}
public void fillList()
{
setListAdapter(simpleCursorAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
Intent intent = new Intent(getActivity().getApplicationContext(), TaskInformation.class);
intent.putExtra("taskId", cursor.getInt(0));
intent.putExtra("taskName", cursor.getString(1));
intent.putExtra("taskDescription", cursor.getString(2));
intent.putExtra("taskStartDate", cursor.getString(3));
intent.putExtra("taskEndDate", cursor.getString(4));
startActivityForResult(intent, UPDATE_TASK);
}
}
The piece of code in MainActivity wich calls it:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
taskListFragment.updateList();
}
}
The code in the TaskInformation class witch runs when i click update
public void updateTask(View view)
{
task = new ArrayList<>();
task.add(0,String.valueOf(currentTaskId));
task.add(1,String.valueOf(taskName.getText()));
task.add(2,String.valueOf(taskDescription.getText()));
task.add(3,String.valueOf(startDate.getText()));
task.add(4,String.valueOf(endDate.getText()));
dbHelper.updateTask(task);
setResult(RESULT_OK, intent);
finish();
}
The result retu nicely and the code arives in the onresult method. But why is the simpleCursorAdapter always NULL when I update it? I want to refresh/reload the listview when I updated it.
