在下面的代码中,我正在尝试创建一个ListActivity
. 此列表的元素由自定义ArrayAdapter
- OrderAdapter 填充。
所有元素获取机制都在 AsyncTask 类中。我可以在 中设置元素m_adapter
,但无法更新 List 的 UI。
活动正在强制关闭。
代码:
public class SoftwarePassionView extends ListActivity {
private ProgressDialog m_progressDialog = null;
private ArrayList<Order> m_orders = null;
private OrderAdapter m_adapter; // Defined Below
private Runnable viewOrders;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.softwarepassionview);
m_adapter = new OrderAdapter(SoftwarePassionView.this,
R.layout.row, m_orders);
new AsyncOperation().execute();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
public class OrderAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> items;
public OrderAdapter(Context context, int textViewResourceId,
ArrayList<Order> elements) {
super(context, textViewResourceId, elements);
this.items = elements;
}
public void setList(ArrayList<Order> items) {
this.items = new ArrayList<Order>();
this.items.addAll(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;
}
}
public class AsyncOperation extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... params) {
getOrders();
if (m_orders != null && m_orders.size() > 0) {
m_adapter.setList(m_orders);
}
return null;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
m_adapter.notifyDataSetChanged();
setListAdapter(m_adapter);
}
}
private void getOrders() {
try {
m_orders = new ArrayList<Order>();
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();
}
}
}