刷新 Listview simpleCursorAdapter 始终为 null
Refresh Listview simpleCursorAdapter always null
我有一个程序,用 SQLLiteDB
填充 listview
。初始加载有效,但是当我编辑条目时程序崩溃并且 simplecursorAdapter = null
.
片段:
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);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
dbHelper = new DBHelper(getActivity());
prepareCursor();
}
public void prepareCursor()
{
new Handler().post(new Runnable()
{
@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);
}
}
MainActivity 中调用它的代码片段:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
taskListFragment.updateList();
}
}
TaskInformation class 中的代码在我单击更新时运行
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();
}
为什么我更新的时候simpleCursorAdapter
总是NULL
?我想在更新时 refresh/reload listview
。
编辑----------------------------
我再次尝试 运行 prepareCursor 但它仍然为空,我似乎无法找出原因:(
编辑 2 ------------------------------
我更进一步了,我的adapter
需要的context
好像没了。通过将方法更新为此来修复它:
public void updateList(Context context)
{
simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
simpleCursorAdapter.notifyDataSetChanged();
fillList();
}
只有这一次我的cursor
是空的。当我尝试用这个重新填充它时:
cursor = dbHelper.getTasksForListView();
我收到这个错误:
05-18 07:30:59.478 7323-7323/com.owlfinity.zeepblok.taskjournal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.owlfinity.zeepblok.taskjournal, PID: 7323
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { cmp=com.owlfinity.zeepblok.taskjournal/.TaskInformation (has extras) }} to activity {com.owlfinity.zeepblok.taskjournal/com.owlfinity.zeepblok.taskjournal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
at com.owlfinity.zeepblok.taskjournal.TaskListFragment.updateList(TaskListFragment.java:61)
at com.owlfinity.zeepblok.taskjournal.MainActivity.onActivityResult(MainActivity.java:37)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这里是 getTaskForListView 方法:
public Cursor getTasksForListView()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return result;
}
编辑 3--------------------
错误消失了,但我的 ListView
仍然没有刷新 :( 这是代码
public void updateList(Context context)
{
dbHelper = new DBHelper(context);
cursor = dbHelper.getTasksForListView();
simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
fillList();
}
public void fillList()
{
dbHelper = new DBHelper(getActivity());
setListAdapter(simpleCursorAdapter);
}
----编辑 4
我的ListView
在update方法中是空的。似乎找不到重新创建它的方法。
我觉得问题出在这段代码:
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(false);
return rootView;
}
如何在更新方法中重新运行这段代码?
您不需要创建新的处理程序。
你应该只使用 listView.post(Runnable)
找到了,我有多个使用 onActivityResult
的 Activity,并在其中重建了 cursor
和 list
。将它们融合为一个并且它起作用了:D 猜猜我的应用程序对如何处理它感到困惑。找到了,因为返回的请求代码是一个我从未设置过的数字。
我有一个程序,用 SQLLiteDB
填充 listview
。初始加载有效,但是当我编辑条目时程序崩溃并且 simplecursorAdapter = null
.
片段:
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);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
dbHelper = new DBHelper(getActivity());
prepareCursor();
}
public void prepareCursor()
{
new Handler().post(new Runnable()
{
@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);
}
}
MainActivity 中调用它的代码片段:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
taskListFragment.updateList();
}
}
TaskInformation class 中的代码在我单击更新时运行
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();
}
为什么我更新的时候simpleCursorAdapter
总是NULL
?我想在更新时 refresh/reload listview
。
编辑----------------------------
我再次尝试 运行 prepareCursor 但它仍然为空,我似乎无法找出原因:(
编辑 2 ------------------------------
我更进一步了,我的adapter
需要的context
好像没了。通过将方法更新为此来修复它:
public void updateList(Context context)
{
simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
simpleCursorAdapter.notifyDataSetChanged();
fillList();
}
只有这一次我的cursor
是空的。当我尝试用这个重新填充它时:
cursor = dbHelper.getTasksForListView();
我收到这个错误:
05-18 07:30:59.478 7323-7323/com.owlfinity.zeepblok.taskjournal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.owlfinity.zeepblok.taskjournal, PID: 7323
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { cmp=com.owlfinity.zeepblok.taskjournal/.TaskInformation (has extras) }} to activity {com.owlfinity.zeepblok.taskjournal/com.owlfinity.zeepblok.taskjournal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
at com.owlfinity.zeepblok.taskjournal.TaskListFragment.updateList(TaskListFragment.java:61)
at com.owlfinity.zeepblok.taskjournal.MainActivity.onActivityResult(MainActivity.java:37)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这里是 getTaskForListView 方法:
public Cursor getTasksForListView()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return result;
}
编辑 3--------------------
错误消失了,但我的 ListView
仍然没有刷新 :( 这是代码
public void updateList(Context context)
{
dbHelper = new DBHelper(context);
cursor = dbHelper.getTasksForListView();
simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
fillList();
}
public void fillList()
{
dbHelper = new DBHelper(getActivity());
setListAdapter(simpleCursorAdapter);
}
----编辑 4
我的ListView
在update方法中是空的。似乎找不到重新创建它的方法。
我觉得问题出在这段代码:
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(false);
return rootView;
}
如何在更新方法中重新运行这段代码?
您不需要创建新的处理程序。
你应该只使用 listView.post(Runnable)
找到了,我有多个使用 onActivityResult
的 Activity,并在其中重建了 cursor
和 list
。将它们融合为一个并且它起作用了:D 猜猜我的应用程序对如何处理它感到困惑。找到了,因为返回的请求代码是一个我从未设置过的数字。