0

Hey guys i am trying to iumplement those projects

https://github.com/erikwt/PullToRefresh-ListView
https://github.com/johannilsson/android-pulltorefresh
https://github.com/chrisbanes/Android-PullToRefresh

i have tried all three but i dont seem to be doing it correctly. can anybody help me??

My code is:

private class JsonReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setTitle(R.string.waiting);
            pDialog.setMessage(getString(R.string.get_stocks));
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            }

            catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            }
            catch (IOException e) {
                // e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }
        @Override
        protected void onPostExecute(String result) {
            ListDrwaer();
            pDialog.dismiss();
        }
    }// end async task
    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[] { url });
    }
    // build hash set for list view
    public void ListDrwaer() {
        List<Map<String, String>> stocksList = new ArrayList<Map<String, String>>();
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("name");
                String price = jsonChildNode.optString("price");
                stocksList.add(createStockList(name, price));
            }


        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        String[] from = { "name", "price"};
        int[] to = { R.id.stock_name, R.id.stock_price };
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, stocksList,
                R.layout.list_item,
                from, to);
        listView.setAdapter(simpleAdapter);
    }
    private HashMap<String, String> createStockList(String name, String price) {
        HashMap<String, String> stockNameNo = new HashMap<String, String>();
        stockNameNo.put("name", name);
        stockNameNo.put("price", price);
        return stockNameNo;
    }
}

and my layout file is

<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#9FA5A4"
    android:layout_weight="1"
    android:shape="rectangle"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:layout_alignParentStart="true"
        android:dividerHeight="2dp"
        android:divider="@drawable/list_grad"
        android:layout_alignParentEnd="true" >

    </ListView>

</LinearLayout>
/>

any help will be much appreaciated. i am new to this so please help me.

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60

2 Answers2

2

Perhaps you may want to try to follow this example.

1) Compile this library: compile 'com.android.support:support-v4:21.0.+'

2) Modify your layout like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolBar">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/startingList"
        android:scrollingCache="false"
        android:animationCache="false"
        android:smoothScrollbar="true"
        android:layout_below="@+id/toolBar" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

3) In your onCreate method add this code

SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
ListView mListView = (ListView) findViewById(R.id.activity_main_list_view);

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
          mSwipeRefreshLayout.setRefreshing(false);
  }
});

Hope it helps!!!

einverne
  • 6,454
  • 6
  • 45
  • 91
0

Perhaps a duplicate of How to implement Android Pull-to-Refresh

I've used Chris Banes' implementation many times and his samples are very clear. The layout you posted isn't wrapped in a

<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout>
Community
  • 1
  • 1
  • You mean something like this???but i dont know how to add the java code in my code..can u help me???i want to accesswebservice when the list is going to refresh – Kostas Drak Mar 13 '14 at 18:55