2014-03-28 86 views
-1

我有一個asycn任務被稱爲有:的Android的ListView不被填充

GetListsJSON task = new GetListsJSON(getActivity()); 
     task.setOnArticleSelectedListener(this); 
     task.execute(url); 

它,然後運行這個異步任務來檢索JSON,並填寫列表視圖:

public class GetListsJSON extends AsyncTask<String, Void, String> { 

    Context c; 
    private ProgressDialog Dialog; 

    public GetListsJSON (Context context) 
    { 
     c = context; 
     Dialog = new ProgressDialog(c); 
    } 

    //****************code for on click 
    OnArticleSelectedListener listenerBeer; 
    public interface OnArticleSelectedListener{ 
     public void onArticleSelected(String myString, String brewery); 

    } 
    public void setOnArticleSelectedListener(OnArticleSelectedListener listener){ 
     this.listenerBeer = listener; 


    } 
    //******************end code for onClick 

    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     return readJSONFeed(arg0[0]); 
    } 

    protected void onPreExecute() { 
     Dialog.setMessage("Getting Lists"); 

     Dialog.setTitle("Loading"); 
     Dialog.setCancelable(false); 
     Dialog.show(); 
    } 

    protected void onPostExecute(String result){ 
     //decode json here 
     try{ 

      JSONArray jsonArray = new JSONArray(result); 

      final List<BeerData> beerList = new ArrayList<BeerData>(); 

      //acces listview 
      final ListView lv = (ListView) ((Activity) c).findViewById(R.id.allYourBeersList); 

      Log.d("list", "inside async"); 

      //make array list for beer 

      //tasteList.add(""); 
      for(int i = 0; i < jsonArray.length(); i++) { 

       String bID = jsonArray.getJSONObject(i).getString("id"); 
       String listName = jsonArray.getJSONObject(i).getString("name"); 

       Log.d("list", listName); 

       //create beer object 
       BeerData thisBeer = new BeerData(listName, bID, "a", "a", "a", "a", 
         "a","a", "a", "a", "a", "breweryID", "breweryName", 
         "a", "a", "a","a", "a", "a"); 

       //add beer to list 
       beerList.add(thisBeer); 


      } 

      //update listview 
      BeerSearchAdapter adapter1 = new BeerSearchAdapter(c ,R.layout.listview_item_row, beerList); 
      lv.setAdapter(adapter1); 

      //set up clicks 
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, 
             int arg2, long arg3) { 

        BeerData beerInfo = beerList.get(arg2); 
        String idToSend = beerInfo.beerId; 

        //************************Launch listener interface 
        listenerBeer.onArticleSelected(idToSend, beerInfo.beerBreweryId); 




       } 
      }); 




     } 
     catch(Exception e){ 

     } 

     Dialog.dismiss(); 

    } 





    public String readJSONFeed(String URL) { 
     StringBuilder stringBuilder = new StringBuilder(); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(URL); 
     try { 
      HttpResponse response = httpClient.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
       HttpEntity entity = response.getEntity(); 
       InputStream inputStream = entity.getContent(); 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(inputStream)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        stringBuilder.append(line); 
       } 
       inputStream.close(); 
      } else { 
       Log.d("JSON", "Failed to download file"); 
      } 
     } catch (Exception e) { 
      Log.d("readJSONFeed", e.getLocalizedMessage()); 
     } 
     return stringBuilder.toString(); 
    } 

} 

我知道JSON解析工作,因爲我的日誌吐了出來:

03-28 16:04:27.175 30167-30167/com.beerportfolio.beerportfoliopro D/list﹕ inside async 
03-28 16:04:27.175 30167-30167/com.beerportfolio.beerportfoliopro D/list﹕ My Fridge 
03-28 16:04:27.175 30167-30167/com.beerportfolio.beerportfoliopro D/list﹕ Wish List 

頁面的列表應該加載XML ED成是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:id="@+id/linlaHeaderProgress" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      android:id="@+id/pbHeaderProgress" 
      style="[email protected]/Spinner" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 
     </ProgressBar> 
    </LinearLayout> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:dividerHeight="0px" 
     android:divider="@null" 
     > 
    </ListView> 

</LinearLayout> 
+0

'catch(Exception e){ }'!。你的代碼崩潰了,但你不知道在哪裏,因爲你忽略了這個異常。 – njzk2

+0

(如果你記錄異常,你會發現錯誤是微不足道的) – njzk2

+0

我應該在哪裏放置catch? – Mike

回答

1

你有這樣一行:

final ListView lv = (ListView) ((Activity) c).findViewById(R.id.allYourBeersList); 

但在你的XML文件,你這個ID定義你的ListView:@android:ID /列表

你應該在您的xml文件中將其切換到以下內容:

@+id/allYourBeersList 
+0

謝謝,真的很感激。如果你的編碼很長,你會感到疲倦,錯過了小事情,這會讓你感到厭煩。 – Mike