Android ContentResolver query

In this Android tip, I am going to show you how to use the query() method of the ContentResolver class to retrieve call logs in an Android device. The query method has the following prototype:

query(Uri url, String[] projection, String selection, String selectionArgs, String sortOrder)

With the url argument, you can specify a table in which the data will be retrieved. To refer to the call logs table, you can write CallLog.Calls.CONTENT_URI . The projection is a list of columns to fetch the data. The selection is WHERE clause (excluding the WHERE itself) to filter rows of data. Passing null will return all rows from the table. You may include ?s in selection, which will be replaced by the values from selectionArgs. The selectionArgs are values to replace the ? symbol in the selection argument. The sortOrder specified how to sort the rows. Passing null will use the default sort order, which may be unordered.



Now to have a workable example application on using query method of ContentResover in Android, you need to create a new Android project. Then modify the activity_main.xml file to add a ListView as shown below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
  
   >
<ListView
    
android:id="@+id/lst_calls"
android:layout_width="fill_parent"
android:layout_height="fill_parent"


/>
  

</LinearLayout>

In this example, the ListView displays number, date, and type of call logs. You need to customize the ListView this goal. In this ListView customization, you will need two files: list_item.xml and CustoAdapter.java file. The list_item.xml file represents row template of the ListView and the CustomAdapter will be used to feed data to the ListView.

list_item.xml


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="5dp" >
  
<TextView
     android:id="@+id/txt_number"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_marginRight="10dp"
       />

<TextView
     android:id="@+id/txt_date"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
android:layout_toRightOf="@+id/txt_number"
android:layout_marginRight="10dp"
      />

<TextView
     android:id="@+id/txt_type"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
android:layout_toRightOf="@+id/txt_date"
      />

  
</RelativeLayout>



CustomAdapter.java

package com.example.andtip;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String> {

int groupid;
ArrayList<String> records;
Context context;

public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){
super(context,vg, id, records);
this.context=context;
groupid=vg;
this.records=records;

}

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

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View itemView = inflater.inflate(groupid, parent, false);
     String[] row_items=records.get(position).split("__");
     TextView textNumber= (TextView) itemView.findViewById(R.id.txt_number);
     textNumber.setText(row_items[0]);
     TextView textDate= (TextView) itemView.findViewById(R.id.txt_date);
     textDate.setText(row_items[1]);
     TextView textType= (TextView) itemView.findViewById(R.id.txt_type);
     textType.setText(row_items[2]);
    
     return itemView;
}
}

Open the MainActivity.java file. In the onCreate() method, you will write code to create a reference to the ListView, create an object of CustomAdapter, set the adapter object to the ListView. In the onStart() method, the code is written to retrieve call logs from an Android device and refresh data in the ListView.

MainActivity.java

  package com.example.andtip;
import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.ListView;
import android.provider.CallLog;


public class MainActivity extends Activity{

   ArrayList<String> records;
   CustomAdapter adapter;
   ListView listview;
   protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   context=this;
   records=new ArrayList<String>();
   listview=(ListView)findViewById(R.id.lst_calls);
   adapter=new CustomAdapter(this, R.layout.list_item, R.id.txt_number,records);
   listview.setAdapter(adapter);
  
  
   }

   public void onStart(){
   super.onStart();
   //get an instance of ContentResolver
   ContentResolver cr=getContentResolver();
   //define columns (number, date, and type) to fetch data
   String[] projection={CallLog.Calls.NUMBER,CallLog.Calls.DATE, CallLog.Calls.TYPE};
   //use query method to fetch data from the call logs table
   Cursor cs=cr.query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
   if(cs.getCount()>0)
   while(cs.moveToNext()){
   String number=cs.getString(cs.getColumnIndex(projection[0]));
   Long date=cs.getLong(cs.getColumnIndex(projection[1]));
   Date d=new Date(date);
   CharSequence callDate=DateFormat.format("yyyy-MM-dd hh:mm:ss", d);
   String type=cs.getString(cs.getColumnIndex(projection[2]));
   String item=number+"__"+callDate.toString()+"__"+type;
   records.add(item);
  
   }
   //notify listview of dataset changed
   adapter.notifyDataSetChanged();
}
}

Before running the example application, you need to add a permission to the AndroidManifest.xml file to read data from the call logs table on an Android device.

  <uses-permission android:name="android.permission.READ_CALL_LOG"/>

Android-ContentResolver-query

Posted by: Dara | post date: 08-10-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.