开发者

Android listview item doesn't appear

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:

  1. Download the xml from the web, parse it and create ArrayList of some objects (done and working)
  2. 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.getOrder开发者_JS百科sFromWeb();
           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 ?


Hey Arutha - why don't you see my answer for this post - I think it is what you need.

Or just let me repost it here

You can extend ArrayAdapter. Here's code example for you. In this example - SearchItem is some custom POJO. Basically you need to override getView() method to build your row by inflating row layout and then populating values based on List of items and current position

class SearchItemsAdapter extends ArrayAdapter<SearchItem> {
Activity context;
List<SearchItem> items;
SearchHeader header;

@SuppressWarnings("unchecked")
public SearchItemsAdapter(final Activity context,
        final Map<SearchHeader, List<SearchItem>> result) {
    super(context, R.layout.item, (List) ((Object[]) result.values()
            .toArray())[0]);
    this.context = context;
    this.header = result.keySet().iterator().next();
    this.items = result.get(this.header);
}

@Override
public View getView(final int position, final View convertView,
        final ViewGroup parent) {
    final View view = this.context.getLayoutInflater().inflate(
            R.layout.item, null);
    final SearchItem item = this.items.get(position);
    ((TextView) view.findViewById(R.id.jt)).setText(item.jt);
    ((TextView) view.findViewById(R.id.dp)).setText(item.dp);
    ((TextView) view.findViewById(R.id.cn)).setText(item.cn);
    ((TextView) view.findViewById(R.id.loc)).setText(item.loc.name);
    final TextView body = ((TextView) view.findViewById(R.id.e));
    body.setText(item.e);
    body.setTag(item.src[0]);
    ((TextView) view.findViewById(R.id.src)).setText(item.src[1]);
    return view;
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜