下面是我的代码,在doInBackground
下没有执行任何操作你们看到我做错了吗?我只是在名为“Order”的对象中添加项目并显示视图。
任何帮助?
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");
}
}
答案 0 :(得分: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 :(得分:1)
更改m_orders时需要runOnUIThread()
为什么要两次初始化m_orders?
答案 2 :(得分:1)
我认为你得到List null。请在AsyncTask的postExecute()方法中使用以下代码:
if(0!= m_orders.size())
this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
setListAdapter(this.m_adapter);
从onCreate()方法中删除上面的行......