SharedPreference不在复选框中

时间:2018-01-09 19:18:39

标签: android

我想根据对checkBox事件的偏好来填充另一个活动的TextView,但它的工作请帮助我排序问题..它是强制停止。请帮助if else语句部分。我们需要写什么活动2根据活动1中的复选框进行选择

活动1:

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,b2;
    public static CheckBox cb1,cb2,cb3;
    TextView tv1,tv2,tv3;
    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);
        b2=(Button)findViewById(R.id.b2);
        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);
        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();
        if(cb1.isChecked()==true) {

            editor.putString("price1", tv1.getText().toString());
            editor.putBoolean("x",true);
        }
        if(cb2.isChecked()==true)
        {

            editor.putString("price2",tv2.getText().toString());
            editor.putBoolean("y",true);

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

            editor.putString("price3",tv3.getText().toString());
            editor.putBoolean("z",true);
        }
        editor.commit();
        Intent i=new Intent(this,MyCart.class);
        startActivity(i);
    }
}

2.Activity

package com.example.rajatanurag.shoppingsites;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MyCart extends AppCompatActivity {
    SharedPreferences sp1;
    TextView tv4,tv5,tv6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_cart);
        tv4=(TextView)findViewById(R.id.tv4);
        sp1=getSharedPreferences("SHOPPING",MODE_PRIVATE);
        if(Book.cb1.isChecked()==true){
            String price11 = sp1.getString("price1", "");
            tv4.setText(price11);
        }
        if(Book.cb1.isChecked()==true){
            String price21=sp1.getString("price2","");
            tv5.setText(price21);
        }
        if(Book.cb1.isChecked()==true){ 
            String price31=sp1.getString("price3","");
            tv6.setText(price31);
        }

    }

}

2 个答案:

答案 0 :(得分:0)

您不应该为视图成员使用静态变量,因为您可能会遇到内存泄漏。当活动被销毁时,您引用的视图也会被销毁,因此在第二个活动中无法访问它们。您可以将复选框选中的值作为意图附加内容发送。

将它放在OnClick1()方法的活动1中:

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

这是您在活动2中获取值的方法(在onCreate()方法中):

Intent intent = getIntent();
if(intent.getBooleanExtra("cb1", false)){
    String price11 = sp1.getString("price1", "");
    tv4.setText(price11);
}
if(intent.getBooleanExtra("cb2", false)){
    String price21=sp1.getString("price2","");
    tv5.setText(price21);
}
if(intent.getBooleanExtra("cb3", false)){ 
    String price31=sp1.getString("price3","");
    tv6.setText(price31);
}

答案 1 :(得分:0)

Package 'Microsoft.Office.Interop.Excel 15.0.4795.1000' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

在你的第二个活动中

 public void OnClick1(View view)
 {
  if(view.getID() == R.id.cb1)
  {
    if(cb1.isChecked())
         editor.putBoolean("x",true);
     else
        editor.putBoolean("x",false);
   }
 }
相关问题