2

I have activity with list of products and I pass attributes to Basket activity

Code in products list activity:

    zakazat.Click += delegate 
        {
            var intent = new Intent(this, typeof(CartActivity));
            intent.PutExtra ("title", (string)(firstitem ["post_title"]));
            intent.PutExtra ("price", (string)(firstitem ["price"] + " грн"));
            intent.PutExtra ("weight", (string)(firstitem ["weight"] + "г"));
            StartActivity(intent);

Receiving products in Basket:

public  void Display (){

        LinearLayout display = FindViewById<LinearLayout> (Resource.Id.product1);         
        TextView productname = FindViewById<TextView> (Resource.Id.posttittle1);
        TextView price = FindViewById<TextView> (Resource.Id.price1);
        TextView weight = FindViewById<TextView> (Resource.Id.weight1);



        price.Text = Intent.GetStringExtra("price");

        productname.Text = Intent.GetStringExtra("title");

        if (productname.Text == Intent.GetStringExtra ("title")) {
            display.Visibility = ViewStates.Visible;
        } 
        else {
            display.Visibility = ViewStates.Gone;
        }

        weight.Text = Intent.GetStringExtra("weight");


    }

I have two questions, how save this attributes when I change activity and how to pass this attributes on background?

Any suggestions how I can realize this?

  • Possible duplicate of [How to transfer data from one activity to another in android](http://stackoverflow.com/questions/20169993/how-to-transfer-data-from-one-activity-to-another-in-android) – Zahan Safallwa Nov 19 '15 at 07:43
  • Nope. I realized transfer to another activity. I need to save attributes in activity as I wrote in end of post @ZahanSafallwa –  Nov 19 '15 at 07:46
  • what do you mean by save attribute? store data to file like thing??? – Zahan Safallwa Nov 19 '15 at 07:48
  • Save attributes when I switch activity, and when I open it again, see it. @ZahanSafallwa –  Nov 19 '15 at 07:51

1 Answers1

1

There are two things you can do.

  1. Save to SQLite
  2. Save to SharedPreferences.

If you do not want to rely on SQLite at this pace then you will need to use option #2 since it is easier and quick to implement.

How do we use the SharedPreferences?

First, you will have to declare an ISharedPreference on your class.

public class YourActivity : Activity 
{ 
    private ISharedPreferences prefs;
}

Next, you will need to initialize the prefs variable in your onCreate method.

prefs = PreferenceManager.GetDefaultSharedPreferences(this);

Then you can write your Intent extras to the preferences like this :

ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutString ("price", Intent.GetStringExtra("price"));
editor.PutString ("title", Intent.GetStringExtra("title"));
editor.PutString ("weight", Intent.GetStringExtra("weight"));
editor.Apply ();

After writing the preferences and you would like to access the data inside. You will have to do this :

string price = prefs.GetString("price", "0"); 
string title = prefs.GetString ("title" , ""); 
string weight = prefs.GetString ("weight" , ""); 
//second argument in GetString(arg1, arg2) means a default value given to the variable if it is null

By this, even closing the Activity you can still retrieve the SharedPreferences values.

Just to note, that you will need to add using Android.Preferences; to use Preferences.

vincent
  • 293
  • 3
  • 15