gpt4 book ai didi

android - 在 onCreate 之外创建时出现 arrayadapter 错误

转载 作者:行者123 更新时间:2023-11-29 22:02:31 25 4
gpt4 key购买 nike

我有一个用于填充 ListView 的 ArrayAdapter,但我无法在 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 get 获取数据。

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[]) 如果我更改它,我将收到更多错误。

我也不确定我对私有(private) 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;
}

最佳答案

我建议您将 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);

这应该会初始化一个空 ListView ,因为您还没有数据。

当您从服务器接收到 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

或者:

您可以创建一个新的适配器,并在收到数据后将其分配给 ListView :

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() 方法中错误地膨胀了行。

关于android - 在 onCreate 之外创建时出现 arrayadapter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11597525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com