2012-02-27 103 views
1

下面是我的代碼,不會執行任何東西下doInBackground你們看到我錯了什麼嗎?,我只是添加物品稱爲「秩序」和顯示視圖。執行doInBackground(AsyncTask) - onCreate(Bundle savedInstanceState)

有幫助嗎?

private ArrayList<Order> m_orders = null; 
private OrderAdapter m_adapter; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 
    m_orders = new ArrayList<Order>(); 

    new taskDoSomething().execute(); 

    this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders); 
    setListAdapter(this.m_adapter); 
} 



private class taskDoSomething extends AsyncTask<Void, Void, Void> 
{ 

    @Override 
    protected Void doInBackground(Void... params) { 

     Log.i("LOGGER", "Starting...doInBackground loadList"); 

     m_orders = new ArrayList<Order>(); 

     Order o1 = new Order(); 
     o1.setOrderName("songs_array[0]"); 
     o1.setOrderStatus("Pending"); 
     o1.setQuantity(111); 
     m_orders.add(o1); 

     return (null); 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     Log.i("LOGGER", "...Done doInBackground loadList"); 
    } 
} 

回答

2

更好:

private class taskDoSomething extends AsyncTask<Void, Void, List<Order>> { 

@Override 
protected List<Order> doInBackground(Void... params) { 

    Log.i("LOGGER", "Starting...doInBackground loadList"); 

    List<Order> orders = new ArrayList<Order>(); 


    Order o1 = new Order(); 
    o1.setOrderName("songs_array[0]"); 
    o1.setOrderStatus("Pending"); 
    o1.setQuantity(111); 
    orders.add(o1); 

    return orders; 
} 

@Override 
protected void onPostExecute(List<Order> result) { 
    m_orders.clear(); 
    m_orders.addAll(result); 
    m_adapter.notifyDataSetChanged(); 
} 

}

1

你改變m_orders

你爲什麼初始化m_orders兩次當需要runOnUIThread()

+0

感謝小費1+ – 2012-02-27 20:06:48

1

我認爲你得到List null。請使用以下在postExecute(代碼)的AsyncTask的方法:

if(0!= m_orders.size()) 

this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders); 
    setListAdapter(this.m_adapter); 

刪除上述從onCreate()方法線....

+0

感謝提示1+ – 2012-02-27 20:06:43

相關問題