Tester Tester - 5 months ago 14
Android Question

reading call logs in android fragments list view

i use this code for reading call logs in fragments tab activity with list view when i use the array adapter to add the values the app is unfortunately stopped but there is no run time or compile time error too
how do i resolve it??

public class TabFragment2 extends Fragment {

ArrayList<String> Calllist1 = new ArrayList<>();
ListView callListView;
ArrayAdapter<String> arrayAdapter;

public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_call, container, false);
callListView = (ListView)view.findViewById(R.id.listView1);
arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_2,Calllist1);
callListView.setAdapter(arrayAdapter);
getCallDetails();
return view;
}

private void getCallDetails() {

String strOrder = CallLog.Calls.DATE + " DESC";
Uri callUri = Uri.parse("content://call_log/calls");
ContentResolver ca = getActivity().getContentResolver();
Cursor managedCursor = getActivity().getContentResolver().query(callUri, null, null, null, strOrder);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

while (managedCursor.moveToNext()) {
String phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDate = new Date(Long.valueOf(strcallDate));
String callDuration = managedCursor.getString(duration);
Calllist1.add(phNum);
Calllist1.add(callTypeCode);
Calllist1.add(strcallDate);
Calllist1.add(callDuration);

int callcode = Integer.parseInt(callTypeCode);
String callType;
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;

}

}
managedCursor.close();

}

}



06-13 15:51:46.663 2998-2998/? E/AndroidRuntime﹕ FATAL EXCEPTION:
main
Process: com.ndot.developer.mcp, PID: 2998
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

Answer

Just change the Layout ID in your onCreateView() from simple_list_item_2 to simple_list_item_1

public View onCreateView(LayoutInflater inflater,ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.activity_call, container, false);
    callListView = (ListView)view.findViewById(R.id.listView1);
    arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1,Calllist1);
    callListView.setAdapter(arrayAdapter);
    getCallDetails();
    return view;
}
Comments