ListView with custom ArrayAdapter - thread issue
<p>I'm trying to create a ListView with custom ArrayAdapter.
Following the example from here.
In the code below, I've created a ListActivity.
This list is getting its value from custom ArrayAdapter - OrderAdapter.
OrderAdapter is getting its elements from ArrayAdapter</p>

<p>I'm using runOnUiThread to fill the elements in adpater.
But inside this thread's run() method, the code is going into infinite loop. (See comments below).
Please check why it not getting out of the for loop.</p>

<p>Code:</p>

<pre><code>public class SoftwarePassionView extends ListActivity {

    private ProgressDialog m_progressDialog = null;
    private ArrayList&lt;Order&gt; m_orders = null;
    private OrderAdapter m_adapter; // Defined Below
    private Runnable viewOrders;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.softwarepassionview);
        m_orders = new ArrayList&lt;Order&gt;();


        viewOrders = new Runnable() {
            @Override
            public void run() {
                getOrders();
            }
        };

        Thread thread = new Thread(null, viewOrders, "MagnatoBackground");
        thread.start();
        m_progressDialog = ProgressDialog.show(SoftwarePassionView.this, "Please Wait",
                "Retriving data", true);

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

    private void getOrders() {
        try {
            m_orders = new ArrayList&lt;Order&gt;();
            Order o1 = new Order();
            o1.setOrderName("T-Shirt Purchase");
            o1.setOrderStatus("Dispatched");
            Order o2 = new Order();
            o2.setOrderName("Deo Purchase");
            o2.setOrderStatus("Pending");
            m_orders.add(o1);
            m_orders.add(o2);

            Thread.sleep(2000);
        } catch (Exception e) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
        }
        runOnUiThread(returnRes);
    }

    private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if (m_orders != null &amp;&amp; m_orders.size() &gt; 0) {
                m_adapter.notifyDataSetChanged();
                /*
                 * Going in an infinite loop below
                 */
                for (int i = 0; i &lt; m_orders.size(); i++)
                    m_adapter.add(m_orders.get(i));
            }
            m_progressDialog.dismiss();
            m_adapter.notifyDataSetChanged();
        }
    };

    private class OrderAdapter extends ArrayAdapter&lt;Order&gt; {

        private ArrayList&lt;Order&gt; items;

        public OrderAdapter(Context context, int textViewResourceId,
                ArrayList&lt;Order&gt; items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            super.getView(position, convertView, parent);
            View v = convertView;
            try {
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.row, null);
                }

                Order o = items.get(position);
                if (o != null) {
                    TextView orderName = (TextView) v
                            .findViewById(R.id.topText);
                    TextView orderStatus = (TextView) v
                            .findViewById(R.id.bottomText);
                    orderName.setText(o.getOrderName());
                    orderStatus.setText(o.getOrderStatus());
                }
            } catch (Exception e) {
                Toast.makeText(SoftwarePassionView.this, e.toString(),
                        Toast.LENGTH_SHORT).show();
            }
            return v;
        }
    }
}
</code></pre>

<p>Order.java</p>

<pre><code>public class Order {
    private String orderName;
    private String orderStatus;

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }
}
</code></pre>
