android 如何在自定义适配器的特定位置添加文本
How to add the text at the particular position in custom adapter in android
我正在创建 android 应用程序来填充 GPS co-ordinates 列表,用户使用这样的自定义适配器输入
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
如果用户选择 check-box 1 那么我必须找到从 check-box 1 lat long 到 check-box 2,check-box 3,check-box-4 经纬度。这里我需要在 Text
各自位置的字段中显示结果文本,但在这里我只在最后一个位置得到结果,谁能告诉我如何实现它,仅供参考:[! [在此处输入图片描述][2]][2]
本sc将为您详细解释。
如果我检查一个值,它只更新最后一个值的结果,但我需要更新并显示整个数据的结果
这是我的代码
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;
}
检查您的 xml 布局,确保您的行 ID 是唯一的并且您正在正确使用 distance_text Textview 对象。也许需要在 for 循环中刷新对象?
对我来说,你的这段代码有点难读,但是你在哪里使用你的 ViewGroup parent 和 View convertView?来自
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
对于我的快速概览,您只获得 (TextView) locations_row.findViewById(R.id.txt_distance);
的 1 个实例,这是您视图中的最后一个位置。确保它根据需要刷新。
像这样修改你的getView()
方法
@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;
}
您夸大了 locations_row
的普遍看法。这将每次创建一个新项目。您需要重用现有项目。我的意思是:
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();
}
您甚至没有使用任何 View convertView
或 final ViewGroup parent
因此,从搜索列表视图适配器的 ViewHolder 模式开始。
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
如果用户选择 check-box 1 那么我必须找到从 check-box 1 lat long 到 check-box 2,check-box 3,check-box-4 经纬度。这里我需要在 Text
各自位置的字段中显示结果文本,但在这里我只在最后一个位置得到结果,谁能告诉我如何实现它,仅供参考:[! [在此处输入图片描述][2]][2]
本sc将为您详细解释。
如果我检查一个值,它只更新最后一个值的结果,但我需要更新并显示整个数据的结果
这是我的代码
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;
}
检查您的 xml 布局,确保您的行 ID 是唯一的并且您正在正确使用 distance_text Textview 对象。也许需要在 for 循环中刷新对象?
对我来说,你的这段代码有点难读,但是你在哪里使用你的 ViewGroup parent 和 View convertView?来自
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
对于我的快速概览,您只获得 (TextView) locations_row.findViewById(R.id.txt_distance);
的 1 个实例,这是您视图中的最后一个位置。确保它根据需要刷新。
像这样修改你的getView()
方法
@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;
}
您夸大了 locations_row
的普遍看法。这将每次创建一个新项目。您需要重用现有项目。我的意思是:
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();
}
您甚至没有使用任何 View convertView
或 final ViewGroup parent
因此,从搜索列表视图适配器的 ViewHolder 模式开始。