gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:14:51 25 4
gpt4 key购买 nike

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

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

日志猫:

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)

注释 fragment :

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

}

主要 Activity :

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)

最佳答案

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

关于java - 如何使用 .notifyDataSetChanged() 更新 Listfragment 中的 SimpleCursorAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22549234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com