gpt4 book ai didi

android - 单击刷新按钮在 android 的 ListView 中每 5 秒显示一次纬度和经度列表

转载 作者:行者123 更新时间:2023-11-30 04:27:33 26 4
gpt4 key购买 nike

嗨,我的应用程序包含一个刷新按钮,单击它我想在 android 的 ListView 中每 5 秒显示一次经度和纬度列表。我试过这个东西。

我的主课是:-

public class MapdemoActivity extends ListActivity
{
private ArrayList<MapData> mapdata = new ArrayList<MapData>();
private MapDataAdapter m_adapter;

public String lng;
public String lat;

private Button retrieveLocationButton;
private LocationManager locationManager;

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; //1 meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 5000; //5 seconds

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

retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);

this.m_adapter = new MapDataAdapter(this, R.layout.row, mapdata);
setListAdapter(this.m_adapter);

**retrieveLocationButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{

}
});**

}

private class MyLocationListener implements LocationListener {

public void onLocationChanged(Location location)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null)
{
lat=Double.toString(location.getLatitude());
lng=Double.toString(location.getLongitude());

MapData data = new MapData();

data.setLatitude(lat);
data.setLongitude(lng);

mapdata.add(data);

Log.i("Lat", mapdata.get(0).Latitude);
Log.i("Lng", lng+location.getLatitude());
}
}

public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MapdemoActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}

public void onProviderDisabled(String s) {
Toast.makeText(MapdemoActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}

public void onProviderEnabled(String s) {
Toast.makeText(MapdemoActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}

}

private class MapDataAdapter extends ArrayAdapter<MapData>
{

private ArrayList<MapData> items;

public MapDataAdapter(Context context, int textViewResourceId, ArrayList<MapData> items)
{
super(context, textViewResourceId, items);
this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
MapData o = items.get(position);
if (o != null)
{
TextView lat = (TextView) v.findViewById(R.id.tt1);
TextView lng = (TextView) v.findViewById(R.id.tt2);
if (lat != null)
{
lat.setText("Latitude: "+o.getLatitude());
}
if(lng != null)
{
lng.setText("Longitude: "+ o.getLongitude());
}
}
return v;
}
}

}

map 数据类

 package com.ap.map.demo;

public class MapData
{

public String Longitude;
public String Latitude;

public String getLatitude() {
return Latitude;
}
public void setLatitude(String latitude) {
this.Latitude = latitude;
}
public String getLongitude() {
return Longitude;
}
public void setLongitude(String longitude) {
this.Longitude = longitude;
}
}

应该向刷新按钮的 onclick 添加什么,以便在 Android 中将更改添加到列表中?

谁能帮我解决这个问题。

最佳答案

只需调用适配器的 notifyDataSetChanged() 方法来刷新(通知)listView。

retrieveLocationButton.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v)
{

MapData data = new MapData();

data.setLatitude("40.4700");
data.setLongitude("78.9800");
mapdata.add(data);

m_adapter.notifyDataSetChanged();
}
});

关于android - 单击刷新按钮在 android 的 ListView 中每 5 秒显示一次纬度和经度列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8210127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com