gpt4 book ai didi

java - 将数据从一个 Activity 发送到另一类(不是 Activity )

转载 作者:行者123 更新时间:2023-12-01 18:26:44 25 4
gpt4 key购买 nike

我想将数据从一个 Activity 发送到另一个类,但第二个类不是 Activity 。它扩展了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);

}

这是我的另一个类

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 作为我的文本....
现在的问题是在另一个类中,sharedpreference 不起作用,因为它不是一个 Activity 。希望得到某人的帮助。

最佳答案

尝试,

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.getText().toString());
edt.commit();

关于java - 将数据从一个 Activity 发送到另一类(不是 Activity ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25864979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com