我很困惑,不知道如何使用startActivityResults和setResults从以前的活动中获取数据。我有一个视图类和一个活动类。
基本上在我的视图类中我有这个对话框,它实际上会启动名为colorActivity类的活动类。当用户选择“是”时,它也会将所选圆圈的名称传递给colorActivity类。在colorActivity类中,允许用户输入特定圆的颜色代码,我想将颜色代码传递回视图类。我使用startActivityForResult和setResult方法将值从活动传递回视图时出现问题。添加,如何利用获取的数据?
我的代码如下
从我的视图类中修改代码:
@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < circles.size(); i++) {
if (circles.get(i).contains(x, y)) {
circleID = i;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new Builder(
getContext());
final EditText text = new EditText(getContext());
builder.setTitle("Adding colors to circles").setMessage(
"Proceed to Enter color");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di,
int i) {
Intent intent = new Intent(
getContext(),
colorActivity.class);
intent.putExtra("circlename", circleNameList.get(circleID));
startActivityForResults(intent, 1); // error incurred here : The method startActivityForResult(Intent, int) is undefined for the type new DialogInterface.OnClickListener(){}
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di,
int i) {
}
});
builder.create().show();
}
}, 3000);
break;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) { // Please, use a final int instead of hardcoded
// int value
if (resultCode == RESULT_OK) {
ccode = (String) data.getExtras().getString("colorcode");
}
}
}
public static String getColorCode() {
return ccode;
}
在colorActivity中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_ecolor);
circlenametextview = (TextView)findViewById(R.id.circlenametextview);
String circlename = super.getIntent().getStringExtra("circlename");
circlenametextview.setText(circlename);//get the circle name
savebutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(colorActivity.this, ?????);//how to return back to the view class?
colorcode = colorEditText.getText().toString();// I am able to get value right up till this point
Intent resultIntent = new Intent();
resultIntent.putExtra("colorcode", colorcode );
setResult(Activity.RESULT_OK, resultIntent);
finish();
}// onclick
});
}
答案 0 :(得分:41)
更正其他代码以便您可以运行该程序后,您可以通过以下方式从活动colorActivity
中检索参数:
Intent resultIntent = new Intent();
resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();
覆盖@onActivityResult(...)
。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) { // Please, use a final int instead of hardcoded int value
if (resultCode == RESULT_OK) {
String value = (String) data.getExtras().getString("NAME OF THE PARAMETER");
参考
答案 1 :(得分:0)
尝试使用
ActivityName.this.startActivityForResult(intent,int)
哦,还有一件小事,在您使用的代码中
startActivityForResults(intent,int)..使用
替换它startActivityForResult(意向,INT)
答案 2 :(得分:0)
STARTACTIVITYFORRESULT 现已弃用
替代它和推荐的解决方案是使用 Activity Result API
您可以使用以 Kotlin 语言编写的此代码:
创建 ResultLauncher:
const someArray = [
[10, 11],
[20, 21],
[30, 31]
];
for (const value of someArray) {
console.log(value);
console.log(...value);
}
使用上述结果启动器启动 Activity:
private var resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
if (null != data && data.getBooleanExtra("REFRESH_PAGE", false)) {
//do your code here
}
}
}
从 val intent = Intent(this, XYZActivity::class.java)
resultLauncher.launch(intent)
返回结果
XYZActivity