BaseAdapter notifyDatasetChanged()调用,但getView()不会被调用 [英] BaseAdapter notifyDatasetChanged() called but getView() is never called

查看:151
本文介绍了BaseAdapter notifyDatasetChanged()调用,但getView()不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可视化的订单列表中的每个行的自定义适配器。

I have a custom adapter that visualize each row in the list of Orders.

public class OrderRowAdapter extends BaseAdapter implements OnClickListener {
    OrderList items_;
    LayoutInflater inflater_;
    int list_view_resource_id_;
    private final String TAG = "OrderRowAdapter";

    public OrderRowAdapter(Context context, int list_view_resource_id,
            OrderList items) {
        this.list_view_resource_id_ = list_view_resource_id;
        this.items_ = items;
        this.inflater_ = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public Object getItem(int position) {
        return items_.getOrders(position);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(TAG, "View updated for item in position = " + position);

        View v = convertView;
        if (v == null) {
            v = inflater_.inflate(list_view_resource_id_, parent);
        }

        Order item = items_.getOrders(position);
        if (item != null) {
            TextView order_info_tv = (TextView) v.findViewById(R.id.order_info);
            TextView order_status_tv = (TextView) v.findViewById(R.id.order_status);

            if (order_info_tv != null) {
                order_info_tv.setText(
                        String.format("For customer: %s\nTotal of %d items", item.getCustomerId(), item.getItemsCount()));
            }
            if (order_status_tv != null) {
                order_status_tv.setText("Status: " + getStatusText(item.getStatus()));
            }
        }
        return v;
    }

    public int getCount() {
        if (items_ == null) {
            Log.d(TAG, "Null so get count returned 0");
            return 0;
        } else {
            Log.d(TAG, "Get count returned " + items_.getOrdersCount());
            return items_.getOrdersCount();
        }
    };

查询订单新的列表,从一个Web服务之后,我想更新ListView控件的内容,所以我有我的活动做的更新之前调用notifyDataSetChanged()

After querying new list of orders from a web service, I want to update the content of the ListView, so I have my Activity does the updating before calling notifyDataSetChanged()

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

    initThreading();
    findViews();
    setUrls();

    // Load the list of order from disk
    try {
        order_list_ = OrderList.parseFrom(new FileInputStream(
                "/sdcard/orderList.bin"));
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Cannot find the file", e);
    } catch (IOException e) {
        Log.e(TAG, "Cannot read the file", e);
    }

    order_row_adapter_ = new OrderRowAdapter(OrderActivity.this,
            R.layout.order_row, order_list_);
    orders_listview_.setAdapter(order_row_adapter_);

    // Request new updates from the server
    updateOrderInformation(-1);
}

public void updateOrders(InputStream new_order_stream) {
    Log.d(TAG, "Updating order UI");
    try {
        order_list_.parseFrom(new_order_stream);
    } catch (IOException e) {
        Log.e(TAG, "IOException" , e);
    }

    runOnUiThread(new Runnable() {
        public void run() {
            guiUpdateOrders();
        }
    });
}

private void guiUpdateOrders() {
    order_row_adapter_.notifyDataSetChanged();
    Log.d(TAG, "Dataset notified that it has changed. GUI update anytime now.");
}

不过,OrderRowAdapter的getView()方法不会被调用。 ListView控件是永远不会更新。

But, the getView() method of OrderRowAdapter is never called. The ListView is never updated.

推荐答案

原来的问题,我的getView()不会被调用,是因为它是不可见的。我的布局XML与FILL_PARENT其高度上的TextView。因此,整个视图只具有单一的TextView可见。

It turns out the problem with my getView() not being called is because it is not visible. My layout xml has the upper TextView with fill_parent for its height. Thus the entire view only has that single TextView visible.

解决方案就是如此:检查有问题的布局的图形视图,以确保在ListView可见

Solution is thus: check the graphical view of the layout in question to make sure the ListView is visible.

这篇关于BaseAdapter notifyDatasetChanged()调用,但getView()不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆