2011-09-04 35 views
2

我想回去Place.class這說明一個地方的全部信息。現在我能夠回到Place活動,但信息只顯示titleaddress ..我想可能在onTab方法發生了。它只有兩件事情是被投入了對意圖....如何通過傳遞值轉到下一個活動的AlertDialog意圖?

intent.putExtra(Constants.COL_TITLE, oi.getTitle()); 
intent.putExtra(Constants.COL_ADDRESS, oi.getSnippet()); 

如果我想把其他領域即contentphone等我如何把數據從數據庫到這個意圖是什麼?

intent.putExtra(Constants.COL_CON, ..........); 

謝謝您的幫助

我有3個班在這裏

xxx.class

public class xxx extends MapActivity { 

    MapView mapView; 
    MapController mapController; 
    private static MyDB mDbHelper; 
    private Cursor c; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.aboutcm); 

     mDbHelper = new MyDB(this); 
     mDbHelper.createDatabase(); 
     mDbHelper.open(); 
     c = mDbHelper.getAttraction(); 

     mapView = (MapView) findViewById(R.id.mapview); 
     mapController = mapView.getController(); 
     Drawable drawable = this.getResources().getDrawable(R.drawable.map_pin_3); 
     List<Overlay> mapOverlays = mapView.getOverlays(); 
     PlaceItemizedOverlay itemizedoverlay = new PlaceItemizedOverlay(drawable, this); 


     mapController.setZoom(13); 
     mapView.setBuiltInZoomControls(true); 


     c.moveToFirst(); 
     do { 

      String title = c.getString(c 
        .getColumnIndex(Constants.COL_TITLE)); 
      String address = c.getString(c 
        .getColumnIndex(Constants.COL_ADDRESS)); 
      String content = c.getString(c 
        .getColumnIndex(Constants.COL_CONTENT)); 
      int latitude = (int) (c.getDouble(c 
        .getColumnIndex(Constants.COL_LA)) * 1E6); 
      int longitude = (int) (c.getDouble(c 
        .getColumnIndex(Constants.COL_LONG)) * 1E6); 


      itemizedoverlay.addOverlay(new OverlayItem(new GeoPoint(latitude, longitude), title, 
        address)); 
      mapOverlays.add(itemizedoverlay); 

     } while (c.moveToNext()); 
     mDbHelper.close(); 

    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

PlaceItemizedOverlay.class

public class PlaceItemizedOverlay extends ItemizedOverlay<OverlayItem> { 

    private Context mContext; 
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 

    public PlaceItemizedOverlay(Drawable defaultMarker, Context context) 
    { 
     super(boundCenterBottom(defaultMarker)); 
     mContext = context; 
    } 
    public void addOverlay(OverlayItem overlay) { 
     mOverlays.add(overlay); 
     populate(); 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     // TODO Auto-generated method stub 
     return mOverlays.get(i); 
    } 

    @Override 
    public int size() { 
     // TODO Auto-generated method stub 
     return mOverlays.size(); } 


    @Override 
    protected boolean onTap(int index) { 

     final OverlayItem oi = mOverlays.get(index); 
     AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
     dialog.setTitle(oi.getTitle()); 
     dialog.setMessage(oi.getSnippet()); 
     dialog.setNegativeButton("Back", null); 
     dialog.setPositiveButton("See More Detail", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Intent intent = new Intent(mContext, Place.class); 
       intent.putExtra(Constants.COL_TITLE, oi.getTitle()); 
       intent.putExtra(Constants.COL_ADDRESS, oi.getSnippet()); 
       mContext.startActivity(intent); 
     }}); 
     dialog.show(); 
     return true; 

    } 


} 
+0

您在哪個活動中創建AlertDialog?您可以爲putExtra創建自己的密鑰。例如:intent.putExtra(「content」,content),然後在接收活動中:intent.getStringExtra(「content」);請參閱goo.gl/VbYcd – Jakar

+0

xxx擴展MapActiviy的活動。如您所見,我在PlaceItemizedOverlay.class的'onTab'方法中創建AlertDialog。順便說一下,intent.putExtra(「content」,content)應該保持onTab方法或外部方法。而且我也使用這一行Intent intent = new Intent(mContext,Place.class);但似乎mContext只有來自PlaceItemizedOverlay構造函數的2個值,它們是標題和地址......其他人呢?我嘗試了很多方法...我混淆了哪些值應該放在第二個內容中。(「content」,?????)我嘗試將i.putExtra(「content」,c.getColumnIndex(「內容」));但是,這不是工作 –

+0

非常感謝你,最後我得到了它 –

回答

1

比方說,您希望將xxx活動的字符串「123456789」傳遞給Place.class。那麼你可以使用intent.putExtra("content", "123456789")然後在Place.class intent.getStringExtra("content")將返回 「123456789」

難道我宣佈....意向書I =新意圖(??????,Place.class); }和 我應該把什麼放入??????

你可以做三件事之一。首先,我的首選方法是在類體內聲明:

Context context; 

然後在onCreate(...)聲明:

context = this; 

,然後你可以這樣做:

Intent i = new Intent(context, Place.class); 

其次,你可以聲明:

Intent i = new Intent(getApplicationContext(), Place.class); 

第三,在你的類主體可以聲明:

Intent i; 

然後在onCreate(...)

i = new Intent(this, Place.class); 

用第三種方法,那麼你將能夠在alertDialog

+0

最後一件事,我想知道..如果我創建dialog.setPositiveButton(「更詳細」,新的DialogInterface.OnClickListener(){...}和我@Override \t \t public void onClick(DialogInterface dialog ,int id){...我可以聲明....意圖我=新的意圖(??????,Place.class);}我應該怎麼把?????? ...如果你可以看看上面的代碼..我不能把(xxx.this,Place.class)或(mContext,Place.class)我應該怎麼做才能得到每個地方的準確結果?非常感謝你 –

0

這是使用startActivity(i)一個簡單的意圖傳遞程序:從主要意圖到第二意圖

public void nextButtonOnClick(View arg0) 
    { 
     Intent intent=(new Intent(MainActivity.this,SecondActivity.class)); 
     startActivity(intent); 
    } 

修改XML文件:

android:onClick="nextButtonOnClick" 
相關問題