0

我有活动 A(具有 Listview 的自定义适配器),在“自定义适配器”的代码中,我想调用 AlertDialog,它将向我显示第二个活动(活动 B)。

我可以完美地展示活动,但我想知道如何在活动 A 和活动 B 之间传递参数?

CustomAdapter.java:

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”在活动之间传递参数。所以,再次,在“alertD.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 错误(就像,活动没有初始化然后它检索错误。)

谢谢。

4

2 回答 2

0

解决了。

我对 AlertDialog 的看法是要有这样的窗口。我发现我们可以将那种类型的窗口(对话框)设置到我们的主题中。因此,现在一切都变得更容易和可能。

// styles.xml
<style name="MyTheme" parent="android:style/Theme.Dialog">
       <item name="android:windowNoTitle">true</item>
</style>
<-------------->
// Manifest.xml
<activity
    android:name="com.example.MyExample.activity_B"
    android:label="@string/activity_B"
    android:windowSoftInputMode="stateHidden" 
    android:theme="@style/MyTheme">
</activity>

然后我可以在我的自定义适配器中使用:

Intent i = new Intent(getContext(), activity_B.class);
i.putExtra("field", "value");
context.startActivity(i);

谢谢。

于 2013-10-08T10:25:53.510 回答
0

尝试这个-

String title1 = this is the title;
Intent i = new Intent(context, activityB.class);
i.putExtra("title", title1);

然后从你的新活动中提取它-

Intent intent = getIntent();
String title1 = intent.getExtras.getString("title");
于 2013-10-08T10:29:09.363 回答