I am trying to access list of details of all missed calls
from my phone.
I am just trying to access first 50
missed calls.
The problem is that when some of the rows in the listview
are filled and some are empty.
This is how it looks i.e blank rows.
This is the java file in which i am trying to access the call details.
public class Second extends AppCompatActivity {
TextView call;
ListView l;
int count = 0, k = 0;
static int flag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
StringBuffer sb = new StringBuffer();
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
int count = 0;
int dircode = 0;
String[] array = new String[50];
int count1 = 0, count2 = 0, count3 = 0, param = 0;
int len=0;
while (managedCursor.moveToNext() &&len<50) {
param = 0;
String phNumber = managedCursor.getString(number);
String namee = managedCursor.getString(name);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
if (flag == 1) {
if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE) {
dir = "MISSED";
sb.append("\nName: " + namee + "\nPhone Number: " + phNumber + " \nCall Type: " + dir + " \nCall Date: " + callDayTime
+ " \nCall duration in sec : " + callDuration);
}
param = 1;
}
if (flag == 2) {
if (Integer.parseInt(callType) == CallLog.Calls.INCOMING_TYPE) {
dir = "RECEIVED";
sb.append("\nName: " + namee + "\nPhone Number: " + phNumber + " \nCall Type: " + dir + " \nCall Date: " + callDayTime
+ " \nCall duration in sec : " + callDuration);
}
param = 1;
}
if (flag == 3) {
Log.d("asd", "inside flag3");
if (Integer.parseInt(callType) == CallLog.Calls.OUTGOING_TYPE) {
dir = "DIALLED";
sb.append("\nName: " + namee + "\nPhone Number: " + phNumber + " \nCall Type: " + dir + " \nCall Date: "
+ callDayTime
+ " \nCall duration in sec : " + callDuration);
}
param = 1;
}
if (flag == 4) {
if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE)
count1++;
if (Integer.parseInt(callType) == CallLog.Calls.INCOMING_TYPE)
count2++;
if (Integer.parseInt(callType) == CallLog.Calls.OUTGOING_TYPE)
count3++;
continue;
}
if (param == 1) {
array[k] = new String(sb);
k++;len++;
}
sb.setLength(0);//flushing the buffer
}
if (flag == 4) {
sb.append("Missed Type : " + count1 + "\nReceived type : " + count2 + "\nDialled type: " + count3);
array[k] = String.valueOf(sb);
k++;
}
l = (ListView) findViewById(R.id.listView);//creating listview
for (int i = 0; i < array.length; i++) {
if(array[i]==null)
Log.d("asd","NULLLLLL "+ i);
else
Log.d("asd","NOT NULL "+i);
}
l.setAdapter(new CustomAdapter(this, array));
managedCursor.close();
}catch (SecurityException e){}
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
String[] your_array_list;
public CustomAdapter(Context context,String [] your_array_list)
{
this.your_array_list=your_array_list;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return your_array_list.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
rowView = inflater.inflate(R.layout.each_row, null);
TextView textView= (TextView) rowView.findViewById(R.id.textview);
textView.setText(your_array_list[position]);
return rowView;
}
}
To verify that all 50
items are not null,i cheked that if some of them are null using this code---
for (int i = 0; i < array.length; i++) {
if(array[i]==null)
Log.d("asd","NULLLLLL "+ i);
else
Log.d("asd","NOT NULL "+i);
}
But all 50 iterations are printing NOT NULL
.
So,if the strings in the array
of strings are not null,then why some of the rows in listview are blank ?
Can someone help me with this problem ?
You should move "param=1" statements into internal if statements. What is the problem. For example, flag==1. But the call type is not MISSED_TYPE:
...
if (flag == 1) {
if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE) {
dir = "MISSED";
sb.append("\nName: " + namee + "\nPhone Number: " + phNumber + " \nCall Type: " + dir + " \nCall Date: " + callDayTime
+ " \nCall duration in sec : " + callDuration);
}
param = 1;
}
...
So, your sb will be empty, but you set param to 1 and this empty sb will be added to the array. You should replace all if statements with:
...
if (flag == 1 && Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE) {
dir = "MISSED";
sb.append("\nName: " + namee + "\nPhone Number: " + phNumber + " \nCall Type: " + dir + " \nCall Date: " + callDayTime
+ " \nCall duration in sec : " + callDuration);
param = 1;
}
...