listview - reading call logs in android fragments list view -


i use code reading call logs in fragments tab activity list view when use array adapter add values app unfortunately stopped there no run time or compile time error how 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 layout 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 resource id textview

just change layout id in oncreateview() simple_list_item_2 simple_list_item_1

public view oncreateview(layoutinflater inflater,viewgroup container,                          bundle savedinstancestate) {     // inflate layout 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; } 

explanation :

why not android.r.layout.simple_list_item_2?

creating new arrayadapter need textview layout parameter, android.r.layout.simple_list_item_2 twolinelistitem layout containing 2 textviews inside it. you can check typing android.r.layout.simple_list_item_2 string in java file put cursor on , press ctrl key. can see layout.

why android.r.layout.simple_list_item_1?

android.r.layout.simple_list_item_1 textview. fulfills requirement of arrayadapter layout being textview. reason have suggested use layout.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -