2017-08-24 49 views
1

我希望能夠檢查一個對象是否存在,根據其_sensor_idArrayList它顯示在ListViewAndroid Arraylist迭代設置或添加基於它是否存在

如果存在,我想設置它,如果不存在,我想添加它。每次此方法運行時,HashMap數據都會不同,我希望它根據_sensor_id來檢查它是否存在於ListView(ArrayList)中或不存在。

if (mEquipmentList.isEmpty()) { 
// Populate the Equipment List 
HashMap < String, String > equip = new HashMap < String, String >(); 
equip.put(FIRST_COLUMN, _sensor_id); 
equip.put(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
equip.put(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
equip.put(FIFTH_COLUMN, Double.toString(distance) + "m"); 
equip.put(SECOND_COLUMN, Double.toString(mEquipmentList.indexOf(equip))); 
mEquipmentList.add(equip); 

for (HashMap < String, String > entry: mEquipmentList) { 
    for (String key: entry.keySet()) { 
    String value = entry.get(key); 
    //Debugging 
    Log.d("equipment contains= ", "sensorid: " + mEquipmentList.get(0)); 
    Log.d("equipment key = ", "key: " + key); 
    Log.d("equipment value = ", "value: " + value); 
    Log.d("equipment index", "value:" + (mEquipmentList.indexOf(equip))); 
    } 
} 
} else { 

// Populate the Equipment List 
HashMap < String, String > equip = new HashMap < String, String >(); 
equip.put(FIRST_COLUMN, _sensor_id); 
equip.put(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
equip.put(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
equip.put(FIFTH_COLUMN, Double.toString(distance) + "m"); 
equip.put(SECOND_COLUMN, Double.toString(mEquipmentList.size())); 
mEquipmentList.set(0, equip); 

for (HashMap < String, String > entry: mEquipmentList) { 
    for (String key: entry.keySet()) { 
    String value = entry.get(key); 
    //Debugging 
    Log.d("equipment contains= ", "sensorid: " + mEquipmentList); 
    Log.d("equipment contains= ", "sensorid: " + mEquipmentList.contains(key)); 
    Log.d("equipment key = ", "key: " + key); 
    Log.d("equipment value = ", "value: " + value); 
    Log.d("equipment index", "value:" + mEquipmentList.indexOf(equip)); 
    } 
} 
} 

// Set the adapter 
ListViewAdapter adapter = new ListViewAdapter(this, mEquipmentList); 
listView.setAdapter(adapter); 
adapter.notifyDataSetChanged(); 

} 

編輯*********

public void updateEquipList(String message) { 

    if (!pathHistory.isEmpty()) { 
     double currentLat = pathHistory.getFirst().lat; 
     double currentLng = pathHistory.getFirst().lng; 
     int currentOrientation = getResources().getConfiguration().orientation; 

     ListView listView = (ListView) findViewById(R.id.equip_list); 

     HashMap<String, Equipment> equipmentMap = new HashMap<String, Equipment>(); 

     try { 
      List<String> data_list = Arrays.asList(message.split(",")); 

      // point added (checks in correct format) 
      String _sensor_id = data_list.get(0); 
      if (_sensor_id.substring(0, Math.min(_sensor_id.length(), 2)).equals("DT")) { 

       double _lat = Double.parseDouble(data_list.get(3)); 
       double _lng = Double.parseDouble(data_list.get(4)); 
       double _heading = Double.parseDouble(data_list.get(5)); 
       double _speed = Double.parseDouble(data_list.get(6)); 
       int distance = (int) Trajectory.ChordLength(currentLat, currentLng, _lat, _lng); 
       boolean doesSensorExist = false; 

       // for input _sensor_id, check if there exists an Equipment object in map. 
       Equipment equip = equipmentMap.get(_sensor_id); 
       if(equip == null) // no object for this sensor_id 
       { 
        equip = new Equipment(); 
        // populate equipment attributes in the object 
        equip.addParameter(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
        equip.addParameter(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
        equip.addParameter(FIFTH_COLUMN, Double.toString(distance) + "m"); 
        equip.addParameter(SECOND_COLUMN, Double.toString(mEquipmentList.indexOf(equip))); 

        // add this Equipment Object in the map. 
        equipmentMap.put(_sensor_id, equip); 
        /// since its a new object, add same to the mEquipmentList which is a unique array of Equipment Object 
        mEquipmentList.add(equip); 
       } 
       else // Equipment object exits for input _sensor_id string 
       { 
        //Update the values for eq object if they are changed 
        equip.addParameter(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
        equip.addParameter(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
        equip.addParameter(FIFTH_COLUMN, Double.toString(distance) + "m"); 
        equip.addParameter(SECOND_COLUMN, Double.toString(mEquipmentList.indexOf(equip))); 
        // here no need to add the Equipment Object in the ArrayList again. 
       } 

       // Set the adapter 
       ListViewAdapter adapter = new ListViewAdapter(this, mEquipmentList); 
       listView.setAdapter(adapter); 

       } 
      } catch(Exception e){ 
       // Marker construction failed 
       Log.d(TAG, "Updating list failed: " + e.toString()); 
      } 
     } 

    } 

class Equipment extends HashMap<String, String> { 
    HashMap<String, String> parameters; 

    void addParameter(String key, String value) 
    { 
     parameters.put(key, value); 
    } 
} 

列表視圖ADAPTER

公共類ListViewAdapter延伸BaseAdapter {

public ArrayList<com.bhp.prox_aware.MapsActivity.Equipment> list; 
Activity MapsActivity; 
TextView txtFirst; 
TextView txtSecond; 
TextView txtThird; 
TextView txtFourth; 
TextView txtFifth; 

public ListViewAdapter(Activity activity, ArrayList<com.bhp.prox_aware.MapsActivity.Equipment> list){ 
    super(); 
    this.MapsActivity=activity; 
    this.list=list; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return list.size(); 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return list.get(position); 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 



@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    LayoutInflater inflater=MapsActivity.getLayoutInflater(); 

    if(convertView == null){ 

     convertView=inflater.inflate(R.layout.equip_row, null); 


     /* TODO add an ImageView here for an icon to display next to the list */ 
     txtFirst=(TextView) convertView.findViewById(R.id.name); 
     txtSecond=(TextView) convertView.findViewById(R.id.id); 
     txtThird=(TextView) convertView.findViewById(R.id.speed); 
     txtFourth=(TextView) convertView.findViewById(R.id.direction); 
     txtFifth=(TextView) convertView.findViewById(R.id.distance); 
    } 

    int color = Color.argb(200, 34, 34, 34); 


    HashMap<String, String> map=list.get(position); 
    txtFirst.setText(map.get(FIRST_COLUMN)); 
    txtSecond.setText(map.get(SECOND_COLUMN)); 
    txtThird.setText(map.get(THIRD_COLUMN)); 
    txtFourth.setText(map.get(FOURTH_COLUMN)); 
    txtFifth.setText(map.get(FIFTH_COLUMN)); 

    //Check if within 100m if yes change colour of background to red 
    double distanceCheck = Double.parseDouble(map.get(FIFTH_COLUMN).replace("m", "")); 
    if(distanceCheck <= 100){ 

     color = Color.argb(200, 163, 0, 0); 
    }else { 
     color = Color.argb(200, 34, 34, 34); 
    } 

    txtFirst.setTextColor(Color.WHITE); 
    txtFirst.setBackgroundColor(color); 

    txtSecond.setTextColor(Color.WHITE); 
    txtSecond.setBackgroundColor(color); 

    txtThird.setTextColor(Color.WHITE); 
    txtThird.setBackgroundColor(color); 

    txtFourth.setTextColor(Color.WHITE); 
    txtFourth.setBackgroundColor(color); 

    txtFifth.setTextColor(Color.WHITE); 
    txtFifth.setBackgroundColor(color); 

    return convertView; 
} 

}

回答

0

你應該能夠模擬你的代碼結構略有不同 如果可能創建一個類說用的設備屬性存儲列參數,如速度,溫度等作爲地圖字符串,Stringthat類

class Equipment 
{ 
    HashMap<String, String> parameters; 

    void addParameter(String key, String value) 
    { 
    parameters.put(key, value); 
    } 
} 

在你的邏輯,sensor_id的關鍵是保持的唯一性設備對象。因此,創建地圖字符串的裝備

HashMap<String, Equipment> equipmentMap = new HashMap<String, Equipment>(); 
// for input _sensor_id, check if there exists an Equipment object in map. 
Equipment equip = sensorMap.get(_sensor_id); 
if(equip == null) // no object for this sensor_id 
{ 
    equip = new Equipment(); 
    // populate equipment attributes in the object 
    equip.addParameter(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
    equip.addParameter(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
    equip.addParameter(FIFTH_COLUMN, Double.toString(distance) + "m"); 
    equip.addParameter(SECOND_COLUMN, Double.toString(mEquipmentList.indexOf(equip))); 

    // add this Equipment Object in the map. 
    sensorMap.put(_sensor_id, equip); 
/// since its a new object, add same to the mEquipmentList which is a unique array of Equipment Object 
    mEquipmentList.add(eq); 
} 
else // Equipment object exits for input _sensor_id string 
{ 
    //Update the values for eq object if they are changed 
    equip.addParameter(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
    equip.addParameter(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
    equip.addParameter(FIFTH_COLUMN, Double.toString(distance) + "m"); 
    equip.addParameter(SECOND_COLUMN, Double.toString(mEquipmentList.indexOf(equip))); 

    // here no need to add the Equipment Object in the ArrayList again. 
} 
// notify adapter for data change 
adapter.notifyDataSetChanged(); 

HashMap中有助於保持基於_sensor_id數據的唯一性。

希望這有助於..

+0

這似乎是它可能是最好的方式來做到這一點,但我不完全知道如何構造它,請你給一個更詳細的例子嗎? –

+0

編輯我的答案更多細節。如果你想了解更多的細節,請分享一些更多的代碼,以便我更好地理解邏輯。 – milindmb

+0

感謝您的幫助milindmb,我添加了你所做的,但我的listview不再顯示。我添加了該列表視圖的其他特定代碼。 –

0

我設法得到它的工作執行以下操作,雖然我相當肯定有這樣做的更好的方法。

if (currentOrientation == 2 && mSettings.get(0)) { 
        if (mEquipmentList.isEmpty()) { 
         // Populate the Equipment List 
         HashMap<String, String> map = new HashMap<String, String>(); 
         map.put(FIRST_COLUMN, _sensor_id); 
         map.put(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
         map.put(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
         map.put(FIFTH_COLUMN, Double.toString(distance) + "m"); 
         mEquipmentList.add(map); 
        } else { 

         for (HashMap<String, String> test : mEquipmentList) { 
          if (test.get(FIRST_COLUMN).equals(_sensor_id)) { 
           HashMap<String, String> map = new HashMap<String, String>(); 
           map.put(FIRST_COLUMN, _sensor_id); 
           map.put(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
           map.put(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
           map.put(FIFTH_COLUMN, Double.toString(distance) + "m"); 
           mEquipmentList.set(mEquipmentList.indexOf(test), map); 
           doesSensorExist = true; 
          } 
         } 
         if (!doesSensorExist && mEquipmentList.size() < 5) { 
          HashMap<String, String> map = new HashMap<String, String>(); 
          map.put(FIRST_COLUMN, _sensor_id); 
          map.put(THIRD_COLUMN, Double.toString(_speed) + "km/h"); 
          map.put(FOURTH_COLUMN, Double.toString(_heading) + "°"); 
          map.put(FIFTH_COLUMN, Double.toString(distance) + "m"); 
          mEquipmentList.add(map); 
         } 
        } 
          // Set the adapter 
          ListViewAdapter adapter = new ListViewAdapter(this, mEquipmentList); 
          listView.setAdapter(adapter); 
          adapter.notifyDataSetChanged(); 
        } 
       } 
相關問題