Berkay Aras
Berkay Aras

Reputation: 127

Android SharedPreference in custom listview

i have a custom expandable listview, and in the child of listview i have a button that deletes entry. When i click it i want to delete the content in the SharedPreferences file.

But in the BaseExpandableListAdapter i can't call the getSharedPreferences.. How can i solve it?? 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();
            }
        });

in delete setonclickListener.. iwant to call it..

Upvotes: 1

Views: 1422

Answers (2)

Ishu
Ishu

Reputation: 5457

//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()

Upvotes: 0

MByD
MByD

Reputation: 137432

Use the context that you passed to the adapter in the constructor.

SharedPreferences pref = mContext.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);

Upvotes: 3

Related Questions