【问题标题】:How to add the text at the particular position in custom adapter in android如何在android的自定义适配器中的特定位置添加文本
【发布时间】:2016-04-25 16:22:58
【问题描述】:

我正在创建 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 字段中的结果文本他们各自的位置,但在这里我只在最后一个位置得到结果,谁能告诉我如何实现它仅供参考:[![在此处输入图像描述][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;
}

【问题讨论】:

  • 先学习一些关于如何制作一个适配器类然后做代码的东西。
  • 兄弟,我没有太多时间阅读该文档。你能帮我解决这个问题吗@Nigam Patro
  • Locations_modle 内添加距离变量。并且每当用户选中复选框时,都会更新列表中的该变量。
  • 是的,我做到了,但我得到了空白值@Nigam Patro
  • 我更新了您代码的getView() 方法。请仔细阅读答案。

标签: android listview maps adapter


【解决方案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;
}

【讨论】:

  • 什么意思,你得到的是空值?
  • 修改objects.get(i).setDistance(distance);这部分代码里面for循环到这个objects.get(i).setDistance(String.valueOf(distance));
  • 对不起,我会检查一下
  • 如果我写 notifyOndatasetchanged 它会阻止我检查复选框
  • 我在问题中编辑了我的代码和屏幕截图看看这个
【解决方案2】:

您夸大了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 模式开始。

【讨论】:

  • 通过谷歌搜索粗体文本,您会发现更多有用的示例代码。
  • 好的兄弟,我稍后再做,但我现在没有足够的时间@JameNickel
【解决方案3】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2022-11-10
    • 2015-03-31
    • 2017-07-16
    相关资源
    最近更新 更多