读取android片段列表视图中的调用日志

时间:2016-06-13 10:18:42

标签: android listview android-fragments

我使用此代码读取片段选项卡活动中的调用日志与列表视图当我使用数组适配器添加值时,应用程序不幸停止但是没有运行时或编译时错误 我该如何解决?

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:致命异常:   主要       进程:com.ndot.developer.mcp,PID:2998       java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView

3 个答案:

答案 0 :(得分:2)

只需将onCreateView()中的布局ID从simple_list_item_2更改为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;
}

说明:

为什么不android.R.layout.simple_list_item_2

  

创建新的ArrayAdapter 需要 TextView 作为布局   参数,但android.R.layout.simple_list_item_2是一个   TwoLineListItem布局中包含两个TextView。 您   可以输入android.R.layout.simple_list_item_2进行检查   java文件中的字符串将光标放在其上,然后按ctrl键。   你可以看到布局。

为什么android.R.layout.simple_list_item_1

  

android.R.layout.simple_list_item_1本身就是 TextView 。哪一个   满足ArrayAdapter布局的要求   TextView中。这就是我建议使用这种布局的原因。

答案 1 :(得分:0)

由于您没有将TextviewId传递给适配器,因此会崩溃应用程序。

替换此

arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_2,Calllist1);

arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1,Calllist1);

答案 2 :(得分:0)

尝试以下代码,

ArrayAdapter  arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1,Calllist1);