Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have 2 arrays named "labels" and "values" I am sending these elements to an adapter class and setting it on the listview. Now I want all these values to be saved in shared preferences and also it should be editable an update the changes. how to use Sharedpreferences in listview I am not getting.

Java
public class MyProfile_Fragment extends Fragment implements View.OnClickListener{
    ArrayList<string> label;
    ArrayList<string> values;
    ArrayList<SelectUser> selectUsers = null;
    ProfileAdapter adapter;
    ListView listView;

    Button editbtn;
    Context _c=getActivity();

    View profile;
    SelectUser cat;
    int clickCount = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        profile = inflater.inflate(R.layout.profile_layout, container, false);

        selectUsers = new ArrayList<SelectUser>();
        editbtn = (Button) profile.findViewById(R.id.edit);
        listView = (ListView) profile.findViewById(R.id.profileList);

        label = new ArrayList<string>();
        label.add("Fname");
        label.add("Lname");
        label.add("Industry");
        label.add("Company");
        label.add("Address");

         values = new ArrayList<string>();
        values.add("abc");
        values.add("M");
        values.add("IT");
        values.add("XYZ");
        values.add("qwerty");

        for (int i = 0; i<= label.size(); i++) {
            SelectUser selectUser = new SelectUser();
            selectUser.setLabel(label.get(i));
            String val=values.get(i);
            selectUser.setValues(String.valueOf(values.get(i));
            selectUsers.add(selectUser);
        }
        adapter = new ProfileAdapter(getActivity(), selectUsers);
        listView.setAdapter(adapter);


//        on edit clickbutton enable or disable the edit text by calling actv() method
        editbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (clickCount == 0) {

                    editbtn.setText("Save");

                    adapter.actv(true);

                    adapter.notifyDataSetChanged();
                    listView.setAdapter(adapter);                 
                    clickCount = 1;

                } else if (clickCount == 1) {

                    editbtn.setText("Edit");

                    adapter.actv(false);

                    adapter.notifyDataSetChanged();
                    listView.setAdapter(adapter);
                    Toast.makeText(getActivity(), "click 1", Toast.LENGTH_LONG).show();
                    clickCount = 0;

                }

            }
        });
        return profile;
    }


    // Adopter class : handle list items
    class ProfileAdapter extends BaseAdapter {

        ViewHolder holder = new ViewHolder();

        public ArrayList&lt;SelectUser> groupItem;

        int flag=0;
        public ProfileAdapter(Context context, ArrayList&lt;SelectUser> group) {
            groupItem = group;

            _c = context;
        }

        @Override
        public int getCount() {
            return groupItem.size();
        }

        @Override
        public SelectUser getItem(int i) {
            return groupItem.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;

        }

        @Override
        public View getView(final int position, final View convertView, ViewGroup parent) {


            View v = convertView;

            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) _c.getSystemService
                        (Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.profile_listitems, parent, false);

                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.mLable = (TextView) v.findViewById(R.id.label);
            holder.mValues = (EditText) v.findViewById(R.id.values);

            flag=position;
            cat = groupItem.get(flag);

            holder.mLable.setText(cat.getLabel());
            holder.mValues.setText(cat.getValues());


            holder.mValues.addTextChangedListener(new GenericTextWatcher(holder.mValues));



            if(clickCount==0)
            {holder.mValues.setEnabled(false);

                actv(false);
            }
            return v;
        }
        protected void actv(final boolean active) {
            holder.mValues.setEnabled(active);
            if (active) {
                holder.mValues.requestFocus();

            }
        }
    }

    class ViewHolder {

        TextView mLable;
        EditText mValues;
    }

    //Declaration
    private class GenericTextWatcher implements TextWatcher {

        private View view;

        private GenericTextWatcher(View view) {
            this.view = view;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        public void afterTextChanged(Editable editable) {
            String text = editable.toString();
            switch (view.getId()) {
                case R.id.values:
                    cat.setValues(text);
                    break;

            }
        }
    }
}
Posted
Updated 20-Jan-16 18:35pm
v3
Comments
Richard MacCutchan 20-Jan-16 7:55am    
You do it by writing code to take the values and save them according to your requirements. Please edit your question and explain what difficulty you are having with your implementation.
[no name] 20-Jan-16 9:07am    
Please make your question pin-pointed means ask what exactly you need.

1 solution

You can loop through the elements of your ArrayAdapter and store them to your SharedPreferences as like:
C#
SharedPreferences.Editor editor = prefs.edit();
// This will remove all entries of the specific SharedPreferences
editor.clear();
for (int i = 0; i < adapter.getCount(); ++i){
    // This assumes you only have the list items in the SharedPreferences.
    editor.putString(String.valueOf(i), adapter.getItem(i));
}
editor.commit();

Also see:
Android: How to store list of values in SharedPreferences | Android Open Tutorials[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900