Tuesday, July 16, 2013

Store and retrive the values using Shared Preferences and display in ListView using Android

2 comments :
Hi 

This  is an example for store and retrive the shared prefernces and display in listview


MainActivity.java

package com.example.sample;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
SharedPreferences.Editor fd;
SharedPreferences FeedPref;
EditText  txt1;
EditText txt2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1=(EditText)findViewById(R.id.editText1);
    txt2=(EditText)findViewById(R.id.editText2);

Button submit=(Button)findViewById(R.id.button1);
 FeedPref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
       fd=FeedPref.edit();        

submit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

txt1=(EditText)findViewById(R.id.editText1);
    txt2=(EditText)findViewById(R.id.editText2);

  FeedPref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
     fd=FeedPref.edit();
 String u_name=txt1.getText().toString();
 String pwdtxt=txt2.getText().toString();
 fd.putString("uname", u_name);
 fd.putString("pwd",pwdtxt);
 fd.commit();
  Intent sample2=new Intent(MainActivity.this,sharedlist.class);
  startActivity(sample2);

}
});

}


}


Sharedlist.java:

package com.example.sample;
import java.util.Map;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.view.Menu;
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.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class sharedlist extends Activity {
String[] presidents;
ListView listView;
ArrayAdapter<String> adapter;
SharedPreferences FeedPref;
SharedPreferences.Editor fd;
TextView txt1,txt2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

        listView = (ListView) findViewById(R.id.list);
     
 FeedPref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
      String  uname=FeedPref.getString("uname",null);
        String  password=FeedPref.getString("pwd", null);
        String[] values = new String[] {uname,password};
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);



listView.setAdapter(adapter);

}
}

activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="122dp"
        android:text="@string/username" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:text="@string/password" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:hint="enter the username" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/editText2"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="24dp"
        android:text="@string/submit"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="22dp"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/cancel" />

</RelativeLayout>

second.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

         <ListView
              android:id="@+id/list"
              android:layout_height="wrap_content"
              android:layout_width="match_parent">
         </ListView>


</LinearLayout>



     
     

2 comments :