public class MyContactsActivity extends Activity {
List<Contacts> my_contacts= new ArrayList<>();
LinearLayout listLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_contacts);
listLayout= (LinearLayout) findViewById(R.id.list);
readContacts();
}
public void readContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, ContactsContract.Contacts.DISPLAY_NAME);
try{
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Contacts cont =new Contacts();
cont.setName(name);
List<String> ph_list= new ArrayList<>();
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ph_list.add(phone);
}
pCur.close();
cont.setPhone(ph_list);
my_contacts.add(cont);
}
}
} cur.close();
}catch(Exception e){
e.printStackTrace();
}
for(int no_of_contacts=0; no_of_contacts < my_contacts.size(); no_of_contacts++){
LinearLayout child= new LinearLayout(this);
child.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
child.setOrientation(LinearLayout.HORIZONTAL);
CheckBox rb= new CheckBox(this);
rb.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0.2f));
rb.setTag(no_of_contacts);
rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int tag = (Integer) buttonView.getTag();
if (isChecked) {
my_contacts.get(tag).setChecked(true);
} else {
my_contacts.get(tag).setChecked(false);
}
}
});
LinearLayout contact_info= new LinearLayout(this);
contact_info.setLayoutParams(new LinearLayout.LayoutParams(0, ActionBar.LayoutParams.MATCH_PARENT,1f));
contact_info.setOrientation(LinearLayout.VERTICAL);
TextView contact_name = new TextView(this);
contact_name.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT));
contact_name.setText(my_contacts.get(no_of_contacts).getName());
TextView phone = new TextView(this);
phone.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT));
phone.setText(my_contacts.get(no_of_contacts).getPhone().get(0));
TextView phone_2 = new TextView(this);
phone_2.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT));
phone_2.setText(my_contacts.get(no_of_contacts).getPhone().get(1));
contact_info.addView(contact_name);
contact_info.addView(phone);
contact_info.addView(phone_2);
View line_view=new View(this);
line_view.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 1));
line_view.setBackgroundResource(R.color.black_line);
child.addView(rb);
child.addView(contact_info);
listLayout.addView(child);
listLayout.addView(line_view);
}
}
}
Be the first to comment.