gpt4 book ai didi

android - 如何从手机中获取所有联系人并在 android 中单击按钮时显示在 ListView 中?

转载 作者:行者123 更新时间:2023-11-29 17:05:20 25 4
gpt4 key购买 nike

我想从手机中获取所有联系人并简单地显示在 android ListView 中,而不存储在数据库中,目的是当用户单击“同步所有联系人”按钮时,应获取所有联系人并显示在 ListView 中。这是主要的 Activity 类,我还创建了带有变量 contacts_namephone_no 以及 CustomAdapterclass

的 getter-setter 类
public class MainActivity extends AppCompatActivity {

ListView listView;
Button sync;



ArrayList<newlist> listitem;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listitem = new ArrayList<newlist>();
listView = (ListView) findViewById(R.id.listViewID);
registerForContextMenu(listView);

sync= (Button) findViewById(R.id.syncID);
sync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//what method will be used here for sync contacts and display in listview
}
});
}
}

如有任何帮助,我们将不胜感激。

最佳答案

尝试使用这个库,您可以轻松获取所有联系人。

https://github.com/EverythingMe/easy-content-providers

对于ListView,你需要写一个自定义适配器,你也可以在网上找到很多例子。这只是一个示例。

public class CustomListViewAdapter extends ArrayAdapter<Person> {

private final LayoutInflater inflater;
private final Context context;
private ViewHolder holder;
private final ArrayList<Person> persons;

public CustomListViewAdapter(Context context, ArrayList<Person> persons) {
super(context,0, persons);
this.context = context;
this.persons = persons;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return persons.size();
}

@Override
public Person getItem(int position) {
return persons.get(position);
}

@Override
public long getItemId(int position) {
return persons.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {

convertView = inflater.inflate(R.layout.list_view_item, null);

holder = new ViewHolder();
holder.personImage = (ImageView) convertView.findViewById(R.id.person_image);
holder.personNameLabel = (TextView) convertView.findViewById(R.id.person_name_label);
holder.personAddressLabel = (TextView) convertView.findViewById(R.id.person_address_label);
convertView.setTag(holder);

}
else{
//Get viewholder we already created
holder = (ViewHolder)convertView.getTag();
}

Person person = persons.get(position);
if(person != null){
holder.personImage.setImageResource(person.getPhotoId());
holder.personNameLabel.setText(person.getName());
holder.personAddressLabel.setText(person.getAddress());

}
return convertView;
}

//View Holder Pattern for better performance
private static class ViewHolder {
TextView personNameLabel;
TextView personAddressLabel;
ImageView personImage;

}
}

onCreate 中阅读库的文档并尝试弄清楚如何获取联系人。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// GET CONTACTS DATA
initialize();
}

private void initialize() {
persons = new ArrayList<Person>();
listView = (ListView) findViewById(R.id.person_list_view);
listViewAdapter = new CustomListViewAdapter(MainActivity.this,persons);
listView.setAdapter(listViewAdapter);
}

关于android - 如何从手机中获取所有联系人并在 android 中单击按钮时显示在 ListView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41962510/

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