嗨,我是新来的,我希望能够提出一些问题和答案。
我的问题是我正在构建一个使用自定义适配器来确定列表的应用程序。此适配器获取多个对象的位置并将其显示在行中的字段上,但是当我滚动列表时,位置随机交换。
这是我的适配器:
public class Adapter extends ArrayAdapter<myObject> implements ListAdapter {
private double currentLongitude;
private double currentLatitude;
private Context context;
private List<myObject> data;
private static class ViewHolder {
TextView myObjectDistance;
TextView myObjectName;
}
public LogSampleCellAdapter(Context context, int resource, List<myObject> data) {
super (context, resource, data);
this.context = context;
this.data = data;
}
@NonNull
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.myObjectName = convertView.findViewById(R.id.adapter_myObjectName);
viewHolder.myObjectDistance = convertView.findViewById(R.id.adapter_myObjectDistance);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (data != null) {
myObject obj = data.get(position);
if (obj.getLatitude() == "" || obj.getLongitude() == "" || obj.getLatitude() == null || obj.getLongitude() == null)
viewHolder.wellDistance.setText(R.string.no_available);
else
getLocation(viewHolder, well);
viewHolder.myObjectName.setText(obj.getName());
}
return convertView;
}
private void getLocation (final ViewHolder viewHolder, final myObject obj) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Be sure to allow location services", Toast.LENGTH_LONG).show();
}
else {
LocationServices.getFusedLocationProviderClient(context).requestLocationUpdates(LOCATION_REQUEST, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
currentLongitude = locationResult.getLastLocation().getLongitude();
currentLatitude = locationResult.getLastLocation().getLatitude();
float[] results = new float[1];
android.location.Location.distanceBetween(Double.parseDouble(obj.getLatitude()), Double.parseDouble(obj.getLongitude()), currentLatitude, currentLongitude, results);
DecimalFormat df = new DecimalFormat("###.##");
double miles = results[0] * 0.000621371192;
if (miles >= 1)
viewHolder.myObjectDistance.setText(String.valueOf(df.format(miles)) + " miles");
else
viewHolder.myObjectDistance.setText(String.valueOf(df.format(miles * 5280)) + " feet");
}
}, Looper.myLooper());
}
}
答案 0 :(得分:0)
最好不要将viewHolder传递给你的案例中的线程getLocation()。相反,你要做的是。你可能需要一个小调整,但这是个主意。
private void getLocation (int position) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Be sure to allow location services", Toast.LENGTH_LONG).show();
}
else {
LocationServices.getFusedLocationProviderClient(context).requestLocationUpdates(LOCATION_REQUEST, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
currentLongitude = locationResult.getLastLocation().getLongitude();
currentLatitude = locationResult.getLastLocation().getLatitude();
float[] results = new float[1];
android.location.Location.distanceBetween(Double.parseDouble(obj.getLatitude()), Double.parseDouble(obj.getLongitude()), currentLatitude, currentLongitude, results);
DecimalFormat df = new DecimalFormat("###.##");
double miles = results[0] * 0.000621371192;
myObject obj = data.get(position);
obj.setLatitude(currentLatitude);
obj.setLongitude(currentLongitude);
data.set(position,obj);
adapter.notifyDataSetChaged();
}
}, Looper.myLooper());
}
}
您的代码存在许多问题,例如从listview适配器请求许可。最好先问一下。