2014-03-13 32 views
0

嗨我試圖iumplement如何創建一個拉來刷新到一個ListView

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

我曾嘗試所有這三個項目,但我不似乎是正確的練習。有誰能夠幫助我??

我的代碼是:

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

和我的佈局文件是

<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> 
/> 

任何幫助將非常appreaciated。我對此很陌生,所以請幫助我。

回答

1

也許您可能想嘗試關注此example

1)編譯這個庫:compile 'com.android.support:support-v4:21.0.+'

2)修改您的佈局,如:

<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)在您的onCreate方法中添加以下代碼

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

希望它可以幫助! !

0

也許How to implement Android Pull-to-Refresh

我用Chris Banes'執行多次,他的樣本重複是很清楚的。您發佈的版面不包含在

<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout> 
+0

你的意思是這樣的???但我不知道如何在我的代碼中添加java代碼..你可以幫我嗎???我想accesswebservice當列表要刷新 –

相關問題