Android 为ListView在Arraylist中添加联系人时出错
您好,在我的项目中,我必须从电话中获取联系人电话号码和姓名。我可以获取call电话号码和姓名,但当我将其添加到Android 为ListView在Arraylist中添加联系人时出错,android,listview,arraylist,hashmap,android-contacts,Android,Listview,Arraylist,Hashmap,Android Contacts,您好,在我的项目中,我必须从电话中获取联系人电话号码和姓名。我可以获取call电话号码和姓名,但当我将其添加到ArrayList中时,我只获取最后一个值,我不知道为什么。但是当我记录下来的时候,我得到了所有的价值。你们能帮我吗?我哪里出错了 public class Contacts extends Activity { Button btnGetContacts; int z = 1; String namereview = null; public Str
ArrayList
中时,我只获取最后一个值,我不知道为什么。但是当我记录下来的时候,我得到了所有的价值。你们能帮我吗?我哪里出错了
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;
}
}
}
公共类联系人扩展活动{
按钮btnGetContacts;
intz=1;
字符串namereview=null;
公共字符串phoneNumber1=“”,phoneNumber,name1,url,urljoingroup;
列表视图列表滑动;
按钮滑动手柄按钮;
HashMap-map1;
ListContactAdapter所有ContactAdapter;
ArrayList ArrayList=新的ArrayList();
HashMap resultp=新的HashMap();
字符串名;
字符串消息=null;
字符串sGetContactUrl;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.getcontactlist_main);
ListSlailing=(ListView)findViewById(R.id.GetContacts\u ListView);
客户_联系人();
}
@SuppressLint(“新API”)
公共无效客户_联系人(){
int timescount=1,y=1;
map1=新的HashMap();
Cursor=getContentResolver().query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,
空,空);
while(phones.moveToNext()){
姓名=电话
.getString(电话)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_NAME));
Log.v(“名称”,名称);
电话号码=电话
.getString(电话)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
//phoneNumber=phoneNumber.replaceAll(“[^\\d.]”,即“”);
map1.输入(“姓名”,电话号码);
map1.put(“命令”,名称);
Log.i(“名称”,“名称:”+名称);
Log.i(“计数”,“计数:+z”);
Log.i(“pnone”,“电话号码:”+电话号码);
arraylist.add(map1);
Log.i(“arraylist”,arraylist+);
}
电话。关闭();
allContactAdapter=新的ListContactAdapter(XmlActivity.this,
arraylist);
Log.v(“适配器”,allContactAdapter+”);
//将适配器绑定到ListView
ListSlidering.setAdapter(allContactAdapter);
}
类ListContactAdapter扩展了BaseAdapter{
//声明变量
语境;
充气机;
阵列列表数据;
//公共静态字符串类别标题;
公共ListContactAdapter(上下文,
ArrayList(ArrayList){
this.context=上下文;
数据=数组列表;
Log.i(“数据”,数据+”);
}
@凌驾
public int getCount(){
返回data.size();
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回0;
}
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
//声明变量
TextView名称、命令;
按钮接合;
充气器=(充气器)上下文
.getSystemService(上下文布局\充气机\服务);
视图项视图=充气机。充气(R.layout.getcontactlist\u视图,
父母,假);
//从结果中获得位置
resultp=newhashmap();
resultp=data.get(位置);
name=(TextView)itemView.findViewById(R.id.name);
command=(TextView)itemView.findViewById(R.id.commamd);
joime=(按钮)itemviewbyd(R.id.getImage);
name.setText(resultp.get(“name”);
Log.v(“获取名称”,resultp.GET(“名称”);
if(resultp.get(“命令”).equals(“”){
command.setText(“无组”);
}否则{
setText(resultp.get(“command”);
}
Log.v(“获取命令”,resultp.GET(“命令”);
返回项目视图;
}
}
}
将其放入while循环
map1 = new HashMap<String, String>();
map1=newhashmap();
像
while(phones.moveToNext())
{
map1=新的HashMap();
.
.
}
将其放入while循环
map1 = new HashMap<String, String>();
map1=newhashmap();
像
while(phones.moveToNext())
{
map1=新的HashMap();
.
.
}
是的,还可以写电话。moveToFirst();before while loop.guys我怎么能用它作为多选列表视图你能帮我吗,因为我需要多选联系人和发送短信,还需要写电话。moveToFirst();伙计们,我怎么能用这个作为多选列表视图呢?你能帮我吗?因为我需要多选联系人和发送短信