2017-08-24 11 views
1

ListViewに表示されるArrayList内のオブジェクトが_sensor_idに基づいて存在するかどうかを確認できます。Androidアライリート反復は、存在するかどうかに基づいて設定または追加します

もし私がそれを設定したいのであれば、それを設定したいのですが、それを追加したいのですが。このメソッドが実行されるたびにHashMapのデータは異なります。ListView(ArrayList)に存在するかどうかを確認するために、_sensor_idに基づいてチェックします。

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(); 

} 

EDIT *********

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は{

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

あなたBaseAdapterを拡張しますコードをモデル化できるはずです構造少し違っ 可能あなたのロジックで

class Equipment 
{ 
    HashMap<String, String> parameters; 

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

Stringthatクラス、文字列の地図としてなどの速度、温度などの列パラメータを格納する属性を持つ機器を言うクラスを作成する場合は、sensor_idはの独自性を維持する鍵となりますEquipmentオブジェクト。だから、HashMapのは_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(); 

文字列のマップを作成し、機器。

・ホープ、このことができます。..

+0

これは最善の方法だと思われますが、どのように構造化すればよいかわからないので、より詳細な例を教えてください。 –

+0

私の答えを詳細に編集しました。より詳細な情報が必要な場合は、ロジックをよりよく理解できるようにコードをいくつか共有してください。 – milindmb

+0

ヘルプmilindmbをありがとう、私はあなたのやり方を追加しましたが、私のリストビューはもう表示されません。そのリストビューに固有のコードの残りの部分を追加しました。 –

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(); 
        } 
       } 
関連する問題