我希望能够检查一个对象是否存在,根据其_sensor_id
内ArrayList
它显示在ListView
。Android 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;
}
}
这似乎是它可能是最好的方式来做到这一点,但我不完全知道如何构造它,请你给一个更详细的例子吗? –
编辑我的答案更多细节。如果你想了解更多的细节,请分享一些更多的代码,以便我更好地理解逻辑。 – milindmb
感谢您的帮助milindmb,我添加了你所做的,但我的listview不再显示。我添加了该列表视图的其他特定代码。 –