我有一个自定义的可扩展列表视图,在listview的子项中,我有一个删除条目的按钮。当我点击它时,我想删除SharedPreferences文件中的内容。
但是在BaseExpandableListAdapter中我无法调用getSharedPreferences ..我该如何解决? Thankss ..
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final UserMessage usermessage = (UserMessage) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_message, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
TextView tv2 = (TextView) convertView.findViewById(R.id.tv2Child);
String lastMsg = usermessage.getLastNewMsg();
tv.setText(usermessage.getNameUser()+" "+usermessage.getSurnameUser());
tv2.setText(myStringSubstract(lastMsg)+"...");
final TextView del = (TextView)convertView.findViewById(R.id.delete);
final ImageView delete = (ImageView)convertView.findViewById(R.id.deleteMsg);
del.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
del.setVisibility(4);
delete.setVisibility(1);
}
});
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int idActivity = usermessage.getIdActivity();
int idUser = usermessage.getidUser();
int sizeofChildren = children.get(groupPosition).size();
Log.d("group and cildren size",""+groups.size()+"-"+children.get(groupPosition).size());
children.get(groupPosition).remove(childPosition);
if(sizeofChildren == 1){
groups.remove(groupPosition);
}
try {
usermessage.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
notifyDataSetChanged();
}
});
删除setonclickListener中的..我想调用它..
答案 0 :(得分:3)
使用在构造函数中传递给适配器的上下文。
SharedPreferences pref = mContext.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);
答案 1 :(得分:0)
//Save value in SharedPreference
public static void saveStringInSP(context _context,String key, String value){
SharedPreferences preferences = _context.getSharedPreferences ("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}//savePWDInSP()