I want to get all Contacts from phone and simply display in android ListView, without store in database,the purpose is that when user click on button "sync all contacts" then ALL contacts should be fetched and show in ListView. Here is the main activity class code
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
}
});
}
}
here is Contats_list class
public class Contact_list {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Contact_list(String name, String phone) {
this.name = name;
this.phone = phone;
}
}
Here is Custom_adapter class
public class Custom_adapter extends ArrayAdapter<Contact_list> {
private final LayoutInflater inflater;
private final Context context;
private ViewHolder holder;
private final ArrayList<Contact_list> persons;
//ArrayList<Contact_list> mylist;
public Custom_adapter(Context context, ArrayList<Contact_list> persons) {
super(context,0, persons);
this.context = context;
this.persons = persons;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();
}
@Override
public Contact_list 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 p) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.custom_adapter, null);
holder = new ViewHolder();
holder. name = (TextView) convertView.findViewById(R.id.txt_name);
holder. phone = (TextView) convertView.findViewById(R.id.txt_phone);
convertView.setTag(holder);
}else{
//Get viewholder we already created
holder = (ViewHolder)convertView.getTag();
}
Contact_list person = persons.get(position);
if(person != null){
holder.name.setText(person.getName());
holder.phone.setText(person.getPhone());
}
return convertView;
}
private static class ViewHolder {
TextView name;
TextView phone;
}
}
please tell me how i can get contacts and show in listview i am new here any help will be appreciated.