如何在Android的自定义适配器中的特定位置添加文本 [英] How to add the text at the particular position in custom adapter in android

查看:112
本文介绍了如何在Android的自定义适配器中的特定位置添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android应用,该应用填充用户使用自定义适配器输入的GPS坐标列表

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

如果用户选中复选框1,则我必须查找距check-框1 lat long到复选框2,复选框3,box-4 lat long。在这里,我需要在 Text 他们的字段中显示结果文本各自的位置,但在这里我只能在最后一个位置得到结果,谁能告诉我如何实现前夕仅供参考:[![在此处输入图片描述] [2]] [2]
此sc将为您详细解释。
如果我检查一个值,它仅在最后一个值处更新结果,但是我需要更新并显示整个数据的结果
这是我的代码

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;
}


推荐答案

修改您的 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;
}

这篇关于如何在Android的自定义适配器中的特定位置添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆