我最近开始使用 Android 和 Java 编程,所以请多多包涵。
我写了一个循环,在将新的姓名和电话号码添加到列表和隐藏数组之前,应该删除之前找到的所有重复项。使用当前方法,我仍然会不断重复,当再次单击按钮添加所有相同的联系人时,我会再次获得所有联系人。这让我觉得重复检查方法根本无法正常工作,但我没有得到任何帮助的错误
我有两个在外部创建的列表数组:
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
这是添加联系人的方法:
public void AddAllContacts(View view) {
try {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String linesp = System.getProperty("line.separator");
TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
duplicatecheck(name, phoneNumber);
addthistothelist(name, phoneNumber);
}
phones.close();
}
catch (Exception e){
e.printStackTrace();
}
}
这是重复检查方法:
public void duplicatecheck(String name,String phoneNumber) {
for (int i=0;i<phnnumbers.size();i++) {
String thenumber = phnnumbers.get(i);
String thename= names.get(i);
if(thenumber.equals(phoneNumber)) {
phnnumbers.remove(i);
names.remove(i);
TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
String textpost = quantityTextView.getText().toString();
String newtextpost = textpost.replaceAll(thenumber, "UNBELIEVABLEEE");
String secondtextpost = newtextpost.replaceAll(thename, "UNBELIEVABLE");
quantityTextView.setText(secondtextpost);
NumberOfContactsAdded--;
}
}
}
这是在检查重复项并删除后调用的方法,下一个方法是下一个添加数字和名称的方法:
public void addthistothelist(String nameofperson,String NumberOfPerson) {
String linesp = System.getProperty("line.separator");
TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
String textpost = quantityTextView.getText().toString();
NumberOfPerson = NumberOfPerson.replaceAll("[^0-9]", "");
if(NumberOfPerson.contains("+1")) {
phnnumbers.add(NumberOfPerson);
names.add(nameofperson);
NumberOfContactsAdded++;
quantityTextView.append(linesp+nameofperson+" " +NumberOfPerson);
} else {
NumberOfPerson= "+1"+NumberOfPerson;
phnnumbers.add(NumberOfPerson);
names.add(nameofperson);
NumberOfContactsAdded++;
quantityTextView.append(linesp+nameofperson+" " +NumberOfPerson);
}
}
我真的不知道我可能做错了什么。我会尝试清理这段代码,但它甚至无法正常工作,我无法清理它。
最佳答案
简单地说,你可以这样做:
为person创建一个bean类
公共(public)类人物{ 私有(private)字符串名称; 私有(private)字符串电话;
public Person(String name, String phone) {
this.name = name;
phone = phone.replaceAll("\\W+", "");
phone = "+1"+phone;
this.phone = phone;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Person person = (Person) o;
return name != null ? name.equals(person.name) : person.name == null && (phone != null ? phone.equals(person.phone) : person.phone == null);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (phone != null ? phone.hashCode() : 0);
return result;
}
然后遍历所有的联系人,将他们放在一个集合中,它会自动避免重复
Set<Person> persons = new HashSet<>();
public void AddAllContacts(View view) {
try {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String linesp = System.getProperty("line.separator");
TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Person person = new Person(name, phoneNumber);
persons.add(person);
}
phones.close();
//在这里你可以用 Person's Set 做什么 } 捕获(异常e){ e.printStackTrace(); }
关于java - 循环不捕获重复项并在 Android(Java) 中删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301880/
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?