20 Oct 2014

Store & Retrive hash map in shared preferences | Remember me functionality in android

Hello Friends,

It’s a very important post "Store Hash map in to Shared Preference",

Normally we are preferred database to store data but there is also alternate way to store data in case of small storage like store “Username” & “Password”

Here, in this sample code I have implemented remember me functionality using hash map with shared preference.


Functionality: Stored Username and password with its key and pair, you will able to find list of user name on Auto complete text, while selecting any username you will able to get its respective password.





Let start implementation with android application

Step : 1 User Bean

The class which have set and get username and password method


public class UserBean
{
       private String Username;
       private String Password;
       public String getUsername()
       {
              return Username;
       }
       public void setUsername(String username)
       {
              Username = username;
       }
       public String getPassword()
       {
              return Password;
       }
       public void setPassword(String password)
       {
              Password = password;
       }       
}
Step : 2 RememberMeActivity


import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class RememberMeActivity extends Activity { HashMap<String, String>map=new HashMap<String, String>(); SharedPreferences pref; SharedPreferences.Editor ed; EditText edtPsw; CheckBox chkUnm,chkPsw; ArrayList<UserBean>arraylistBean=new ArrayList<UserBean>(); private AutoCompleteTextView autoComplete; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete); edtPsw=(EditText) findViewById(R.id.edtPassword); chkUnm=(CheckBox) findViewById(R.id.chkUsername); chkPsw=(CheckBox) findViewById(R.id.chkPassword); Loadcredentials(); ((Button)findViewById(R.id.btnDone)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String username = autoComplete.getText().toString(); String password =edtPsw.getText().toString(); pref = getSharedPreferences("HB", MODE_PRIVATE); ed = pref.edit(); ed.putString(username, password); ed.commit(); } }); } /*Load credentials which is save in share preference Hashmap*/ @SuppressWarnings({ "rawtypes", "unchecked" }) private void Loadcredentials() { pref = this.getSharedPreferences("HB", MODE_PRIVATE); ed = pref.edit(); Map map = pref.getAll(); Set keySet = map.keySet(); Iterator iterator = keySet.iterator(); while (iterator.hasNext()) { UserBean bean=new UserBean(); String key = (String) iterator.next(); String value = (String)map.get(key); Log.i("HashMap","Username==="+ key +" Psw=== " +value); bean.setUsername(key); bean.setPassword(value); arraylistBean.add(bean); } for (int i = 0; i < arraylistBean.size(); i++) { UserBean bean=arraylistBean.get(i); Log.e("ArrayList","Username=="+ bean.getUsername() +" Psw== " +bean.getPassword()); } List countryList = getCountryList(arraylistBean); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countryList); autoComplete.setAdapter(adapter); autoComplete.setThreshold(1); autoComplete.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> view, View arg1, int position,long arg3) { Toast.makeText(getBaseContext(), ""+view.getItemAtPosition(position),Toast.LENGTH_LONG).show(); edtPsw.setText(arraylistBean.get(position).getPassword()); } }); } @SuppressWarnings({ "rawtypes", "unchecked" }) private List getCountryList(ArrayList<UserBean> countries) { List list = new ArrayList(); for (UserBean c : countries) { list.add(c.getUsername()); } return list; } }
Learn more about Auto complete text view visit

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

No comments:

Post a Comment