Hello Guys,
I should write in the title instead of 'doesn't work' something like 'I don't know how to do it' but the first version feels better :). What I am trying to do is the following:
- Download the xml from the web, parse it and create ArrayList of some objects (done and working)
- Display the objects using custom Adapter (doesn't work)
The second one works if I add the items to my ArrayList before I add it to the view using
m_orderAdapter = new OrderAdapter(this,m_orders); //code for orderadapter below setListAdapter(m_orderAdapter);
I have found on the web something like this: (in my onCreate method)
handler = new Handler(); viewOrders = new Runnable(){
@Override
public void run() {
getOrders();
}
};
new Thread(){
@Override
public void run(){
handler.post(viewOrders);
}
}.start();
then, the following code for the methods:
private void getOrders(){ try{ OrderManager om = new OrderManager(); m_orders = om.getOrdersFromWeb(); Log.i("ARRAY", ""+ m_orders.size()); } catch (Exception e) { Log.e("BACKGROUND_PROC", e.getMessage()); } runOnUiThread(returnRes); }
OrderManager downloads and parse the xml into Order objects and returns array list of those. Then I set this list to my member array list m_orders. Once downloading and parsing is done I run returnRes method on the ui thread using runOnUiThread method
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
Log.i("ORDER",m_orders.get(0).getOrder_id());
setListAdapter(m_orderAdapter);
m_orderAdapter.notifyDataSetChanged();
}
m_orderAdapter.notifyDataSetChanged();
}
};
and I call notifyDataSetChanged() on my adapter.
The view I do all this stuff extends ListView and the code for the adapter itself is listed below:
public class OrderAdapter extends BaseAdapter{
private Context ctx;
private List<Order> orders;
public OrderAdapter(Context ctx, List<Order> orderLst){
this.ctx = ctx;
this.orders = orderLst;
}
@Override
public int getCount() {
return orders.size();
}
@Override
public Object getItem(int pos) {
return orders.get(pos);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Order o = orders.get(position);
return new OrderListAdapterView(this.ctx,o);
}
}
When I debug I have the data inside my m_orders list but when I call notifyDataSetChanged nothing happens, I've read that I have to execute that on the UI thread which I think I do. So whats the problem? any help highly appreciated, or maybe just a link to the nice tutorial on the web explaining this issue on how to update the list view at runtime?
Thanks