从Bmob读取数据实现下拉刷新

移动开发 码拜 8年前 (2017-05-05) 2083次浏览
从云端读取信息后传到数据源,然后绑定数据适配器实现下拉刷新,有BUG求帮忙改正

swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshList();
                listList.setAdapter(mAdapter);
                listList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Toast.makeText(ContentActivity.this, "you clicked", Toast.LENGTH_SHORT).show();
                    }
                });
                Toast.makeText(ContentActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
//                mAdapter.notifyDataSetChanged();
            }
        });
    }
    private void refreshList() {
        BmobQuery<Lost> query = new BmobQuery<Lost>();
        query.order("-createdAt");
        query.findObjects(this, new FindListener<Lost>() {
            @Override
            public void onSuccess(List<Lost> list) {
                mLostList.addAll(list);
                mAdapter = new LostAdapter(ContentActivity.this, mLostList);
                swipeRefresh.setRefreshing(false);
                Log.d(TAG, "onSuccess: " + "ccccccccccccccccccccccccccccccccccccccccccccccc");
            }
            @Override
            public void onError(int i, String s) {
                Toast.makeText(ContentActivity.this, "刷新失败", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onError: " + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
            }
        });
    }

以下是适配器

public class LostAdapter extends BaseAdapter{
    private List<Lost> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    public LostAdapter(Context context,List<Lost> list){
        mContext = context;
        mList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.list_item,null);
            viewHolder.mTvLostTitle = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.mTvLostPhone = (TextView) convertView.findViewById(R.id.tv_phone);
            viewHolder.mTvLostDescribe = (TextView) convertView.findViewById(R.id.tv_describe);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Lost lost = mList.get(position);
        viewHolder.mTvLostTitle.setText(lost.getTitle());
        viewHolder.mTvLostPhone.setText(lost.getPhone());
        viewHolder.mTvLostDescribe.setText(lost.getDescribe());
        return convertView;
    }
    private static class ViewHolder {
        public TextView mTvLostTitle;
        public TextView mTvLostPhone;
        public TextView mTvLostDescribe;
    }
}
解决方案

40

引用:
Quote: 引用:

你这样岂不是每刷新一次就把listview set一下adapter,和加点击事件?
把listview设置适配器和添加点击事件的代码提出来,别放到刷新事件里,例如就放到oncreate里,刷新事件只操作数据源,操作完notify一下

你好,本人改正后,第一次刷新没反应,第二次刷新,可以显示出信息,但是第三次刷新,程序崩溃。提示本人空指针,可是本人检查了本人觉得不应该是空的呀。提示如下:
java.lang.NullPointerException: Attempt to read from field “android.widget.TextView com.example.bmobtest.LostAdapter$ViewHolder.mTvLostTitle1” on a null object reference
at com.example.bmobtest.LostAdapter.getView(LostAdapter.java:55)
本人的适配器如下:

public class LostAdapter extends BaseAdapter{
    private List<Lost> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    public LostAdapter(Context context,List<Lost> list){
        mContext = context;
        mList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.list_item,null);
            viewHolder.mTvLostTitle1 = (TextView) convertView.findViewById(R.id.tv_title1);
            viewHolder.mTvLostPhone1 = (TextView) convertView.findViewById(R.id.tv_phone1);
            viewHolder.mTvLostDescribe1 = (TextView) convertView.findViewById(R.id.tv_describe1);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Lost lost = mList.get(position);
        viewHolder.mTvLostTitle1.setText(lost.getTitle());
        viewHolder.mTvLostPhone1.setText(lost.getPhone());
        viewHolder.mTvLostDescribe1.setText(lost.getDescribe());
        return convertView;
    }
    private static class ViewHolder {
        public TextView mTvLostTitle1;
        public TextView mTvLostPhone1;
        public TextView mTvLostDescribe1;
    }
}

Lost类如下:

public class Lost extends BmobObject {
    private String title;//标题
    private String describe;//描述
    private String phone;//联系手机
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescribe() {
        return describe;
    }
    public void setDescribe(String describe) {
        this.describe = describe;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

谢谢你

viewholder你没settag的原因吧


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明从Bmob读取数据实现下拉刷新
喜欢 (0)
[1034331897@qq.com]
分享 (0)