问题
I am creating android app that populates the list of GPS co-ordinates which was entered by the user using custom adapter like this
SELECTION LAT LONG DISTANCE
-------------------------------------------------
checkbox1 123.4546 456.48751 Text
checkbox2 123.4546 456.48751 Text
checkbox3 123.4546 456.48751 Text
checkbox4 123.4546 456.48751 Text
If user selects the check-box 1 then i have to find the distance from check-box 1 lat long to check-box 2,check-box 3,check-box-4 lat long .Here i need to display the result text in the field of Text
their respective position but here i am getting the result only at the last position can anyone tell me how to achieve it FYI:[![enter image description here][2]][2]
This sc will explain you in detail.
If i check the one value it was updating the result only at the last value but i need to update and display the result for the whole data
This is my code
check_locations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
latitude_string = location.getLatitude();
longitude_string = location.getLongitude();
baseLat_double = Double.parseDouble(latitude_string);
baseLong_double = Double.parseDouble(longitude_string);
location_a = new Location("Base position");
location_a.setLatitude(Double.parseDouble(latitude_string));
location_a.setLongitude(Double.parseDouble(longitude_string));
location_b = new Location("End position");
for (int i = 0; i < objects.size(); i++) {
finalLat_double = Double.parseDouble(objects.get(i).getLatitude());
finalLong_double = Double.parseDouble(objects.get(i).getLongitude());
location_b.setLatitude(finalLat_double);
location_b.setLongitude(finalLong_double);
distance = location_a.distanceTo(location_b);
distance = distance * 1.609344;
objects.get(i).setDistance(String.valueOf(distance));
}
notifyDataSetChanged();
distance_text.setText(location.getDistance());
}
}
});
return locations_row;
}
回答1:
Modify your getView()
methods something like this
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final View locations_row = LayoutInflater.from(context).inflate(R.layout.layout_adapter_list_details, null);
final Locations_modle location = (Locations_modle) objects.get(position);
TextView text_cust_name = (TextView) locations_row.findViewById(R.id.txt_cust_name_heading);
TextView latitude = (TextView) locations_row.findViewById(R.id.txt_latitude);
latitude.setText(location.getLatitude());
TextView longitude = (TextView) locations_row.findViewById(R.id.txt_longitude);
TextView distance_text = (TextView) locations_row.findViewById(R.id.txt_distance);
if (StringUtils.isEmpty(location.getDistance()))
distance_text.setText("DISTANCE");
longitude.setText(location.getLongitude());
text_cust_name.setText(location.getLocationName());
CheckBox check_locations = (CheckBox) locations_row.findViewById(R.id.check_locations);
check_locations.setTag(position);
if (position == selectedPostion) {
check_locations.setChecked(true);
} else {
check_locations.setChecked(false);
}
check_locations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedPostion = (int) buttonView.getTag();
latitude_string = objects.get(selectedPostion).getLatitude();
longitude_string = objects.get(selectedPostion).getLongitude();
baseLat_double = Double.parseDouble(latitude_string);
baseLong_double = Double.parseDouble(longitude_string);
location_a = new Location("Base position");
location_a.setLatitude(Double.parseDouble(latitude_string));
location_a.setLongitude(Double.parseDouble(longitude_string));
location_b = new Location("End position");
for (int i = 0; i < objects.size(); i++) {
finalLat_double = Double.parseDouble(objects.get(i).getLatitude());
finalLong_double = Double.parseDouble(objects.get(i).getLongitude());
location_b.setLatitude(finalLat_double);
location_b.setLongitude(finalLong_double);
distance = location_a.distanceTo(location_b);
distance = distance * 1.609344;
objects.get(i).setDistance(distance);
}
Locations_Adapter.this.notifyDataSetChanged();
}
}
});
return locations_row;
}
回答2:
You inflated locations_row
as a general view. This will create a new item every time. You need to reuse the existing items. I mean:
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_adapter_list_details, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
You didn't even use any of the View convertView
or final ViewGroup parent
So, start from searching about ViewHolder pattern for listview adapter.
回答3:
Check your xml layout that your row id is unique and you are using correctly distance_text Textview object. Maybe need to refresh the object in the for loop?
For me this code of yours is little hard to read but where do you use your ViewGroup parent and View convertView? From
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
For my quick overview you are only getting 1 instance of your (TextView) locations_row.findViewById(R.id.txt_distance);
and that is the last position on your view. Make sure it get's refreshed as you want.
来源:https://stackoverflow.com/questions/36834279/how-to-add-the-text-at-the-particular-position-in-custom-adapter-in-android