2013-10-08 34 views
0

我有活性的(其具有對列表視圖的定製適配器),在「自定義適配器」的代碼,我想呼叫的AlertDialog它將顯示我的第二活性(活性乙)。的Android AlertDialog如何傳遞ARGS

我可以顯示完美的活動,但我想知道如何傳遞活動A和活動B之間ARGS?

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異常的錯誤(這就像,活動並沒有對其進行初始化檢索錯誤。)

感謝。

+0

問題提取它是不明確,在i.putExtra( 「稱號」 也發佈logcat的 – Riskhan

+0

,「這是標題」));你寫兩個右括號。也許你沒有收到任何東西的原因。 –

回答

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); 

感謝。

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");