当前位置: 首页 > 面试题库 >

调用BaseAdapter notifyDatasetChanged()但从不调用getView()

柯新翰
2023-03-14
问题内容

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

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的内容,因此我让Activity在调用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永远不会更新。


问题答案:

原来我getView()不被叫的问题是因为它不可见。我的布局XML已经上TextViewfill_parent它的高度。因此,整个视图只有一个TextView可见的。

解决方案: 检查相关布局的图形视图,以确保ListView可见。



 类似资料:
  • 当我在AWS平衡器下部署我的应用程序时,应用程序会从myDir/myAppName调用一些资源。但是样式资源是从根调用的,比如myDir/。 例如myDir/myAppName/index.html可用于loadBalancer.但是myDir/myStilesheet.css不可访问。 如何在myDir/myAppName下设置这些资源(css等)? 提前谢谢!

  • 问题内容: 我有一个自定义我在我农具。 问题是该方法从未调用过。我想我需要在我的主适配器而不是自定义适配器中实现它。 MyListViewAdapter.java : 我对吗? 问题答案: 为什么在适配器内部会有OnItemClickListener?您可能在Adapter内有OnClickListener,或者最佳实践是将OnItemClickListener设置为ListView或您在活动/片

  • 我有一个问题 特征通知,我关注Android的文档并在许多堆栈溢出帖子中搜索。我所有的代码都有效,直到我尝试从我的设备获取数据,当我订阅通知时,我得到这个日志: 蓝牙Gatt:setCharacterticNotification() 但我的方法从未被调用 我的UUID很好,我的服务发送的数据我已经用其他应用程序检查过了。我为我的应用程序启用位置。 我在清单中拥有此权限: uses-permiss

  • 我创建了一个节点lambda函数,它对Aurora数据库进行简单调用。当我在控制台中测试该函数时,查询返回,我可以在日志中看到结果,但回调似乎永远不会被调用,所以我的lambda函数超时了。我无法找出问题所在。希望这里有人能指出我的问题。 生成的Cloudwatch日志如下所示。。。

  • 任何人都有,当在自身上调用时,它只调用,而不调用和。但这种活动在设备上是看不见的吗? 我没有看到任何其他错误日志。 根据android活动生命周期,如果活动是不可见的,那么它应该调用。但事实并非如此。它发生在Android 5.0上。 我找到了原因,但我不明白为什么会发生。 扩展碎片活动,定义为启动模式:singleTask。 B扩展了Activity,它在Manifest中定义了android:

  • 所以看起来是正确的?我也试着测试“日期”,这也不起作用。 代码本身是这样的(部分):