2016-04-25 72 views
0

enter image description here我创建Android应用程序填充这是由用户使用自定义适配器这样如何在特定的位置添加文本自定义适配器的Android

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 

进入GPS坐标列表如果用户选择复选框1,那么我必须找到从复选框1 lat long到复选框2,复选框3,复选框4 lat的距离。在这里我需要显示结果文本的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; 
} 
+0

在第一项研究如何使一个适配器类,然后一些事情做的代码。 –

+0

我没有太多的时间老兄用于读取doc.can你能帮我解决这个问题@Nigam Patro – lakshmansundeep

+0

里面'Locations_modle'添加变量的距离。每当用户检查复选框更新列表中的变量。 –

回答

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

获取空值man没有结果 – lakshmansundeep

+0

意味着什么,你会得到空值? –

+0

修改'objects.get(ⅰ).setDistance(距离);'这部分代码内部for循环到此'objects.get(ⅰ).setDistance(将String.valueOf(距离));' –

0

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

对我来说,你的这个代码是有点难以阅读,但你在哪里使用您的ViewGroup家长和查看convertView?从

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

对于我快速浏览你只让你(TextView) locations_row.findViewById(R.id.txt_distance);的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 所以,从搜索约ViewHolder模式的列表视图适配器开始。

+0

您可以详细解释代码吗 – lakshmansundeep

+0

您可以通过搜索粗体文本找到更多有用的示例代码。 – JamesNickel

+0

好吧兄弟我会做,但我现在没有足够的时间@ JameNickel – lakshmansundeep

相关问题