检查列表是不是包含多个值android
Posted
技术标签:
【中文标题】检查列表是不是包含多个值android【英文标题】:Check if list contains multiple values android检查列表是否包含多个值android 【发布时间】:2013-02-08 11:15:05 【问题描述】:我已经搜索到:How to check if a value already exists in the database in android。我遇到的问题是吐司从不显示,并且当输入的值与数据库中已有的值匹配时,数据仍然输入到数据库中。我看过有关使用 UNIQUE 确保数据不重复的帖子,但它不适用于我需要做的事情。我已经尝试过 if (entries.contains(checkDuplicates) == false) 和 if (!entries.equals(checkDuplicates)),也得到了相同的结果。我知道这是我想念或不理解的简单事情。请有人帮助我并为我指明正确的方向吗?
private void saveState()
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
eventDate = sdf.format(calendar.getTime());
String date = eventDate;
String event = eventTitle.getText().toString();
String empID = fiveAndTwo.getText().toString();
/**
* CRUD Operations
* */
// Inserting entries to DB
// Reading all contacts
Log.d("Reading: ", "Reading all entries..");
List<EventEntries> entries = db.getAllEntries();
for (EventEntries ee : entries)
String log = "Id: " + ee.getID() + " Date: " + ee.getDate() + " Event Name: " + ee.getEvent() + " Emp ID: " + ee.getempID();
// Writing entries to log
Log.d("Entry: ", log);
String checkDuplicates = date + event + empID;
if (!entries.contains(checkDuplicates))
Log.d("Insert: ", "Inserting ..");
db.addEntry(new EventEntries(date, event, empID));
String logEntry = "Date: " + date + " Event Name: " + event + " Emp ID: " + empID;
Log.d("Entered to DB:", logEntry);
else
Toast.makeText(this,"Employee has already been scanned for this event.", Toast.LENGTH_SHORT).show();
编辑:我让它工作了。这是我所做的:
private void saveState()
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
eventDate = sdf.format(calendar.getTime());
String date = eventDate;
String event = eventTitle.getText().toString();
String empID = fiveAndTwo.getText().toString();
/**
* CRUD Operations
* */
// Reading all entries
Log.d("Reading: ", "Reading all entries..");
List<EventEntries> entries = db.getAllEntries();
for (EventEntries ee : entries)
String log = "Id: " + ee.getID() + " Date: " + ee.getDate() + " Event Name: " + ee.getEvent() + " Emp ID: " + ee.getempID();
// Writing entries to log
Log.d("Entry: ", log);
EventEntries checkDuplicates = new EventEntries(date, event, empID);
if (!entries.contains(checkDuplicates))
Toast.makeText(this,"Successfully entered.", Toast.LENGTH_SHORT).show();
// Inserting entries to DB
Log.d("Insert: ", "Inserting ..");
db.addEntry(new EventEntries(date, event, empID));
String logEntry = "Date: " + date + " Event Name: " + event + " Emp ID: " + empID;
Log.d("Entered to DB:", logEntry);
else
empAlert();
这是我添加到 EventEntries 类的内容:
@Override
public int hashCode()
final int prime = 31;
int result = 1;
result = prime * result + ((_date == null) ? 0 : _date.hashCode());
result = prime * result + ((_empID == null) ? 0 : _empID.hashCode());
result = prime * result + ((_event == null) ? 0 : _event.hashCode());
return result;
@Override
public boolean equals(Object obj)
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EventEntries other = (EventEntries) obj;
if (_date == null)
if (other._date != null)
return false;
else if (!_date.equals(other._date))
return false;
if (_empID == null)
if (other._empID != null)
return false;
else if (!_empID.equals(other._empID))
return false;
if (_event == null)
if (other._event != null)
return false;
else if (!_event.equals(other._event))
return false;
return true;
【问题讨论】:
【参考方案1】:entries.contains(checkDuplicates)
总是返回 false,因为 entries
是 EventEntries
对象的列表,而 checkDuplicates
是 String
。您应该使用EventEntries
对象作为参数调用entries.contains
。
还有一件事:
// assuming entries is not empty
EventEntries first = entries.get(0);
entries.contains(first); // --> this is TRUE
// create a dupe of "first"
EventEntries firstDupe = new EventEntries(.....);
entries.contains(firstDupe); // --> in *general* this is FALSE
一般来说,第二种情况是假的。如果在EventEntries
类中覆盖equals
方法,那么first.equals(firstDupe)
将返回true,这可能是真的。只有这样,第二个例子才会返回 true。
这是一个示例实现:
public boolean equals(Object obj)
if (obj instanceof EventEntries)
EventEntries other = (EventEntries)obj;
if (date.equals(other.date) && event.equals(other.event)
&& empID == other.empID)
return true;
return false;
(根据需要进行调整。)
【讨论】:
我尝试了下面的评论但没有成功。我是否需要做一些不同的事情来获取 EventsEntries 对象? 感谢您提供的信息。在你的帮助下我得到了它,我发现 ***.com/questions/4404084/… 也对我有帮助。【参考方案2】:替换:
String checkDuplicates = date + event + empID;
与:
EventEntries checkDuplicates = new EventEntries(date, event, empID);
【讨论】:
我试过了,它仍然不能正常工作。它从不承认信息已经存在。更改为这会使日期、事件和 empID 成为 EventEntries 对象,对吗?请原谅我的无知,我对android、java和面向对象编程还是新手。 感谢您的帮助。我结合了你的答案和上面的答案来让它工作。以上是关于检查列表是不是包含多个值android的主要内容,如果未能解决你的问题,请参考以下文章