我希望在用户长按行时删除其在SharedPreferences中保存的值来删除该行。因此,我需要将行的文本值作为ID。我在onCreateView()的listview中设置了一个onItemLongClick()方法,但是我收到了错误。
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;
}
});
记录结果:
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'
答案 0 :(得分:1)
尝试从ListView项目数据持有者处获取数据:
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String,String> row1= new HashMap<String, String>();
row1.put("cName","A");
list.add(row1);
public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
CharSequence name = list.get(index).get("cName");
}