如何使用.notifyDataSetChanged()更新Listfragment中的SimpleCursorAdapter?

时间:2014-03-21 02:49:24

标签: java android android-listfragment simplecursoradapter notifydatasetchanged

我想向ListView(ListFragment)插入一个新的视图数据行,我已经在方法中的setAdapter()中声明了数组,但是它有NullPointerException错误 我应该如何在新方法中使用adapter.notifyDataSetChanged();

我尝试再次在new方法中声明Cursor,或者在类中声明SimpleCursorAdapter,但是它们有一些错误

logcat的:

03-21 02:41:30.099: W/dalvikvm(4130): threadid=1: thread exiting with uncaught exception (group=0xa4b60648)
    03-21 02:41:30.103: E/AndroidRuntime(4130): FATAL EXCEPTION: main
    03-21 02:41:30.103: E/AndroidRuntime(4130): java.lang.NullPointerException
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at com.example.mycalc.NoteFragment.updateView(NoteFragment.java:80)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at com.example.mycalc.MainActivity.updateResult(MainActivity.java:34)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at com.example.mycalc.CalcFragment$1.onClick(CalcFragment.java:95)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at android.view.View.performClick(View.java:4240)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at android.view.View$PerformClick.run(View.java:17721)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at android.os.Handler.handleCallback(Handler.java:730)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at android.os.Handler.dispatchMessage(Handler.java:92)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at android.os.Looper.loop(Looper.java:137)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at java.lang.reflect.Method.invoke(Method.java:525)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    03-21 02:41:30.103: E/AndroidRuntime(4130):     at dalvik.system.NativeStart.main(Native Method)

NoteFragment:

package com.example.mycalc;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.widget.ListView;

public class NoteFragment extends ListFragment {


private DB mDbHelper;
private Cursor mCursor;
private SimpleCursorAdapter adapter;
//String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
//int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};

//SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
        //mCursor, from_column, to_layout);

private Cursor newCursor;

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

    //int layout = android.R.layout.simple_list_item_1;

    //ArrayAdapter<String> adapter = new ArrayAdapter(getActivity(), layout, CalcFragment.InputResult);

    //setListAdapter(adapter);

    setAdapter();

    if(Debug.log){ Log.d("NoteFragment", "onCreated");}

}

@Override
public void onStart() {
    super.onStart();
    FragmentManager fm = getFragmentManager();
    if( fm.findFragmentById(R.id.note_fragment)!= null){
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

public void setAdapter(){

    mDbHelper= new DB(getActivity());
    mDbHelper.open();
    mCursor = mDbHelper.getAll();
    getActivity().startManagingCursor(mCursor);

    String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
    int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};

    adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
            mCursor, from_column, to_layout);

    setListAdapter(adapter);

    if(Debug.log){ Log.d("NoteFragment", "setAdapter");}

}

public void updateView(){

    //mDbHelper= new DB(getActivity());
    //mDbHelper.open();
    //mCursor = mDbHelper.getAll();
    //getActivity().startManagingCursor(mCursor);*/

    //String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
    //int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};

    adapter.changeCursor(mCursor);

    adapter.notifyDataSetChanged();

    //if(Debug.log){ Log.d("NoteFragment", "updateView");}
}

}

MainActivity:

public class MainActivity extends FragmentActivity implements CalcFragment.OnEqualSelectedListener {
    ...
    @Override
    public void updateResult() {
        NoteFragment fragmentResult = new NoteFragment();
        fragmentResult.updateView();
        if(Debug.log){ Log.d("MainActivity", "updateResult");}
    }

新错误消息:

03-22 13:49:07.321: W/dalvikvm(1566): threadid=1: thread exiting with uncaught exception (group=0xa4c21648)
03-22 13:49:07.325: E/AndroidRuntime(1566): FATAL EXCEPTION: main
03-22 13:49:07.325: E/AndroidRuntime(1566): java.lang.NullPointerException
03-22 13:49:07.325: E/AndroidRuntime(1566):     at com.example.mycalc.NoteFragment.updateView(NoteFragment.java:80)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at com.example.mycalc.MainActivity.updateResult(MainActivity.java:34)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at com.example.mycalc.CalcFragment$1.onClick(CalcFragment.java:155)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at android.view.View.performClick(View.java:4240)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at android.view.View$PerformClick.run(View.java:17721)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at android.os.Handler.handleCallback(Handler.java:730)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at android.os.Looper.loop(Looper.java:137)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at java.lang.reflect.Method.invoke(Method.java:525)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 13:49:07.325: E/AndroidRuntime(1566):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
        mCursor, from_column, to_layout);

此行声明本地变量。您没有使用相同的名称初始化您的成员变量。你应该把它改成

adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
        mCursor, from_column, to_layout);

答案 1 :(得分:0)

实际上你在setAdapter方法中实例化局部变量中的适配器。

所以将其改为

adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
            mCursor, from_column, to_layout);