简体   繁体   中英

Send data from one activity to another class(Not Activity)

I want to send data from one activity to another class but the second class is not an activity. It extends Framelayout.

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);

        }

Here my another class

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);

But i am get android.widget.textview as my text....
Now the problem is in another class sharedpreference is not working because it is not an activity. Hope to receive help from someone.

Try,

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

Instead of using

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

use

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

SharedPreferences object in any Layout class

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

Please make sure Context should be same,which one you used for saving any object in SharedPreferences...Hope this will help.

you can pass values by constrictor like this

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;
}

}

or you can create class extend Application and define SharedPreferences on it like this

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());
}

}

then, you can call SharedPreferences like this

SharedPreferences sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");

and be sure to set application name in AndroidManifest.xml like this

<application
        android:name=".YourClass" .... />

you can call SharedPreferences anywhere on project

You can use SharedPreference in non-Activity class.
SharedPreference is related to Context . So, The solution is:

  1. Define a static Context class in an Activity class.

    public static Context mAppContext;

  2. Initialize the mAppContext in using the getApplicationContext method.

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

  3. Get the SharedPreferences with the YourActivity.mAppContext .

    SharedPreferences prefs = YourActivity.mAppContext.getSharedPreferences("A", MODE_WORLD_WRITEABLE);

Create a static object in other class while creating instance of that class setvalue of that object

For eg:

  XYZ xyz=new XYZ(context);
  xyz.somestaticvariable=somevalue;

Also you are doing a simple mistake you are using tv.toString(); instead use tv.getText();

You don't care about your class is activity or not. Because getSharedPreference() is a method of Context class.

Change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM