Java 如何将结果从alertDialog传递到onActivityResult
我有活动A和活动B。 我使用onActivityResult,但我有一个问题: java.lang.RuntimeException:传递结果失败 ResultInfo{who=null,request=3,result=-1,data=Intent{(有附加项) }}到活动{com.example.sellcar/com.example.sellcar.View_Offer}: android.database.CursorIndexOutOfBoundsException:请求索引0, 大小为0的 我想我无法将这种方式的“结果”从alertDialog传递到onActivityResult。 我不知道如何解决这个问题:/ 请帮忙 活动A:Java 如何将结果从alertDialog传递到onActivityResult,java,android,sqlite,android-activity,sdk,Java,Android,Sqlite,Android Activity,Sdk,我有活动A和活动B。 我使用onActivityResult,但我有一个问题: java.lang.RuntimeException:传递结果失败 ResultInfo{who=null,request=3,result=-1,data=Intent{(有附加项) }}到活动{com.example.sellcar/com.example.sellcar.View_Offer}: android.database.CursorIndexOutOfBoundsException:请求索引0, 大小
bBUTTON.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity_A.this,Activity_B.class);
startActivityForResult(intent, 3);
}
});
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 3 && resultCode == RESULT_OK){
String pId = data.getStringExtra("MyData");
Toast.makeText(Activity_A.this,pId,Toast.LENGTH_LONG).show();
}
}
AlertDialog.Builder builder=new AlertDialog.Builder(View_Sell.this);
builder.setTitle("UWAGA !").setMessage("blablabla");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String ostatnioDodanaSprzedaz="XYZ";
Intent intent = new Intent();
intent.putExtra("MyData", ostatnioDodanaSprzedaz);
setResult(RESULT_OK, intent);
onBackPressed();
} });
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
} });
AlertDialog ad = builder.create();
ad.show();
活动B:
bBUTTON.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity_A.this,Activity_B.class);
startActivityForResult(intent, 3);
}
});
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 3 && resultCode == RESULT_OK){
String pId = data.getStringExtra("MyData");
Toast.makeText(Activity_A.this,pId,Toast.LENGTH_LONG).show();
}
}
AlertDialog.Builder builder=new AlertDialog.Builder(View_Sell.this);
builder.setTitle("UWAGA !").setMessage("blablabla");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String ostatnioDodanaSprzedaz="XYZ";
Intent intent = new Intent();
intent.putExtra("MyData", ostatnioDodanaSprzedaz);
setResult(RESULT_OK, intent);
onBackPressed();
} });
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
} });
AlertDialog ad = builder.create();
ad.show();
尝试替换:
setResult(RESULT_OK, intent);
onBackPressed();
关于:
尝试替换:
setResult(RESULT_OK, intent);
onBackPressed();
关于:
您可以遵循观察者模式 首先,在Activity类本身中创建一个接口:
public interface ResultListener {
void onResultSet(String text);
}
然后,全局创建ResultListener
的对象:
ResultListener = rl;
在onCreate方法中实现onResultSet(字符串文本)方法:
rl = new ResultListener() {
@Override
public void onPositiveResult(String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
};
接下来,创建AlertDialog
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("UWAGA !").setMessage("blablabla");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String ostatnioDodanaSprzedaz="XYZ";
tl.onResultSet(ostatnioDodanaSprzedaz);
// onBackPressed();
} });
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
} });
builder.show();
以下是最终代码的外观:
public class MainActivity extends Activity {
ResultListener rl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
rl = new ResultListener() {
@Override
public void onPositiveResult(String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("UWAGA !").setMessage("blablabla");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String ostatnioDodanaSprzedaz="XYZ";
rl.onResultSet(ostatnioDodanaSprzedaz);
// onBackPressed();
} });
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
} });
builder.show();
} //onCreate closed
public interface ResultListener {
void onResultSet(String text);
}
} //MainActivity Class closed
您可以遵循观察者模式 首先,在Activity类本身中创建一个接口:
public interface ResultListener {
void onResultSet(String text);
}
然后,全局创建ResultListener
的对象:
ResultListener = rl;
在onCreate方法中实现onResultSet(字符串文本)方法:
rl = new ResultListener() {
@Override
public void onPositiveResult(String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
};
接下来,创建AlertDialog
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("UWAGA !").setMessage("blablabla");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String ostatnioDodanaSprzedaz="XYZ";
tl.onResultSet(ostatnioDodanaSprzedaz);
// onBackPressed();
} });
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
} });
builder.show();
以下是最终代码的外观:
public class MainActivity extends Activity {
ResultListener rl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
rl = new ResultListener() {
@Override
public void onPositiveResult(String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("UWAGA !").setMessage("blablabla");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String ostatnioDodanaSprzedaz="XYZ";
rl.onResultSet(ostatnioDodanaSprzedaz);
// onBackPressed();
} });
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
} });
builder.show();
} //onCreate closed
public interface ResultListener {
void onResultSet(String text);
}
} //MainActivity Class closed
我试着用finish()代替onBackPressed(),但不起作用我试着用finish()代替onBackPressed(),但不起作用这很奇怪,因为我试过你的代码,它通常适用于mesory,它起作用:)这也是另一个错误。谢谢你的帮助这很奇怪,因为,我试过你的代码,它对mesory正常工作,它工作:)这也是另一个错误。谢谢你的帮助