java Android AlertDialog如何传递参数
在“自定义适配器”的代码中,我有活动A(它有Listview的自定义适配器),我想调用AlertDialog,它将显示第二个活动(活动B)
我可以完美地显示活动,但我想知道如何在活动A和活动B之间传递args
自定义适配器。爪哇:
view_details.setClickable(true);
view_details.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.activity_activity_B, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptView);
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
首先,我只想把下面的代码放在'alertD'下面。show()':
TextView title_ = (TextView) v.findViewById(R.id.title_B); // Activity B
title_.setText("Example");
但是没有起作用。然后我考虑使用“Bundle”在活动之间传递参数。所以,再一次,在“警报”之后。show()':
Intent i = new Intent(context, activityB.class);
i.putExtra("title", "this is the title"));
// And get this way in ActivityB:
// Bundle extras = getIntent().getExtras();
// String g = extras.getString("title");
也没用。 使用最后一段代码,我没有收到任何错误,但它也不显示信息。使用“setText”时,我收到一个NullPointerException错误(就像活动没有初始化然后检索错误一样)
谢谢
# 1 楼答案
解决了
我对AlertDialog的观点是要有这样的窗口。我发现我们可以在主题中设置这种类型的窗口(对话框)。因此,现在一切都变得容易和可能
然后我可以在自定义适配器中使用:
谢谢
# 2 楼答案
试试这个-
然后从你的新活动中提取-