在onCreate之外创建时,arrayadapter错误

时间:2012-07-22 03:13:49

标签: android listview android-arrayadapter

我有一个ArrayAdapter用于填充listview,但我无法在oncreate事件之外创建它,但那时我没有数据。

public class CusPickup extends Activity {

private OrdersReady orderready_data[];
private ListView lView; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.orders);

这里我将得到一个零点异常。

    OrderReadyAdapter adapter = new OrderReadyAdapter(this,R.layout.listview_item_row, orderready_data);
    lView = (ListView)findViewById(R.id.listView1);
    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    lView.addHeaderView(header);
    lView.setAdapter(adapter);

getData();
}
}

这里我从HTTP获取数据。

private final Handler handler = new Handler() {

        @Override
        public void handleMessage(final Message msg) {
            progressDialog.dismiss();
            String bundleResult = msg.getData().getString("RESPONSE");
                int TotalRecords = myResult.d.results.size();
                for (int i = x; i < TotalRecords; i++ ) {
                    orderready_data[i] = new OrdersReady(myResult.d.results.get(i).myStr, myDate ,invResult.d.results.get(i).numberStr, invResult.d.results.get(i).qtyInt, myAmount)
                }  
           }
}

如果我放置OrderReadyAdapter,我会得到一个代码错误修复“将OrderReadyAdapter(Context,Int,OrdersReady [])更改为OrderReadyAdapter(Handle,Int,OrdersReady []),如果我更改它,我将收到更多错误。

另外我不确定我的私有OrdersReady orderready_data []的声明是否正确,因为如果我在代码中声明它,我会声明它:OrdersReady orderready_data [] = new OrdersReady [TotalRecords];

感谢您的帮助。


新适配器

public class OrderReadyAdapter extends ArrayAdapter<OrdersReady>{

Context context; 
int layoutResourceId;    
ArrayList<OrdersReady> data = null;

public OrderReadyAdapter(Context context, int layoutResourceId, ArrayList<OrdersReady> orderReadyArray) {
    super(context, layoutResourceId, orderReadyArray);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = orderReadyArray;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    OrderHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new OrderHolder();
        holder.mytxt1 = (TextView)row.findViewById(R.id.mytxt1);
        holder.mytxt2 = (TextView)row.findViewById(R.id.mytxt2);
        holder.mytxt3 = (TextView)row.findViewById(R.id.mytxt3);
        holder.mytxt4 = (TextView)row.findViewById(R.id.mytxt4);
        holder.mytxt5 = (TextView)row.findViewById(R.id.mytxt5);

        row.setTag(holder);
    }
    else
    {
        holder = (OrderHolder)row.getTag();
    }

    OrdersReady orderready = data.get(position);
    holder.mytxt1.setText(orderready.place);
    holder.mytxt2.setText(orderready.Date);
    holder.mytxt3.setText(orderready.invoice);
    holder.mytxt4.setText(String.valueOf(orderready.Qty));
    holder.mytxt5.setText(String.valueOf(orderready.Amount));

    return row;
}

static class OrderHolder
{
    TextView mytxt1;
    TextView mytxt2;
    TextView mytxt3;
    TextView mytxt4;
    TextView mytxt5;
}

}

1 个答案:

答案 0 :(得分:2)

我建议你将OrdersReady []更改为ArrayList。在onCreate方法中初始化它。同时将orderReady适配器变为类字段。

orderReadyArray = new ArrayList<OrderReady>();
ordersReadyAdapter = new OrderReadyAdapter(this,R.layout.listview_item_row, orderReadyArray);
lView = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
lView.addHeaderView(header);
lView.setAdapter(ordersReadyAdapter);

这应该初始化一个空的列表视图,因为你还没有数据。

当您从服务器收到OrdersReady数据时,请更新orderReadyArray:

orderReadyArray.clear(); // remove old data
for (int i = x; i < TotalRecords; i++ ) {
   orderReadyArray.add(data); // add new data one by one
}
ordersReadyAdapter.notifyDataSetChanged(); // this forces the listview to repaint

可替换地:

您可以创建新的适配器,并在收到数据后将其分配给列表视图:

List<OrderReady> orderReadyArray = new ArrayList<OrderReady>(); // create a new array to hold data
for (int i = x; i < TotalRecords; i++ ) {
   orderReadyArray.add(data); // add new data one by one
}
OrderReadyAdapter ordersReadyAdapter = new OrderReadyAdapter(this,R.layout.listview_item_row, orderReadyArray);
lView.setAdapter(ordersReadyAdapter);

这应该更新您的列表。如果您仍然没有看到这些项目,则问题出在适配器中,可能是您在getView()方法中错误地对行进行了充气。