如何在android中的自定义适配器中的特定位置添加文本

时间:2016-04-25 07:22:52

标签: android listview maps adapter

enter image description here我正在创建一个Android应用程序,用于填充用户使用自定义适配器输入的GPS坐标列表

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

如果用户选中复选框1,那么我必须找到从复选框1 lat long到复选框2,复选框3,check-box-4 lat long的距离。这里我需要显示结果文本在Text他们各自的位置,但在这里我只得到最后一个位置的结果,任何人都可以告诉我如何实现它FYI:[![在此输入图像描述] [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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

你夸大了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 convertViewfinal ViewGroup parent中的任何一个 因此,从搜索适用于listview适配器的ViewHolder模式开始。

答案 2 :(得分:0)

检查您的xml布局,您的行ID是唯一的,并且您正在使用distance_text Textview对象。也许需要在for循环中刷新对象?

对我来说,你的这段代码有点难读,但是你在哪里使用ViewGroup父和View convertView?从

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {

对于我的快速概述,您只获得(TextView) locations_row.findViewById(R.id.txt_distance);的一个实例,这是您视图中的最后一个位置。确保它可以根据需要刷新。