Jul 3, 2011

Android get phone contacts

I'll demonstrate Android get phone contacts example. First create a new Android project in Eclipse . Then in your created xml layout just define the following things:

    


Just add the following codes to your Activity class:
public class ContactFetchActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button contactButton = (Button)findViewById(R.id.button1);
        
  contactButton.setOnClickListener(l);
    }
    
    OnClickListener l = new OnClickListener(){

  @Override
  public void onClick(View arg0) {
   try{
    Uri contactData = ContactsContract.Contacts.CONTENT_URI;
    Cursor contactsCursor = managedQuery(contactData,null, null, null, null);
    Log.i("The first one ", ""+ contactsCursor.getCount());
    while (contactsCursor.moveToNext()) {
     String id = contactsCursor.getString(contactsCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
     String name = contactsCursor.getString(contactsCursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
     String hasPhoneNumber = contactsCursor.getString(contactsCursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
     Toast.makeText(ContactFetchActivity.this, Integer.parseInt(hasPhoneNumber) + "", 11).show();
     if (Integer.parseInt(hasPhoneNumber) > 0) {
      Uri myPhoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
      String phoneNumber = null;
      while (phones.moveToNext()) {
       phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
       Toast.makeText(ContactFetchActivity.this, phoneNumber , 20).show();
      }
      Toast.makeText(ContactFetchActivity.this, id + "-----" + name , 20).show();
     }
     
    }
   }catch(Exception ex){
    Toast.makeText(ContactFetchActivity.this, "Problem because " + ex.getMessage(), 10).show();
   }
   //Uri myPerson = ContentUris.withAppendedId(contentUri, id)
  }
    };
}
Just fix your imports. And then finally on your AndroidManifest.xml file add the line:
Hossain Doula

Spring MVC JSON example

One of the person was asking me to post a Spring MVC JSON example in stackoverflow. I am demonstrating only the code on the fly, not in formal way.

public ModelAndView JsonExample(HttpServletRequest request,
   HttpServletResponse response) throws Exception {

  HashMap  map = new HashMap ();
  String pattern = request.getParameter("tag");
  ArrayList < Map < String , Object > > list = distributorService.getCustomerList();
  

  if (pattern != null && !pattern.equalsIgnoreCase("")) {
   ArrayList < Map < String , Object > > list1 = new ArrayList < Map < String , Object > >();
   for (int i = 0; i < list.size(); i++) {
    
    
    if ((((String) list.get(i).get("caption")).toLowerCase()).contains(pattern.toLowerCase()))
     list1.add(list.get(i));
    
   }
   map.put("customerList", list1);

  } else {
   map.put("customerList", null);

  }
  

  return new ModelAndView(new JSONView(), map);
 }
And then create a service interface and implement that interface:
public ArrayList < Map < String , Object > > getCustomerList(){
  List list = distributorDao.getCustomerList();
  ArrayList < Map < String , Object > > listN = new ArrayList < Map < String , Object > >();
  Map < String , Object > info =null;
  String type="";
  
  for(int i = 0;i < list.size();i++){
   info = new HashMap < String , Object >();
   Object[] obj = (Object[]) list.get(i);
   info.put("value", obj[0]);
   info.put("caption", obj[1]);
   listN.add(info);
  }
  return listN;
 }
And later add the DAO interface and implement that:
public List getCustomerList(){

  String queryStr = "select customerid,customer_name from customer_tbl";
  Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
  Query query = session.createSQLQuery(queryStr);
  List list = query.list();
  
  return list;
 }





--- Hossain Doula