2016-04-25 77 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

相關問題