pulltorefresh下拉后不能恢复刷新前

我用 ContentProvider 做了一个查询系统中联系人的 DEMO,将查询出来的联系人信息放在 ListView 里面展示,用到了 pulltorefresh 框架,但是遇到一个问题,就是在下拉刷新之后那个“正在刷新” 会一起显示,并不会自己恢复。求大家帮忙看一下,我用的列表适配器是自定义的 CursorAdapter ,官方文档上面说用 CursorAdapter 要备份一个 ListView 和一个 Cursor ,我不明白是什么意思,我在 onrefresh 里面调用了 listview.onRefreshComplete() 方法,但是也不会恢复,我估计是因为用了 CursorAdapter 的缘故。

这个是 Activity 页面

 package com.caoligai.contentproviderdemo.activity;

import com.caoligai.contentproviderdemo.R;
import com.caoligai.contentproviderdemo.adapter.MyCursorAdapter;
import com.handmark.pulltorefresh.library.IPullToRefresh;
import com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * * @author caoligai E-mail: * @date 创建时间:2015-5-7 下午8:50:08 * @version 1.0 * @parameter
 * * @since * @return
 */
public class ContactList extends Activity {

    String tag = "ContactList";

    PullToRefreshListView listview;
    MyCursorAdapter adapter;
    ProgressDialog pd;

    Cursor cursor;
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, };

    boolean isFirstLoad = true;

    public ListView actualListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactlist);

        // Log.d(tag, "cursor's length = " + cursor.getCount());
        listview = (PullToRefreshListView) findViewById(R.id.listview);
        listview.setMode(Mode.PULL_FROM_START);



        listview.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                refreshView.setRefreshing(true);
                listview.onRefreshComplete();
            }
        });


        new listviewLoader().execute(null, null, null);
    }

    class listviewLoader extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {

                Log.d(tag, "listviewLoader.onPreExecute()");
                pd = new ProgressDialog(ContactList.this);
                pd.setCancelable(false);
                pd.setMessage("正在查询数据...");
                pd.show();

        }

        @Override
        protected Void doInBackground(Void... params) {
                Log.d(tag, "listviewLoader.doInBackground()");
                cursor = getContentResolver().query(Contacts.CONTENT_URI,
                        projection, null, null, null);
                Log.d(tag, "cursor.length = " + cursor.getCount());
                isFirstLoad = false;
                return null;


        }

        @Override
        protected void onPostExecute(Void result) {

                Log.d(tag, "listviewLoader.onPostExecute()");
                pd.dismiss();
                adapter = new MyCursorAdapter(ContactList.this, cursor,
                        getLayoutInflater(), getContentResolver());
                listview.setAdapter(adapter);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }



}

这个是自定义的 CursorAdapter

 package com.caoligai.contentproviderdemo.adapter;

import com.caoligai.contentproviderdemo.R;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

/**
 * * @author caoligai
 * 
 * 自定义游标适配器,用于将 cursor 游标的数据映射到 ListView 中
 * 
 */
public class MyCursorAdapter extends CursorAdapter {

    LayoutInflater flater;
    Context context;
    ContentResolver cr;

    public MyCursorAdapter(Context context, Cursor c, LayoutInflater flater,
            ContentResolver cr) {
        super(context, c);
        this.context = context;
        this.flater = LayoutInflater.from(context);
        this.cr = cr;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        View item = flater.inflate(R.layout.list_item, null);
        holder.name = (TextView) item.findViewById(R.id.item_name);
        holder.number = (TextView) item.findViewById(R.id.item_number);
        item.setTag(holder);
        return item;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        // TextView name = (TextView) view.findViewById(R.id.item_name);
        // 设置姓名的值
        holder.name.setText(cursor.getString(cursor
                .getColumnIndex(Contacts.DISPLAY_NAME)));

        // 根据联系人 id 查询电话号码
        Cursor idcursor = cr
                .query(CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        CommonDataKinds.Phone.CONTACT_ID
                                + " = "
                                + cursor.getString(cursor
                                        .getColumnIndex(ContactsContract.Contacts._ID)),
                        null, null);
        //一个联系人可能有多个电话号码,这里只处理同一个联系人的第一个号码
        Log.d(null, "idcursor.length = " + idcursor.getCount());
        if (idcursor.moveToNext()) {
            holder.number
                    .setText(idcursor.getString(idcursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        }

    }

    static class ViewHolder {
        TextView name;
        TextView number;
    }

}

运行之后一起保持这个样子

图片说明

3个回答

onRefreshComplete()方法不要写在setOnRefreshListener里,写在异步的onPostExecute里。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐