ViewBinder - null对象参考/还没有在SwipeRefresh上工作?

时间:2015-10-15 00:00:46

标签: java android

我正在使用ViewBinder并且到目前为止已经成功地将它用于SimpleDateFormat,我现在正在尝试同时获取Contect Names而不是数字。

1。我的代码显示不会更新联系人姓名,但可以使用dateformat。我该如何解决这个问题?

2. 我试图在swiperefresh上再次添加视图绑定器,但刷新后它不会格式化我的日期?

非常感谢任何帮助:)

这是我的代码:

        adapter1.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
         public boolean setViewValue(View arg0, Cursor arg1, int arg2) {

            if (arg0.getId() == R.id.lblNumber){
                String HeadNum = arg1.getString(arg2);
                Uri nameuri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(HeadNum));
                Cursor c = getActivity().getContentResolver().query(nameuri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
                String ContactName = "null";
                if (c.moveToFirst()){ ContactName = c.getString(c.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); }
                ((TextView)arg0).setText(ContactName);
            }

            if(arg0.getId() == R.id.lblDate){
                String date = arg1.getString(arg2);
                Long timestamp = Long.parseLong(date);
                String dateString = sdf.format(new Date(timestamp));
                ((TextView)arg0).setText(dateString);
                return true;
            }else
                return false;
        }
    });
    lvMsgList.setAdapter(adapter1);

这是OnRefresh中的代码:

phmsgSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
        @Override
    public void onRefresh(){
            Uri inboxURI = Uri.parse("content://mms-sms/conversations");
            Cursor b = getActivity().getContentResolver().query(inboxURI, new String[]{"*"}, null, null, "normalized_date desc");
            adapter1 = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.row, b,
                    new String[]{"body", "date", "address", "thread_id"},
                    new int[]{R.id.lblMsg, R.id.lblDate, R.id.lblNumber, R.id.lblID}, 0);
            adapter1.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(View arg0, Cursor arg1, int arg2) {
                    if(arg0.getId() == R.id.lblDate){
                        String date = arg1.getString(arg2);
                        Long timestamp = Long.parseLong(date);
                        String dateString = sdf.format(new Date(timestamp));
                        ((TextView)arg0).setText(dateString);
                        return true;
                    }else
                        return false;
                }
            });
            lvMsgList.setAdapter(adapter1);
            phmsgSwipe.setRefreshing(false);
        }
    });

1 个答案:

答案 0 :(得分:0)

我通过更改

解决了这个问题
((TextView)arg0).setText(ContactName);

if (ContactName!=null){
                ((TextView)arg0).setText(ContactName);
                return true;}else return false;