正如我的代碼所示,我想根據專案的位置打開一個新的活動,點擊串列視圖。我對每個位置都有這個 if else 陳述句。它作業正常,因為我的串列只需要 3 個專案,但是如果我有一個很長的串列(比如 20 個專案)怎么辦?我怎樣才能得到點擊的專案的位置?
感謝您抽出寶貴時間來回答。這是我當前的代碼
lv_places.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,savedAddresses));
lv_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0) {
// open map activity
Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
i.putExtra("lat", savedLocations.get(0).getLatitude());
i.putExtra("long", savedLocations.get(0).getLongitude());
startActivity(i);
}
else if(position == 1){
// open map activity
Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
i.putExtra("lat", savedLocations.get(1).getLatitude());
i.putExtra("long", savedLocations.get(1).getLongitude());
startActivity(i);
}
else if(position == 2){
// open map activity
Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
i.putExtra("lat", savedLocations.get(2).getLatitude());
i.putExtra("long", savedLocations.get(2).getLongitude());
startActivity(i);
}
else if(position == 3){
// open map activity
Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
i.putExtra("lat", savedLocations.get(3).getLatitude());
i.putExtra("long", savedLocations.get(3).getLongitude());
startActivity(i);
}
}
});
uj5u.com熱心網友回復:
您有一個位置編號作為引數,因此您可以優化您的代碼:
lv_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
i.putExtra("lat", savedLocations.get(position).getLatitude());
i.putExtra("long", savedLocations.get(position).getLongitude());
startActivity(i);
}
});
uj5u.com熱心網友回復:
lv_places.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, savedAddresses));
lv_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position/*weGetVariableHere*/, long id) {
// open map activity
Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
i.putExtra("lat", savedLocations.get(position/*variableUsedHere*/).getLatitude());
i.putExtra("long", savedLocations.get(position/*variableUsedHere*/).getLongitude());
startActivity(i);
}
});
我們正在使用變數本身而不是 if-else-elseif 它適用于n專案數
uj5u.com熱心網友回復:
您可以使用如下所示的直接位置
lv_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// open map activity
Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
i.putExtra("lat", savedLocations.get(position).getLatitude());
i.putExtra("long", savedLocations.get(position).getLongitude());
startActivity(i);
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403191.html
標籤:
上一篇:我正在傳遞日期索引而不是預期日期
下一篇:本地廣播收不到廣播
