共享首选项不起作用?

时间:2018-01-13 22:14:25

标签: android

实际上我试图使用共享首选项制作一个演示购物车,但数据不存储在购物车中。我使用了三本书并将其添加到购物车中但是当我点击进入购物车的主页时,存储在购物车中的数据每次都被删除。请帮我解决问题。必须有一些小错误共享首选项

Home
package com.example.rajatanurag.shoppingsites;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class Home extends AppCompatActivity {
    SharedPreferences sp1;
    Button bh1;
    Intent i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        bh1 = (Button) findViewById(R.id.bh1);
        sp1 = getSharedPreferences("SHOPPING", MODE_PRIVATE);


    }

    public void onClick2(View view) {
        if(sp1.getBoolean("isTrue",false))
        {
            Intent intent = new Intent(Home.this, MyCart.class);
            startActivity(intent);
        }
       else {
            Intent intent = new Intent(Home.this, Home.class);
            startActivity(intent);
        }


    }





    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mymenu, menu);
        return super.onCreateOptionsMenu(menu);


    }


    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.page1) {
            i = new Intent(Home.this, Book.class);
            startActivity(i);
        } else if (item.getItemId() == R.id.page2) {
            i = new Intent(Home.this, Electronics.class);
            startActivity(i);

        }


        return super.onOptionsItemSelected(item);

    }



}




Books

package com.example.rajatanurag.shoppingsites;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class Book extends AppCompatActivity {
    Button b1;
   CheckBox cb1,cb2,cb3;
    TextView tv1,tv2,tv3,tv7,tv8,tv9;
     SharedPreferences sp1;
    ImageView iv1,iv2,iv3;
    String x,y,z;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);
        b1=(Button)findViewById(R.id.b1);
        cb1=(CheckBox)findViewById(R.id.cb1);
        cb2=(CheckBox)findViewById(R.id.cb2);
        cb3=(CheckBox)findViewById(R.id.cb3);
        tv1=(TextView)findViewById(R.id.tv4);
        tv2=(TextView)findViewById(R.id.tv2);
        tv3=(TextView)findViewById(R.id.tv3);
        tv7=(TextView)findViewById(R.id.tv7);
        tv8=(TextView)findViewById(R.id.tv8);
        tv9=(TextView)findViewById(R.id.tv9);
        iv1=(ImageView)findViewById(R.id.iv1);
        iv2=(ImageView)findViewById(R.id.iv2);
        iv3=(ImageView)findViewById(R.id.iv3);
        sp1=getSharedPreferences("SHOPPING",MODE_PRIVATE);


    }

    public void OnClick1(View view)
    {
        SharedPreferences.Editor editor = sp1.edit();
        //editor.putString("book1",tv1.getText().toString());
        editor.putBoolean("isTrue",true);
        if(cb1.isChecked()==true) {

            editor.putString("price1", tv1.getText().toString());
            editor.putString("name1",tv7.getText().toString());
            editor.commit();


        }

        if(cb2.isChecked()==true)
        {

            editor.putString("price2",tv2.getText().toString());
            editor.putString("name2",tv8.getText().toString());
            editor.commit();



        }


        if(cb3.isChecked()==true)
        {

            editor.putString("price3",tv3.getText().toString());
            editor.putString("name3",tv9.getText().toString());
            editor.commit();



        }


        Intent i1=new Intent(this,MyCart.class);
        i1.putExtra("cb1", cb1.isChecked());
        i1.putExtra("cb2", cb2.isChecked());
        i1.putExtra("cb3", cb3.isChecked());
        startActivity(i1);

        //   Intent i=new Intent(this,MyCart.class);
     //   startActivity(i);








    }


}


MyCart


package com.example.rajatanurag.shoppingsites;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyCart extends AppCompatActivity {
    SharedPreferences sp1;
   public  String price11,price21,price31,name11,name12,name13;
     TextView tv4,tv5,tv6,tv10,tv11,tv12,tv20;
        Button b2h,p2c;
    int add;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_cart);
        tv4=(TextView)findViewById(R.id.tv4);
        tv5=(TextView)findViewById(R.id.tv5);
        tv10=(TextView)findViewById(R.id.tv10);
        tv11=(TextView)findViewById(R.id.tv11);
        tv12=(TextView)findViewById(R.id.tv12);
        tv6=(TextView)findViewById(R.id.tv6);
        tv20=(TextView)findViewById(R.id.tv20);
        b2h=(Button)findViewById(R.id.b2h);
        p2c=(Button)findViewById(R.id.p2c);
        sp1=getSharedPreferences("SHOPPING",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp1.edit();
            int x=0,y=0,z=0;
        Intent intent=getIntent();
           // if(Book.cb1.isChecked()==true){
            if(intent.getBooleanExtra("cb1",false)){
               price11 = sp1.getString("price1", "");
                 x=Integer.parseInt(price11);
                name11 = sp1.getString("name1", "");
                editor.commit();

                tv4.setText(price11);

                tv10.setText(name11);
        }
           // if(Book.cb2.isChecked()==true){
        if(intent.getBooleanExtra("cb2",false)){
                 price21=sp1.getString("price2","");
                y=Integer.parseInt(price21);
                 name12 = sp1.getString("name2", "");
            editor.commit();

                tv5.setText(price21);
                tv11.setText(name12);
        }
               // if(Book.cb3.isChecked()==true){
        if(intent.getBooleanExtra("cb3",false)){
                 price31=sp1.getString("price3","");
                     z=Integer.parseInt(price31);
                    name13 = sp1.getString("name3", "");
            editor.commit();

                    tv6.setText(price31);
                    tv12.setText(name13);}
        add=x+y+z;

        //SharedPreferences.Editor editor=sp1.edit();
       // editor.commit();
    }

    public void BackToHome(View view)
    {
        SharedPreferences.Editor editor=sp1.edit();
        sp1.getBoolean("isTrue",false);
        editor.commit();
        Intent i=new Intent(this,Home.class);
        startActivity(i);
    }

    public void Checkout(View view)

    {

        tv20.setText(Integer.toString(add));


    }



}``

0 个答案:

没有答案
相关问题