I want to remove the row by deleting its saved value in SharedPreferences when user press long the row.For this reason I need to accsess row's text value as ID. I have set a onItemLongClick() method to my listview in onCreateView() but I'm getting error.
listView.setLongClickable(true);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
SharedPreferences prefs = getActivity().getSharedPreferences("records",getActivity().MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
TextView rowText = (TextView)v.findViewById(R.id.cName);
CharSequence name = rowText.getText();
if(name.equals("A")){
editor.remove("aChecked");
}else if(name.equals("B")){
editor.remove("bChecked");
}
Toast.makeText(getActivity(),"Item removed", Toast.LENGTH_LONG).show();
return true;
}
});
Log results :
11-13 11:02:23.201: E/AndroidRuntime(1785): FATAL EXCEPTION: main
11-13 11:02:23.201: E/AndroidRuntime(1785): java.lang.NullPointerException
11-13 11:02:23.201: E/AndroidRuntime(1785): at com.example.application.FavorilerFragment$1.onItemLongClick(FavorilerFragment.java:68)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2621)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:564)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.view.View.showContextMenu(View.java:3581)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.view.View.performLongClick(View.java:3550)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.widget.TextView.performLongClick(TextView.java:9221)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.view.View$CheckForLongPress.run(View.java:14083)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.os.Handler.handleCallback(Handler.java:605)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.os.Looper.loop(Looper.java:137)
11-13 11:02:23.201: E/AndroidRuntime(1785): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-13 11:02:23.201: E/AndroidRuntime(1785): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 11:02:23.201: E/AndroidRuntime(1785): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 11:02:23.201: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-13 11:02:23.201: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-13 11:02:23.201: E/AndroidRuntime(1785): at dalvik.system.NativeStart.main(Native Method)
11-13 11:02:23.721: I/dalvikvm(1785): threadid=3: reacting to signal 3
11-13 11:02:23.721: I/dalvikvm(1785): Wrote stack traces to '/data/anr/traces.txt'
Try to get data from ListView item data holder :