3

我正在处理来自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();
        }
    });     
}
4

2 回答 2

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 () 并且它工作正常。

于 2013-05-24T16:39:56.900 回答
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);
            }   

  }
}
于 2013-05-28T13:30:12.690 回答