[英]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. 它扩展了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.... 但是我将android.widget.textview作为文本...
Now the problem is in another class sharedpreference is not working because it is not an activity. 现在问题出在另一个类中,因为sharedpreference不是活动,所以它不起作用。 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 任何Layout类中的SharedPreferences对象
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. 请确保上下文应该相同,这是您用来在SharedPreferences中保存任何对象的那个。希望这会有所帮助。
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 或者您可以像这样创建类扩展Application并在其上定义SharedPreferences
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 sharedPreferences = YourClass.getSharedPreferences();
sharedPreferences.getString("","");
YourClass.getSharedPreferences().getString("","");
and be sure to set application name in AndroidManifest.xml like this 并确保像这样在AndroidManifest.xml中设置应用程序名称
<application
android:name=".YourClass" .... />
you can call SharedPreferences anywhere on project 您可以在项目中的任何地方调用SharedPreferences
You can use SharedPreference
in non-Activity class. 您可以在非活动类中使用SharedPreference
。
SharedPreference
is related to Context
. SharedPreference
与Context
有关。 So, The solution is: 因此,解决方案是:
Define a static Context
class in an Activity
class. 在Activity
类中定义一个静态Context
类。
public static Context mAppContext; 公共静态上下文mAppContext;
Initialize the mAppContext
in using the getApplicationContext
method. 使用getApplicationContext
方法初始化mAppContext
。
public void onCreate() { mAppContext= getApplicationContext(); 公共无效onCreate(){mAppContext = getApplicationContext(); } }
Get the SharedPreferences
with the YourActivity.mAppContext
. 使用YourActivity.mAppContext
获取SharedPreferences
。
SharedPreferences prefs = YourActivity.mAppContext.getSharedPreferences("A", MODE_WORLD_WRITEABLE); 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 在创建该类的该对象的setvalue实例的同时,在其他类中创建一个静态对象
For eg: 例如:
XYZ xyz=new XYZ(context);
xyz.somestaticvariable=somevalue;
Also you are doing a simple mistake you are using tv.toString();
同样,您在使用tv.toString();
犯了一个简单的错误tv.toString();
instead use tv.getText();
而是使用tv.getText();
You don't care about your class is activity or not. 您不在乎您的课堂是否活动。 Because getSharedPreference() is a method of Context class. 因为getSharedPreference()是Context类的方法。
Change 更改
SharedPreferences pref = getSharedPreferences("A", MODE_WORLD_WRITEABLE);
to 至
SharedPreferences pref = getContext().getSharedPreferences("A", MODE_WORLD_WRITEABLE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.