2017-04-06 19 views
-1

如何將id中顯示的id傳遞給另一個地圖活動?如何傳遞用戶標識來映射列表適配器的活動?

好友列表:

Friends list

地圖活動:

enter image description here

我已經在ListAdapter

btnLocate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      stringID=getItem(position).getID(); 
      Toast.makeText(context, "Friend ID"+stringID, Toast.LENGTH_LONG).show(); 
      Intent mapScreen = new Intent(context, MapActivity.class); 
      mapScreen.putExtra("ID",getItem(position).getID()); 
      context.startActivity(mapScreen); 

     } 
    }); 

做這個代碼和這些代碼MapActivity.java

@Override 
     protected String doInBackground(Void... params) { 
      String response; 
      Intent listScreen= getIntent(); 
      Log.d(MainActivity.LOG_TAG,"UID List1"+ listScreen.getStringExtra("ID")); 
      String body = "UID=" + listScreen.getStringExtra("ID"); 
      Log.d(MainActivity.LOG_TAG,"UID List2"+ listScreen.getStringExtra("ID")); 
      response = http.postRequest(HttpConfigs.URL_COORDINATES, body); 
      return response; 
     }` 
+0

是什麼意思*另一個地圖活動朋友列表*?對於當前的MapActivity,你做得很好。 – tahsinRupam

+0

我想你應該先從你的MapActivity的創建方法中獲得你的ID,然後在你的AsyncTask中使用它。你的代碼看起來很好。 – Oussaki

+0

@Waseem你在MapActivity中沒有獲得ID嗎?還是出現錯誤? –

回答

0

您可以在MapActivity中創建一個名爲'recievedID'的實例變量,然後可以獲取該ID並將其存儲到該實例變量中。

在你MapActivity像onCreate方法獲取的ID的例子:

private String recievedID = "" 

onCreate(...){ 
    Intent listScreen= getIntent(); 
    String recievedID = listScreen.getStringExtra("ID"); 
    ... 
} 

在您的AsyncTask使用它:

@Override 
    protected String doInBackground(Void... params) { 
     String response; 
     String body = "UID=" + recievedID; 
     Log.d(MainActivity.LOG_TAG,"body = "+ body); 
     response = http.postRequest(HttpConfigs.URL_COORDINATES, body); 
     return response; 
    } 

我希望這有助於。

0

在您的適配器類嘗試創建自己的公共接口,如:

public interface ListItemClickListener { 
    void onItemClicked(int position); 
} 

那麼你的適配器類中做的也:

ListItemClickListener listener; 
現有ListListener把裏面

然後:

listener.onItemClicked(itemId); 

其中itemId是您從該點擊偵聽器獲得的內容。

現在你必須去你的活動並實現你的界面。這將生成覆蓋方法onItemClicked(int position) {}。 在這個方法裏面你可以得到你的id。

@Override 
private void onItemClicked(int position) { 
    this.position = position; 
} 

現在,一旦你在列表中選擇一個項目,然後按下它,它也會觸發活動的方法,並在那個時候你會得到itemId

相關問題