代码之家  ›  专栏  ›  技术社区  ›  lakshmansundeep

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

  •  0
  • lakshmansundeep  · 技术社区  · 10 年前

    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,复选框-4 lat-long Text 他们各自的位置,但在这里我只能在最后一个位置得到结果,任何人都可以告诉我如何实现它,仅供参考:[![在此处输入图像描述][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 回复  |  直到 10 年前
        1
  •  1
  •   Nigam Patro    10 年前

    修改您的 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;
    }
    
        2
  •  1
  •   JamesNickel    10 年前

    你充气了 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 所以,从搜索开始 listview适配器的ViewHolder模式 .

        3
  •  0
  •   Leolian    10 年前

    检查您的xml布局,确保您的行id是唯一的,并且您使用的是正确的distance_textTextview对象。也许需要刷新for循环中的对象?

    对我来说,你的代码有点难读,但你在哪里使用ViewGroup父级和ViewconvertView?从…起

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

    对于我的快速概述,您只获得了 (TextView) locations_row.findViewById(R.id.txt_distance); 这是你观点中的最后一个观点。确保它按您的要求刷新。