3

iam dealing with well known pull to refresh example from Git-hub

I loaded library and everything is working as it should but when i want to call method setOnRereshListener, Eclipse don't find it. What could be a problem?

This is code from example:

        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();
        }
    });

And this is my code:

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();
        }
    });     
}
Miha GaPiha
  • 35
  • 1
  • 9

2 Answers2

3

Not sure, but I believe that is due to the method signature OnRefresh (PullToRefreshBase refreshView), try removing PullToRefreshBase refreshView, like this:

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

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

I use this same structure without signing method OnRefresh () and it works normally.

Taynã Bonaldo
  • 621
  • 9
  • 17
  • Now i changed from (pullToRefreshView in to listView.setOnR...) and i also try your code but it doesn't find that method. – Miha GaPiha May 24 '13 at 17:12
  • You are using PullToRefreshListView within an Activity or within a ListActivity? – Taynã Bonaldo May 24 '13 at 17:14
  • 1
    Have you checked if the imports are doing correctly? import com.yourpackage.yourwidgets.PullToRefreshListView; import com.yourpackage.yourwidgets.PullToRefreshListView.OnRefreshListener; – Taynã Bonaldo May 24 '13 at 17:17
  • Iam using it in in Activity not in ListActivity. – Miha GaPiha May 24 '13 at 17:17
  • I have those imports. Iam running it in Activity because i think you can't run it in ListActivity because you can't use id:list. – Miha GaPiha May 24 '13 at 17:23
  • Exactly, I say bullshit! I had another idea, try removing of the stretch "new OnRefreshListener ()", after, remove PullToRefreshBase refreshView from onRefresh() signature. – Taynã Bonaldo May 24 '13 at 17:28
  • Thank you now it is working, do you maybe know what i need to do now in new class GetDataTask ? – Miha GaPiha May 24 '13 at 17:40
  • In this method, you should put the code that will be responsible for updating the list. You basically should handle your List and update your adapter to update ListView. GetDataTask is an implementation of AsyncTask, use doInBackground to update the data and OnPostExecute () to update your adapter and you PullToRefreshListView. – Taynã Bonaldo May 24 '13 at 18:06
0

Here is the code for those who will face with the same problem as i did.Thanks to Taynã Bonaldo i made it through problems.

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);
            }   

  }
}
Miha GaPiha
  • 35
  • 1
  • 9