2012-10-20 90 views
0

所以我有一個問題在onPostExecute方法中填充listActivity。 SingleData的結果列表是正確的...然後我使用自定義適配器綁定到android.R.id.list ...Android asynctask on listview

但它給了我錯誤...我真的不明白爲什麼... 所以這裏的適配器的代碼..:

private class RequestAdapter extends ArrayAdapter<SingleData> { 

    private int _resource; 
    private LayoutInflater _inflater; 
    private Context _context; 

    public RequestAdapter(Context context, int textViewResourceId, 
      List<SingleData> objects) { 
     super(context, textViewResourceId, objects); 
     _resource = textViewResourceId; 
     _context = context; 
     _inflater = LayoutInflater.from(_context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     convertView = (ListView)_inflater.inflate(_resource,null); 
     SingleData sdd = getItem(position); 

     ImageView iv = (ImageView)convertView.findViewById(R.id.imagePlaceHolder_IV); 
     iv.setImageBitmap(sdd.thumbIMG); 

     TextView tv = (TextView)convertView.findViewById(R.id.createdAt_TV); 
     tv.setText(sdd.timeStamp); 

     // TODO Auto-generated method stub 
     return convertView;   
    } 

} 

這裏是onPostExecute:

private class DownloadTheImages extends AsyncTask<Void, Void, List<SingleData>> { 

    private Context _ctx; 

    public DownloadTheImages(Context ctx) { 
     _ctx = ctx; 
    } 

    protected void onPostExecute(List<SingleData> result) {    

     ListView mainLV = (ListView)findViewById(android.R.id.list); 
     mainLV.setAdapter(new RequestAdapter(_ctx, R.layout.single_row, result)); 

    } 

    protected List<SingleData> doInBackground(Void... params) { 
       ..... 
      .... 
       } 
     } 

和logcat的:

任何線索......感謝..

這些都是較新的錯誤:

10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.AbsListView.obtainView(AbsListView.java:1430) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.ListView.makeAndAddView(ListView.java:1745) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.ListView.fillDown(ListView.java:670) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.ListView.fillFromTop(ListView.java:727) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.ListView.layoutChildren(ListView.java:1598) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.AbsListView.onLayout(AbsListView.java:1260) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.view.View.layout(View.java:7175) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.view.View.layout(View.java:7175) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.view.View.layout(View.java:7175) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.view.View.layout(View.java:7175) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140) 
10-20 16:32:56.615: E/AndroidRuntime(1487):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 

回答

0

這些行導致此問題。

 RequestAdapter adapter; // This will be member variable 
    ListView mainLV = (ListView)findViewById(android.R.id.list); 
    mainLV.setAdapter(new RequestAdapter(_ctx, R.layout.single_row, result)); 

移動這些線中的onCreate()和實例適配器然後,在簡單地onPostExecute值添加到「結果」 ArrayList和調用adapter.notifyDatasetChanged()之前初始化的結果。避免在onPostExecute()或onProgressUpdate()中查找視圖。

+0

我會immeadetly嘗試... –

+0

現在它給我的錯誤,在setAdapter方法的開始......我想這是在格蘭適配器的getView becaseu沒有檢查,如果obecjt賴以做getItem爲空或空...任何建議如何檢查? –

+0

設置適配器之前,你需要初始化結果ArrayList是你在做什麼? –

0

我明白onPostExecute的工作方式是它接受從doInBackground()傳入的結果對象。因此,除非您將此結果返回到doinbackground,然後將其傳遞到您的RequestAdapter,否則您將獲得NullPointer。你在哪裏初始化你的結果成員?在OnCreate?

protected List<SingleData> doInBackground(Void... reuslt) { 
      ..... 
     .... 
     ///Do some stuff 

      return result; 
      } 
    } 


private class DownloadTheImages extends AsyncTask<Void, Void, List<SingleData>> { 

private Context _ctx; 

public DownloadTheImages(Context ctx) { 
    _ctx = ctx; 
} 
+0

我通過返回列表結束時DoinBackground。在調試部分中,斷點在調用onPostExecute內部的適配器之前給了我21個正確的對象,所以列表正確地傳遞給了onPostExecute ...我想.. –

+0

返回列表..這是在doInBackground的末尾。 –

+0

好吧,我有點困惑,因爲我沒有看到你的doInbackground的代碼。正如下面的人說,雖然我會誇大我的列表視圖,實例化和設置我的適配器onCreate而不是onPostExec ....任何理由,你爲什麼這樣做呢? –