需要在lv1.setOnItemLongClickListener方法中加入一個長按獲取電話號碼,聯系人的代碼,我寫的代碼總感覺有錯誤,但是不知道錯在哪,我嘗試用system輸出我獲取的數值,數值是對的,但是傳不到打電話和發短信的地方
這是效果圖
package com.example.lianxi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;
import android.R.string;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.os.Build;
public class M1 extends Activity {
private ListView lv1;
private int[] pic = new int[] { R.drawable.img01, R.drawable.img02,
R.drawable.img03, R.drawable.img04, };
private String[] xm = new String[] { "張三", "李四", "王五", "趙六" };
private String[] dh = new String[] { "626746", "624882", "620874",
"130533", };
String s = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m1);
lv1 = (ListView) findViewById(R.id.lv1);
final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < pic.length; i++) {
Map<String, Object> m1 = new HashMap<String, Object>();
m1.put("pic", pic[i]);
m1.put("xm", xm[i]);
m1.put("dh", dh[i]);
list.add(m1);
}
SimpleAdapter sa = new SimpleAdapter(M1.this, list, R.layout.moban,
new String[] { "pic", "xm", "dh" }, new int[] { R.id.iv1,
R.id.tv1, R.id.tv2 });
lv1.setAdapter(sa);
lv1.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
HashMap<String, Object> map = (HashMap<String, Object>) lv1
.getItemAtPosition(arg2);
String xm = String.valueOf(map.get("xm").toString());
String dh = String.valueOf(map.get("dh").toString());
System.out.println(xm);
System.out.println(dh);
return false;
}
});
registerForContextMenu(lv1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
MenuInflater m1 = new MenuInflater(M1.this);
m1.inflate(R.menu.m1, menu);
menu.setHeaderIcon(R.drawable.i);
menu.setHeaderTitle("請選擇");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.dh:
Intent intent = new Intent();
intent.setAction(intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + dh));
startActivity(intent);
break;
case R.id.dx:
Intent intent1 = new Intent();
intent1.setAction(intent1.ACTION_SENDTO);
intent1.setData(Uri.parse("smsto:" + dh));
intent1.putExtra("sms_body", "");
startActivity(intent1);
break;
}
return super.onContextItemSelected(item);
}
}
因為你在setData時候用的dh引用的是一個String數組啊,不是你要的選取的電話號碼,你應該在長按事件裡面把那個dh改一下名然後定義成全局變量,然後再在setData裡面使用這個電話號就行了~