我正在让用户选择多个联系人,我将其添加到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();
}
}
}
答案 0 :(得分:1)
for(int i = 0;i > names.size(); i++){
我觉得这个循环永远不会被执行,因为我很确定一个集合不能有负面大小...
您的意思是使用<
吗?
此外,您不应该在numbers.toArray()
循环的每次迭代中调用names.toArray()
和for
。对于较大的集合,这可能效率低下。