gpt4 book ai didi

java - 如何从联系人中获取电话号码?

转载 作者:行者123 更新时间:2023-12-02 06:54:08 26 4
gpt4 key购买 nike

我有两个 String[],一个包含姓名,另一个包含电话号码。如果我从列表中选择一个名字,我如何从对话框中调用该人的电话号码。例如,first string[] 中的第一个人的电话号码为 second[]。如何获得该号码?

package com.mkyong.android;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListFruitActivity extends ListActivity {

static final String[] Imena = new String[] { "Aleksandar Panic", "Dubravka Protic", "Milutin Panic",
"Jelica Panic", "Nemanja Gagic", "Doris Dragojevic", "Milica Protic", "Kiki Beba",
"Dule Savic", "Miroslav Miskovic", "Petar Djuric", "Dragoslav Joksimovic", "Petar Petrovic" };
static final String[] Brojevi=new String[] { "0638638045", "062450050", "065045324", "05443",
"06535475", "564218", "546567", "514574","5454333","444787","413133","354867","24879"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,
Imena));

ListView listView = getListView();
listView.setTextFilterEnabled(true);

listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {


AlertDialog.Builder adb=new AlertDialog.Builder(ListFruitActivity.this);
adb.setTitle("Odabir");
adb.setMessage("Izabrali ste profesora "+parent.getItemAtPosition(position));


adb.setPositiveButton("Call",new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog, int which) {
Intent i=new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:+651234567")); //here is the problem, how to get the right number
startActivity(i);
}
});
adb.show();
}
});
}
}

最佳答案

为什么你需要两个String[]数组?

您有两种选择可以更好地实现它:

  1. 使用 Map<String, String>将姓名存储为键,将电话号码存储为值。
  2. 创建一个类 Contact存储您的所有联系方式,如姓名、地址、电话号码、电子邮件等,然后使用 List<Contact>或一个数组(推荐的方法)。

关于java - 如何从联系人中获取电话号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17620293/

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