Java 将数据从一个活动发送到另一个类(非活动)

Java 将数据从一个活动发送到另一个类(非活动),java,android,android-layout,sharedpreferences,Java,Android,Android Layout,Sharedpreferences,我想将数据从一个活动发送到另一个类,但第二个类不是活动。它扩展了框架布局 if (imageHolder1 != null) { FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER); ImageCel

我想将数据从一个活动发送到另一个类,但第二个类不是活动。它扩展了框架布局

if (imageHolder1 != null) {
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                Gravity.CENTER);
        ImageCell newView = new ImageCell(this);
        resourceId = R.drawable.tile;
        newView.setBackgroundResource(resourceId);
        // newView.setImageResource(resourceId);
        imageHolder1.addView(newView, lp);
        TextView tv =newView.getTextView();
        tv.setText("A");
        SharedPreferences pref = getSharedPreferences("A", MODE_WORLD_WRITEABLE);
        Editor edt = pref.edit();
        edt.putString("A", tv.toString());
        edt.commit();
        newView.getPointTxtView().setText("5");

        newView.mEmpty = false;
        newView.mCellNumber = -1;
        mLastNewCell = newView;
        mImageCount++;

        newView.setOnClickListener(this);
        newView.setOnLongClickListener(this);
        newView.setOnTouchListener(this);

    }

if (imageHolder2 != null) {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                    Gravity.CENTER);
            ImageCell newView = new ImageCell(this);
            resourceId = R.drawable.tile;
            // newView.setImageResource(resourceId);
            newView.setBackgroundResource(resourceId);
            imageHolder2.addView(newView, lp);
//          newView.getTextView().setText("B");
            TextView tv =newView.getTextView();
            tv.setText("B");
            SharedPreferences pref = getSharedPreferences("A", MODE_PRIVATE);
            Editor edt = pref.edit();
            edt.putString("A", (String) tv.getText());
            edt.commit();
            newView.getPointTxtView().setText("2");

            newView.mEmpty = false;
            newView.mCellNumber = -1;
            mLastNewCell = newView;
            mImageCount++;

            newView.setOnClickListener(this);
            newView.setOnLongClickListener(this);
            newView.setOnTouchListener(this);

        }
这是我的另一堂课

pref = getContext().getSharedPreferences("A", Context.MODE_PRIVATE);


    textView.setText(pref.getString("A", null));
    Log.e("ad", "awd" + pref.getString("A", null));
    Log.i("Position:", "" + mCellNumber);

    int column = mCellNumber % total_col;
    int row = mCellNumber / total_col;
    Log.i("Column:", "" + column);
    Log.i("Row:", "" + row);
但是我得到了android.widget.textview作为我的文本…

现在问题出在另一个类SharedReference不起作用,因为它不是活动。希望得到他人的帮助。

在其他类中创建静态对象,同时创建该类的实例,设置该对象的值

例如:

  XYZ xyz=new XYZ(context);
  xyz.somestaticvariable=somevalue;
同时,你也犯了一个简单的错误
tv.toString()
改用

tv.getText()

在其他类中创建静态对象,同时创建该类的实例,设置该对象的值

例如:

  XYZ xyz=new XYZ(context);
  xyz.somestaticvariable=somevalue;
同时,你也犯了一个简单的错误
tv.toString()
改用
tv.getText()

试试看

SharedPreferences pref = getContext().getSharedPreferences("A", Context.MODE_WORLD_WRITEABLE);
而不是使用

Editor edt = pref.edit();
edt.putString("A", tv.toString());
edt.commit();
使用

试试看

而不是使用

Editor edt = pref.edit();
edt.putString("A", tv.toString());
edt.commit();
使用


你不在乎你的课堂是否是活动。因为getSharedReference()是上下文类的方法

改变

SharedPreferences pref = getSharedPreferences("A", MODE_WORLD_WRITEABLE);


你不在乎你的课堂是否是活动。因为getSharedReference()是上下文类的方法

改变

SharedPreferences pref = getSharedPreferences("A", MODE_WORLD_WRITEABLE);


任何布局类中的SharedReferences对象

this.getContext().getSharedPreferences("A", MODE_WORLD_WRITEABLE);

请确保上下文应该是相同的,您用于保存SharedReferences中的任何对象的上下文…希望这会有所帮助。

任何布局类中的SharedReferences对象

this.getContext().getSharedPreferences("A", MODE_WORLD_WRITEABLE);

请确保上下文应该相同,您用于保存SharedReferences中的任何对象的上下文…希望这会有所帮助。

您可以像这样通过constrator传递值

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />
}

或者您可以创建类扩展应用程序并在其上定义SharedReference,如下所示

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />
}

然后,您可以像这样调用SharedReference

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />
确保在AndroidManifest.xml中设置应用程序名称,如下所示

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />


您可以在project的任何位置调用SharedReferences

您可以像这样通过constrator传递值

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />
}

或者您可以创建类扩展应用程序并在其上定义SharedReference,如下所示

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />
}

然后,您可以像这样调用SharedReference

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />
确保在AndroidManifest.xml中设置应用程序名称,如下所示

public class YourClass extends FrameLayout {

private String value;

public YourClass(Context context, String value) {
    super(context);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, String value) {
    super(context, attrs);
    this.value = value;
}

public YourClass(Context context, AttributeSet attrs, int defStyle, String value) {
    super(context, attrs, defStyle);
    this.value = value;
}
public class YourClass extends Application {

private static SharedPreferences sharedPreferences;

public static SharedPreferences getSharedPreferences() {
    return sharedPreferences;
}

@Override
public void onCreate() {
    super.onCreate();
    YourClass.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
}
SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
<application
        android:name=".YourClass" .... />


您可以在project的任何位置调用SharedReference

您可以在非活动类中使用
SharedReference

SharedReference
上下文相关。因此,解决方案是:

  • 活动
    类中定义静态
    上下文

    公共静态上下文mAppContext

  • 使用
    getApplicationContext
    方法初始化中的
    mAppContext

    public void onCreate(){ mAppContext=getApplicationContext(); }

  • 使用
    YourActivity.mAppContext
    获取
    SharedReferences

    SharedReferences prefs=YourActivity.mAppContext.GetSharedReferences(“A”,模式为“世界”可写)


  • 您可以在非活动类中使用
    SharedReference

    SharedReference
    上下文相关。因此,解决方案是:

  • 活动
    类中定义静态
    上下文

    公共静态上下文mAppContext

  • 使用
    getApplicationContext
    方法初始化中的
    mAppContext

    public void onCreate(){ mAppContext=getApplicationContext(); }

  • 使用
    YourActivity.mAppContext
    获取
    SharedReferences

    SharedReferences prefs=YourActivity.mAppContext.GetSharedReferences(“A”,模式为“世界”可写)



  • 不能使用SharedReference吗?你能从我上面的代码中告诉我如何发送数据吗?是的,它可以通过SharedReference发送,但我建议通过SharedReference发送数据。因此,基本上你能详细说明如何在我上面的代码中发送数据以从共享首选项中获取数据吗?使用preference.getString(“key”,defaultvalue);告诉我你是怎么做的不,我已经更新了我的代码,但是我的文本设置为“android.widget.Textviews”,而不是文本。使用SharedReference不可能吗?你能从我上面的代码中告诉我如何发送数据吗?是的,它可以通过SharedReference发送,但我建议通过SharedReference发送数据。因此,基本上你能详细说明如何在我上面的代码中发送数据以从共享首选项中获取数据吗?使用preference.getString(“key”,defaultvalue);告诉我你是怎么做的不,我已经更新了我的代码,但是我的文本设置为“android.widget.Textviews”,而不是textSP有什么问题吗?我不能调用SharedReferences pref=GetSharedReferences(“A”,MODE\u WORLD\u WRITEABLE);对于非活动类,需要使用getSharedReferences()方法的上下文。像context.getSharedReferences.your的问题现在解决了??不,我已经更新了我的代码,但是我的文本设置为“android.widget.Textviews”,而不是textSP有什么问题吗?我不能调用SharedReferences pref=getSharedReferences(“A”,MODE\u WORLD\u WRITEABLE);对于非活动类,需要使用getSharedReferences()方法的上下文。像context.getSharedReferences.your问题现在解决了??不,我已经更新了我的代码,但是我的文本设置为“android.widget.Textviews”,而不是text简单地使用context.MODE\u WORLD\u WRITEABLE@MehdiNo,我已经更新了我的代码,但是我的文本设置为“android.widget.Textviews”与textBro不同的是,我现在在另一个类中为不同的textview设置了两个不同的值,我想使用SharedReference显示选定的textview值。我已经更新了我的代码。看看它。只需使用Context.MODE\u WORLD\u WRITEABLE@MehdiNo,我已经更新了我的代码,但我的文本设置为“android.widget.Textviews”而不是textBro。有一个问题,我为diff设置了两个不同的值