gpt4 book ai didi

java - 获取联系方式失败?

转载 作者:行者123 更新时间:2023-12-01 12:58:23 25 4
gpt4 key购买 nike

我让用户选择多个联系人,将其电话号码/姓名添加到 LinkedHashSet 中以避免重复。我的 for 循环(循环访问链接的哈希集没有被执行。我相信联系信息收集存在问题,尽管该部分被执行了)

private static final int Req_Code = 1;

private LinkedHashSet<String> names = new LinkedHashSet<String>();
private LinkedHashSet<String> numbers = new LinkedHashSet<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fetchContacts();

final Button sendBtn = (Button) findViewById(R.id.send);
final EditText text = (EditText) findViewById(R.id.text);

sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String t = text.getText().toString();
if(!t.equalsIgnoreCase("") && t != null){
for(int i = 0;i > names.size(); i++){//After debud messages , the only part of the program not getting excecuted is this loop.

Log.i("shit", (String) numbers.toArray()[i] + " " + (String) names.toArray()[i]);
}
text.setText("");
numbers = new LinkedHashSet<String>();
names = new LinkedHashSet<String>();
}
}

});

}

private void fetchContacts() {
Log.i("shit", "fetching");
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);

startActivityForResult(contactPickerIntent, Req_Code);

}

@SuppressWarnings("deprecation")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == Req_Code) {

if(resultCode == RESULT_OK){

Uri uri = data.getData();

Cursor cursor = getContentResolver().query(uri, null, null, null, null);

cursor.moveToFirst();
names.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

cursor.moveToFirst();
numbers.add(cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));

fetchContacts();

}else if (resultCode == RESULT_CANCELED) {
if(names.size() == 0 || numbers.size() == 0){
fetchContacts();
}
}
}

最佳答案

for(int i = 0;i > names.size(); i++){

在我看来,这个循环永远不应该被执行,因为我很确定集合不能有负大小......

您是否想使用 <相反?

此外,您不应该调用 numbers.toArray()names.toArray() for 的每次迭代环形。对于较大的集合,这可能会变得低效。

关于java - 获取联系方式失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23712783/

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