拉到刷新示例,Eclipse没有找到setOnRefreshListener命令

时间:2013-05-24 16:26:59

标签: android eclipse pull-to-refresh

我正在处理来自Git-hub

的众所周知的拉动刷新示例

我加载了库,一切正常,但是当我想调用方法setOnRereshListener时,Eclipse找不到它。可能有什么问题?

这是来自示例的代码:

        PullToRefreshListView pullToRefreshView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);      
    pullToRefreshView.setOnRefreshListener(new OnRefreshListener<ListView>() {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    });

这是我的代码:

public class MainActivity extends Activity {
private static final String URL = "http://192.168.1.103/php-android/testphp.php"; 
private static final String TAG_DATA = "data";
private static final String TAG_ID = "name";       
private static final String TAG_DATE = "date";      
public PullToRefreshListView listView;  
JSONArray data = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .detectAll().penaltyLog().build(); 
    StrictMode.setThreadPolicy(policy);
    getDataInArray();

    PullToRefreshListView pullToRefreshView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);      
    pullToRefreshView.setOnRefreshListener(new OnRefreshListener<ListView>() {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    });     
}

2 个答案:

答案 0 :(得分:3)

不确定,但我相信这是由于方法签名OnRefresh(PullToRefreshBase refreshView),尝试删除PullToRefreshBase refreshView,如下所示:

pullToRefreshView.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Do work to refresh the list here.

            new GetDataTask().execute();
        }
    });

我使用相同的结构而没有签名方法OnRefresh(),它正常工作。

答案 1 :(得分:0)

以下是那些将面临同样问题的人的代码。感谢TaynãBonaldo我解决了问题。

public class MainActivity extends ListActivity {
private static final String URL = "http://192.168.1.103/php-android/testphp.php"; 
private static final String TAG_DATA = "data";
private static final String TAG_ID = "name";       
private static final String TAG_DATE = "date";      
public PullToRefreshListView listView;  
public JSONArray data = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pull_to_refresh);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .detectAll().penaltyLog().build(); 
    StrictMode.setThreadPolicy(policy);
    listView = (PullToRefreshListView) getListView();
    listView.scrollTo(0, 60);
    // Set a listener to be invoked when the list should be refreshed.
    ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    });                     
    GetArrayData();
}
    private void GetArrayData() {
     // TODO Auto-generated method stub// Hashmap for listView
        ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();

        // creating JSON Parser instance
        JSONParser jParser = new JSONParser();  

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(URL);

        try {
            // Getting Array data 
            data = json.getJSONArray(TAG_DATA);

            for(int i = 0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);

                // Storing each jason item in variable
                String name = c.getString(TAG_ID);
                String date = c.getString(TAG_DATE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // Adding each child node to HASMap key => value
                map.put(TAG_ID, name);
                map.put(TAG_DATE, date);

                // adding HashList to Array list
                dataList.add(map);
            } 
        }catch (JSONException e) {
            e.printStackTrace();
        }
        // Updating parsed JASON data in to ListView
        ListAdapter adapter = new SimpleAdapter(this, dataList, R.layout.list_item, 
                new String[]{TAG_ID, TAG_DATE}, new int[]{
                R.id.name, R.id.date});
        // Set view to listView
        listView.setAdapter(adapter);
    }
    private class GetDataTask extends AsyncTask<Void, Void, String[]>{

        @Override
        protected String[] doInBackground(Void... params) {
            // TODO Auto-generated method stub
             try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    ;
                }
                return null;
        }

        @Override
        protected void onPostExecute(String[] result) {
            //
            listView.scrollTo(0,0);
            GetArrayData();
            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();

            super.onPostExecute(result);
            }   

  }
}