2016-10-21 58 views
-4

我需要顯示通話記錄中的ListView的片段如何在Fragment上的ListView中顯示通話記錄?

setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, 
      R.id.tvNameMain, conNames)); 
    return rootView; 

而且全部代碼

private ListView mListView; 

private ArrayList<String> conNames; 
private ArrayList<String> conNumbers; 
private ArrayList<String> conTime; 
private ArrayList<String> conDate; 
private ArrayList<String> conType; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.activity_recents, container, false); 
    mListView = (ListView)rootView.findViewById(R.id.list); 

    conNames = new ArrayList<String>(); 
    conNumbers = new ArrayList<String>(); 
    conTime = new ArrayList<String>(); 
    conDate = new ArrayList<String>(); 
    conType = new ArrayList<String>(); 

    Cursor curLog = getAllCallLogs(getActivity().getContentResolver()); 

    setCallLogs(curLog); 

    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, 
      R.id.tvNameMain, conNames)); 
    return rootView; 
} 

類MyAdapter

private class MyAdapter extends ArrayAdapter<String> { 

    public MyAdapter(Context context, int resource, int textViewResourceId, 
        ArrayList<String> conNames) { 
     super(context, resource, textViewResourceId, conNames); 

    } 

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

     View row = setList(position, parent); 
     return row; 
    } 

    private View setList(int position, ViewGroup parent) { 
     LayoutInflater inf = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View row = inf.inflate(R.layout.liststyle, parent, false); 

     TextView tvName = (TextView) row.findViewById(R.id.tvNameMain); 
     TextView tvNumber = (TextView) row.findViewById(R.id.tvNumberMain); 
     TextView tvTime = (TextView) row.findViewById(R.id.tvTime); 
     TextView tvDate = (TextView) row.findViewById(R.id.tvDate); 
     TextView tvType = (TextView) row.findViewById(R.id.tvType); 

     tvName.setText(conNames.get(position)); 
     tvNumber.setText(conNumbers.get(position)); 
     tvTime.setText("(" + conTime.get(position) + "sec)"); 
     tvDate.setText(conDate.get(position)); 
     tvType.setText("(" + conType.get(position) + ")"); 

     return row; 
    } 
} 

類CallLog

private void setCallLogs(Cursor curLog) { 
    while (curLog.moveToNext()) { 
     String callNumber = curLog.getString(curLog 
       .getColumnIndex(android.provider.CallLog.Calls.NUMBER)); 
     conNumbers.add(callNumber); 

     String callName = curLog 
       .getString(curLog 
         .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME)); 
     if (callName == null) { 
      conNames.add("Unknown"); 
     } else 
      conNames.add(callName); 

     String callDate = curLog.getString(curLog 
       .getColumnIndex(android.provider.CallLog.Calls.DATE)); 
     SimpleDateFormat formatter = new SimpleDateFormat(
       "dd-MMM-yyyy HH:mm"); 
     String dateString = formatter.format(new Date(Long 
       .parseLong(callDate))); 
     conDate.add(dateString); 

     String callType = curLog.getString(curLog 
       .getColumnIndex(android.provider.CallLog.Calls.TYPE)); 
     if (callType.equals("1")) { 
      conType.add("Incoming"); 
     } else 
      conType.add("Outgoing"); 

     String duration = curLog.getString(curLog 
       .getColumnIndex(android.provider.CallLog.Calls.DURATION)); 
     conTime.add(duration); 

    } 
} 

類光標

public static Cursor getAllCallLogs(ContentResolver cr) { 
    // reading all data in descending order according to DATE 
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
    Uri callUri = Uri.parse("content://call_log/calls"); 
    Cursor curCallLogs = cr.query(callUri, null, null, null, strOrder); 

    return curCallLogs; 
} 
+0

請添加更多的代碼 - 2號線是不夠的人來幫助你。描述什麼是錯的。 –

回答

0

如果你想用你的setListAdapter應該Fragment延伸ListFragment。但在你的情況下,將其更改爲mListView.setAdapter([...])應該做的伎倆

變化

setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.tvNameMain, conNames)); 

mListView.setAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.tvNameMain, conNames)); 
+0

這是.... ArrayAdapter adapter = new MyAdapter(getActivity()。getApplicationContext(),R.layout.liststyle,R.id.tvNameMain,conNames); mListView.setAdapter(adapter); – Piyawat

+0

我不明白什麼是錯的 – theBugger

+0

在我的情況下,我必須解決它。它到mListView.setAdapter << it Error – Piyawat

相關問題