您好,在我的项目中,我必须从手机上取一个联系人电话号码和姓名。我可以拨打电话号码和姓名,但是当我将其添加到ArrayList<HashMap<String, String>>
时我只能获得最后的价值我不知道为什么。但当我记录它时,我得到了所有的价值,你们可以帮助我哪里出错了
public class Contacts extends Activity {
Button btnGetContacts;
int z = 1;
String namereview = null;
public String phoneNumber1 = "", phoneNumber, name1, url,urljoingroup;
ListView listSliding;
Button slideHandleButton;
HashMap<String, String> map1;
ListContactAdaptor allContactAdapter;
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> resultp = new HashMap<String, String>();
String name;
String message = null;
String sGetContactUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getcontactlist_main);
listSliding = (ListView) findViewById(R.id.GetContacts_Listview);
customer_contact();
}
@SuppressLint("NewApi")
public void customer_contact() {
int timescount = 1, y = 1;
map1 = new HashMap<String, String>();
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.v("Name", name);
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// phoneNumber = phoneNumber.replaceAll("[^\\d.]", "");
map1.put("NAME", phoneNumber);
map1.put("COMMAND", name);
Log.i("name", "name : " + name);
Log.i("count", "count :" + z);
Log.i("pnone", "phone no: " + phoneNumber);
arraylist.add(map1);
Log.i("arraylist", arraylist+"");
}
phones.close();
allContactAdapter = new ListContactAdaptor(XmlActivity.this,
arraylist);
Log.v("ADAPTER", allContactAdapter + "");
// Binds the Adapter to the ListView
listSliding.setAdapter(allContactAdapter);
}
class ListContactAdaptor extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
// public static String CategoryHeading;
public ListContactAdaptor(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
Log.i("data", data + "");
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// Declare Variables
TextView name, command;
Button joinme;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.getcontactlist_view,
parent, false);
// Get the position from the results
resultp = new HashMap<String, String>();
resultp = data.get(position);
name = (TextView) itemView.findViewById(R.id.name);
command = (TextView) itemView.findViewById(R.id.commamd);
joinme= (Button) itemView.findViewById(R.id.getImage);
name.setText(resultp.get("NAME"));
Log.v("GET NAME", resultp.get("NAME"));
if (resultp.get("COMMAND").equals("")) {
command.setText("No Group");
} else {
command.setText(resultp.get("COMMAND"));
}
Log.v("GET COMMAND", resultp.get("COMMAND"));
return itemView;
}
}
}
答案 0 :(得分:4)
将此放入while循环
map1 = new HashMap<String, String>();
像
while (phones.moveToNext())
{
map1 = new HashMap<String, String>();
.
.
}