经过对互联网的大量研究后,我找不到如何在列表视图中启用电话的答案。 这是我的示例代码: 假设我有50家医院的名单,并附有电话联系方式
public class Medical extends ListActivity {
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bus_main);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.rowview,
new String[] {"title","address","phone"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
private void populateList() {
HashMap<String,String>
map = new HashMap<String,String>();
map.put("title","UHS Hospital");
map.put("address", "Street 54");
map.put("phone", "4077000");
list.add(map);
map = new HashMap<String,String>();
map.put("title","Calvary Hospital");
map.put("address", "Street 43");
map.put("phone", "2362491");
list.add(map);
}
}
如何根据医院的个人电话号码明细(text3)启用所有列表视图条目的电话。
答案 0 :(得分:1)
在您的活动中添加此方法:
public class Medical extends ListActivity {
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.rowview,
new String[] {"title","address","phone"},
new int[] {R.id.textView1,R.id.textView2, R.id.textView3}
);
populateList();
setListAdapter(adapter);
}
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l,v,position,id);
if(position>=0 && position<list.size()) {
HashMap<String, String> tmp = list.get(position);
if(tmp.containsKey("phone")) {
String tel = tmp.get("phone");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+tel));
startActivity(callIntent);
}
}
}
private void populateList() {
HashMap<String,String>
map = new HashMap<String,String>();
map.put("title","UHS Hospital");
map.put("address", "Street 54");
map.put("phone", "4077000");
list.add(map);
map = new HashMap<String,String>();
map.put("title","Calvary Hospital");
map.put("address", "Street 43");
map.put("phone", "2362491");
list.add(map);
}
}
并添加
<uses-permission android:name="android.permission.CALL_PHONE" />
你的清单中的
注意此代码不会检查该号码是否有效。