gpt4 book ai didi

java - Android Arraylist迭代根据是否存在设置或添加

转载 作者:太空宇宙 更新时间:2023-11-04 11:12:26 24 4
gpt4 key购买 nike

我希望能够根据其 _sensor_id 检查对象是否存在于 ArrayList 中,该对象显示在 ListView 中。

如果存在,我想设置它,如果不存在,我想添加它。每次此方法运行时,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);
}
}

ListView 适配器

公共(public)类 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;
}

}

最佳答案

您应该能够以稍微不同的方式对代码结构进行建模如果可能的话,创建一个类,例如设备,其属性用于将列参数(如速度、温度等)存储为字符串映射,字符串该类

class Equipment
{
HashMap<String, String> parameters;

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

在您的逻辑中,sensor_id 是保持 Equipment 对象唯一性的关键。因此创建一个字符串和设备的映射

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 保持数据的唯一性。

希望这有帮助..

关于java - Android Arraylist迭代根据是否存在设置或添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45853087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com