- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对这个适配器元素(NotifyDataSetChanged)有点困惑,因为当我使用它时,我的 ListView 没有更新。
我的工作场景:首先,我创建了一个 void,这样每当我更新 listview 项目上的某些内容时,我就可以再次调用我的 void。
private void bindOrderListSorted(String sort){
orderList = new ArrayList<Order>();
mySQLiteAdapter = new SQLiteAdapter(context);
mySQLiteAdapter.openToRead();
String selectQuery = "MY QUERY...";
Cursor cursor =mySQLiteAdapter.read(selectQuery);
while(cursor.moveToNext())
{
Order order = new Order();
order.orderdDesc = cursor.getString(0);
order.orderitemCode = cursor.getString(1);
order.orderitemID = cursor.getString(2);
order.orderPrice = cursor.getString(3);
order.qtyOrdered = cursor.getString(4);
order.stocksQty = cursor.getString(5);
orderList.add(order);
}
cursor.close();
mySQLiteAdapter.close();
orderAdapter =new OrderListAdapter(this, orderList,context, sort);
listViewSearchPanel.setAdapter(orderAdapter);
}
主要问题是,每当我更新数据库上的某些内容并调用“bindOrderListSorted”时,我的 ListView 就会更新,但正如预期的那样,它会再次重新绑定(bind), ListView 的位置再次变为零。
这是我之前的逻辑,但后来我发现,如果我有一长串项目怎么办?它不合适且用户友好,因为用户必须再次向下滚动才能找到他/她希望更新的项目
所以我了解了 NotifyDataSetChanged 并为此再次创建了一个 void
private void NotifyDataChangedOrderListSorted(String sort){
orderList = new ArrayList<Order>();
mySQLiteAdapter = new SQLiteAdapter(context);
mySQLiteAdapter.openToRead();
String selectQuery = "MY QUERY... SAME AS BEFORE";
Cursor cursor =mySQLiteAdapter.read(selectQuery);
while(cursor.moveToNext())
{
Order order = new Order();
order.orderdDesc = cursor.getString(0);
order.orderitemCode = cursor.getString(1);
order.orderitemID = cursor.getString(2);
order.orderPrice = cursor.getString(3);
order.qtyOrdered = cursor.getString(4);
order.stocksQty = cursor.getString(5);
orderList.add(order);
}
cursor.close();
mySQLiteAdapter.close();
orderAdapter =new OrderListAdapter(this, orderList,context, sort);
orderAdapter.notifyDataSetChanged();
}
每当我更新某些内容时,都将其称为“NotifyDataChangedOrderListSorted”。
我的主要问题是我的 ListView 没有更新。我非常确定我的 orderList 具有更新的值,因为使用调试器和断点我期望的数据符合预期。这是为什么?
欢迎大家提出建议或意见。如果您想查看我的基本适配器,请这么说..
提前致谢
最佳答案
您正在创建一个新的 List 和 ArrayAdapter,因此 notifyDataSetChanged()
不起作用,并且 orderAdapter
不再引用 ListView 中的适配器。您有两个选择:
在 NotifyDataChangedOrderListSorted()
方法中调用 setAdapter()
:
...
orderAdapter =new OrderListAdapter(this, orderList,context, sort);
listViewSearchPanel.setAdapter(orderAdapter);
但这与bindOrderListSorted()
重用orderList
和orderAdapter
:
private void NotifyDataChangedOrderListSorted(String sort){
orderList.clear(); // Change this
mySQLiteAdapter = new SQLiteAdapter(context);
mySQLiteAdapter.openToRead();
...
cursor.close();
mySQLiteAdapter.close();
orderAdapter.notifyDataSetChanged(); // Change this
}
但是确实您应该使用 CursorAdapter,因为它们更快、更小......但这取决于您。
关于java - Android NotifyDataSetChanged 和 BaseAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131982/
好的,我一直在仔细搜索,但我在实现 BaseAdapter 时遇到了一些问题。 我已经能够实现一个简单的游标适配器 http://developer.android.com/resources/sam
我将使用下面的代码从服务器(URL)加载适配器的图像。它适用于新型移动设备。但是,旧模型会崩溃并返回“java.lang.OutOfMemoryError”。它将把 A 行或 B 行标记为错误。如何避
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我想在 ListView 中显示具有多列的数组。 调用 View.getView() 时出现此错误: android.widget.linerlayout cannot be cast to andr
嗨,我已经扩展了 BaseAdapter,我的列表大小是 20,但它只显示了 7 条记录,在第 7 条记录之后它又从 0 开始显示。 公共(public)类 ContactsAdapter 扩展 Ba
大家 最近在用ADT开发android 2.2的程序。程序在模拟器上运行正常,但在真机上运行却遇到了一个奇怪的问题。 我使用 BaseAdapter 创建一个 MenuAdapter,它将加载名为 V
这是我的自定义 BaseAdapter 的构造函数 public MyAdapterAds(Activity activity, BaseAdapter delegate) { this.ac
我有一个像这样的 JSON 数组:[{"usename":"user", "user_pic":"picture", "photo":"http://hfhfhfhhfh.jpg", "timesta
嗨,我是这个机器人的新手。我只是对基本适配器感到困惑。我的问题是考虑在一个数组中我有 10 个项目(从 0 到 9)。在 baseadapter 的 getview() 选项中,我在 textview
有没有办法通过 getApplicationContext() 获取 BaseAdapter 类中的 context? 我需要这个,因为我要从 url 加载图像。我在这里使用 context: 这是在
我有一个 ListView,它扩展了 BaseAdapter。我有一个数据 [] 数组。 ListView 正确膨胀和填充。我想要做的是在用户选择项目时以及是否选择了上一个项目时,使 ImageVie
无论如何,我正在努力制作的应用程序都有一个简单的列表和一个位于底部的按钮。我的问题是我的自定义 BaseAdapter 不显示元素。我知道,因为我的元素只是一个字符串,所以我可以使用 ArrayAda
我正在尝试将一个整数数组传递给一个 baseadapter,这样 A.class 就会将一个整数数组传递给 B.class 中的 BaseAdapter。以下是我在 A.Class(发件人)中传递整数
我在运行刚从 ADT(Eclipse) 转换而来的 android 项目时遇到此错误。 找到一个话题 here但没有回答。谁有解决办法? 最佳答案 这是解决方法。 类 XXXAdapter 扩展了 a
我正在使用 Android Studio 创建 Android 应用程序。我在使用自定义 SimpleAdapter 的 Activity 中有 ListView 。我需要在自定义适配器中使用自定义字
我使用了 ListView 。我有 5 件元素。当我启动程序时,我只看到 3. 处理程序,用于进度条开始处理 3/5 项目。我去看 4,5 项,所以我从 View 1,2 项中丢失了。 4 和 5 项
我有一个自定义BaseAdapter对于我的ListView我在其中实现 AdapterView.OnItemClickListener . 问题是onItemClick(AdapterView, V
在我的应用程序中,我遇到了与 here 相同的错误或here但是,建议的清理 Eclipse 项目的解决方案对我不起作用。具体来说,我有一个自定义 ListView 适配器,它尝试从其布局文件加载两个
我有一个 ListView 和一个将值附加到 View 的 Baseadapter 类。适配器类看到除一个之外的所有变量..我不知道该变量是否超出了类的范围..我已经检查过,但似乎找不到问题出在哪里
我刚刚构建了一个扩展 BaseAdapter 的新类。我相信我已经正确设置了所有内容,但是,当我尝试在 fragment 内设置适配器时,我想使用它,但出现以下错误: 构造函数 HomeBase(Ho
我是一名优秀的程序员,十分优秀!