2014-02-08 29 views
1

在我的應用程序中,我正在顯示從互聯網下載的項目列表(類型爲pItem)。我使用的擴展ArrayAdapter<pItem>定製ArrayAdapter這基本上是這樣的:創建自定義ArrayAdapter時創建ArrayAdapter後設置項目?

public class PArrayAdapter extends ArrayAdapter<pItem> { 

private List<pItem> pList = new ArrayList<pItem>(); 

public PArrayAdapter(Context context) { 
    super(context, R.layout.p_row); 
    this.context = context; 
} 


public void SetList(List<PItem> pl){ 
    pList = pl; 
} 

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

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View rowView = convertView; 

    if (rowView == null) { 
     LayoutInflater vi; 
     vi = LayoutInflater.from(getContext()); 
     rowView = vi.inflate(R.layout.p_row, null); 
    } 

    // ... 

    return rowView; 
} 

} 

現在,通常情況下,項目的列表在構造函數中設置。但在我的情況下,當我動態獲取AsyncTask中的項目(該列表不斷變化)時,我將項目的設置與創建的適配器分開。

在我的主要活動,這是我做的:(只有代碼的主要部分)

public class MainPageActivity extends SherlockFragmentActivity implements 
OnScrollListener { 

// List of items 
private List<pItem> pList = new ArrayList<pItem>(); 

// Adapter 
private arrayAdapter pAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_page); 

    //... 

    listView = (ListView) findViewById(R.id.p_list); 

    adpater = new PArrayAdapter(getApplicationContext()); 

    new ASTask().execute(""); 
} 

private class ASTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... args) { 

     // Add the downloaded items to list 
     pList.addAll(Downloader.GetItems()); 

     return ""; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     Log.d("Main", String.valueOf(pList.size())); // Shows the right number 

     // Show Items 
     pAdapter.setList(pList); 
     listView.setAdapter(pAdapter); 

     Handler mHandler = new Handler(Looper.getMainLooper()); 

     Runnable rn = new Runnable() { 
      @Override 
      public void run() { 
       pAdapter.notifyDataSetChanged(); 
      } 
     }; 

     mHandler.post(rn); 
    } 

    @Override 
    protected void onPreExecute() {} 

    @Override 
    protected void onProgressUpdate(Void... values) {} 

} 


} 

這裏有一個問題:當我運行它,沒有什麼是在ListView所示。它不會給出任何錯誤。

但是,如果我在PArrayAdapter的構造函數中設置列表,一切工作正常,我看到的項目。

我在這裏做錯了什麼?

+0

您的數據來自用戶?我的意思是滾動時數據進入插槽或10或20。 –

+0

@MehulRanpara,當調用'GetItems()'時,它的常數(5)來了。 – Mahm00d

回答

0

確定。我發現我的問題是什麼。它實際上是一個新手的錯誤:

而不是分配的輸入列表到列表裏面適配器:

pList = pl; 

我應該有添加所有的項目是這樣的:

pList.addAll(pl); 

這解決了問題。

0

在onPostExecute()方法的末尾添加 pAdapter.notifyDataSetChanged();

嘗試這樣的:

@Override 
    protected void onPostExecute(String result) { 
     Log.d("Main", String.valueOf(pList.size())); // Shows the right number 

     // Show Items 
     pAdapter.setList(pList); 
     listView.setAdapter(pAdapter); 
     pAdapter.notifyDataSetChanged();  
    } 
+0

我擁有它。你的意思是將它移動到可運行的最後和最後? – Mahm00d

+0

更新了我的回答 – localhost

+0

仍然一樣。 – Mahm00d

0

在onPostExecute

刪除此代碼

Handler mHandler = new Handler(Looper.getMainLooper()); 

    Runnable rn = new Runnable() { 
     @Override 
     public void run() { 
      pAdapter.notifyDataSetChanged(); 
     } 
    }; 

    mHandler.post(rn); 

添加此行只

pAdapter.notifyDataSetChanged(); 
+0

它沒有任何區別。 – Mahm00d

相關問題