将String缓冲区传递到android中的List中

时间:2015-09-09 06:35:01

标签: android string stringbuffer

我需要在片段布局中显示所有呼叫记录,对于样本我只是试图显示该电话号码和值。这里是字符串phNum。 当我尝试显示所有通话记录时,由于Static中的List<model>字符串,它仅显示最后通话记录。我知道,我使用StringBuffer来收集。所以对于Here List<Model>我需要传递字符串缓冲区我不能这样做。如果可能的话,请提出解决这个问题的想法。

我的班级档案:

public class CallFragment extends Fragment {
    static String phNum;
    String callDuration;
    Date callDate;
    String phNum1;
    String callType = null;
    TextView t;
    ListView l;
    StringBuffer sb;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getdetails();
        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentById(android.R.id.content) == null) {
            DataListFragment list = new DataListFragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        }
        return null;
    }
    private void getdetails() {
        // TODO Auto-generated method stub
        sb = new StringBuffer();
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
        Cursor managedCursor = getActivity().getContentResolver().query(
                CallLog.Calls.CONTENT_URI, 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);
        sb.append("Call Log :");
        while (managedCursor.moveToNext()) {
            phNum = managedCursor.getString(number);
            String callTypeCode = managedCursor.getString(type);
            String strcallDate = managedCursor.getString(date);
            callDate = new Date(Long.valueOf(strcallDate));
            callDuration = managedCursor.getString(duration);
            int callcode = Integer.parseInt(callTypeCode);
            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;
            }
            sb.append("\nPhone Number: " + phNum + " \nCall Type: "+ callType + " \nCall Date: " + callDate+ " \nCall duration in sec : " + callDuration);
        System.out.println("phNum : " + phNum);
        }
        managedCursor.close();
    }
    public static class DataListFragment extends ListFragment implements
            LoaderManager.LoaderCallbacks<List<Model>> {
        CustomArrayAdapter mAdapter;
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            System.out.println("DataListFragment.onActivityCreated");
            setEmptyText("No Data Here");
            mAdapter = new CustomArrayAdapter(getActivity());
            setListAdapter(mAdapter);
            setListShown(false);
            getLoaderManager().initLoader(0, null, this);
        }
    }
    public static class DataListLoader extends AsyncTaskLoader<List<Model>> {
        List<Model> mModels;
        public DataListLoader(Context context) {
            super(context);
        }
        @Override
        public List<Model> loadInBackground() {
            System.out.println("DataListLoader.loadInBackground");
            List<Model> entries = new ArrayList<Model>();
            entries.add(new Model(phNum, "2"));
            return entries;
        }
        @Override
        public void deliverResult(List<Model> listOfData) {
            if (isReset()) {
                if (listOfData != null) {
                    onReleaseResources(listOfData);
                }
            }
            List<Model> oldApps = listOfData;
            mModels = listOfData;
            if (isStarted()) {
                super.deliverResult(listOfData);
            }
            if (oldApps != null) {
                onReleaseResources(oldApps);
            }
        }
        @Override
        protected void onStartLoading() {
            if (mModels != null) {
                deliverResult(mModels);
            }
            if (takeContentChanged() || mModels == null) {
                forceLoad();
            }
        }
        @Override
        protected void onStopLoading() {
            cancelLoad();
        }
        @Override
        public void onCanceled(List<Model> apps) {
            super.onCanceled(apps);
            onReleaseResources(apps);
        }
        @Override
        protected void onReset() {
            super.onReset();
            onStopLoading();
            if (mModels != null) {
                onReleaseResources(mModels);
                mModels = null;
            }
        }
        protected void onReleaseResources(List<Model> apps) {
        }
    }
}

模型文件:

public class Model {
    private String name;
    private String id;
    public Model(String name, String id) {
        this.name = name;
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

1 个答案:

答案 0 :(得分:0)

Solution here

    public class CallLogFragment extends Fragment
{
    private View view;
    private ListView list_calllog;
    private ArrayList<HashMap<String,String>> callLog;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        if(view==null)
        {
            view=inflater.inflate(R.layout.fragment_call_log_layout, container,false);
        }   
        else
        {
            ViewGroup parent = (ViewGroup) view.getParent();
            parent.removeView(view);
        }   

        callLog=getCallLog();
        CustomCallLogListAdapter adapter=new CustomCallLogListAdapter(getActivity(),R.layout.row_call_log_layout,callLog);
        list_calllog=(ListView)view.findViewById(R.id.list_calllog);
        list_calllog.setAdapter(adapter);
        return view;
    }

    @SuppressLint("NewApi")
    public ArrayList<HashMap<String,String>> getCallLog()
    {
        ArrayList<HashMap<String,String>> callLog=new ArrayList<HashMap<String,String>>();
        CursorLoader cursorLoader=new CursorLoader(getActivity(),CallLog.Calls.CONTENT_URI, null, null, null, null);
        Cursor cursor=cursorLoader.loadInBackground();
        if(cursor.moveToFirst())
        {
            while (cursor.moveToNext())
            {
                HashMap<String,String> hashMap=new HashMap<String, String>();
                hashMap.put(CallLog.Calls.NUMBER, cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)));
                hashMap.put(CallLog.Calls.DATE, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
                hashMap.put(CallLog.Calls.DURATION, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));   
                callLog.add(hashMap);
            }
        }
        return callLog;
    }
}