- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
这是我的自定义 CursorAdapter:
public class TasksAdapter extends CursorAdapter implements Filterable {
private final Context context;
public TasksAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
/**
* @see android.widget.CursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(android.R.layout.simple_list_item_checked, parent, false);
ViewHolder holder = new ViewHolder();
holder.textview = (CheckedTextView)v.findViewById(android.R.id.text1);
v.setTag(holder);
return v;
}
/**
* @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder)view.getTag();
int titleCol = cursor.getColumnIndexOrThrow(Tasks.TITLE);
int completedCol = cursor.getColumnIndexOrThrow(Tasks.COMPLETED);
String title = cursor.getString(titleCol);
boolean completed = Util.intToBool(cursor.getInt(completedCol));
holder.textview.setText(title);
holder.textview.setChecked(completed);
}
/**
* @see android.widget.CursorAdapter#runQueryOnBackgroundThread(java.lang.CharSequence)
*/
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
StringBuffer buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuffer();
buffer.append("UPPER (");
buffer.append(Tasks.TITLE);
buffer.append(") GLOB ?");
args = new String[] { "*" + constraint.toString().toUpperCase() + "*" };
}
Cursor c = context.getContentResolver().query(Tasks.CONTENT_URI,
null, (buffer == null ? null : buffer.toString()), args,
Tasks.DEFAULT_SORT_ORDER);
c.moveToFirst();
return c;
}
/**
* @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
*/
@Override
public CharSequence convertToString(Cursor cursor) {
final int titleCol = cursor.getColumnIndexOrThrow(Tasks.TITLE);
String title = cursor.getString(titleCol);
return title;
}
static class ViewHolder {
CheckedTextView textview;
}
}
这是否属于 ViewHolder 模式的约束?我不确定,因为这是一个没有 getView
的 CursorAdapter。如果有任何问题或建议,请指出来。
最佳答案
CursorAdapter
不会在每次需要新行时调用 newView
;如果它已经有一个View
,它会调用bindView
,所以创建的view实际上是复用的。
也就是说,正如 Joseph 在评论中指出的那样,您仍然可以使用 ViewHolder 以避免重复调用 findViewById
。
如果您仍然关心效率,请查看 SimpleCursorAdapter
实现,它使用 WeakHashMap
(WeakReferences
的映射):
WeakHashMap<View, View[]> mHolders = new WeakHashMap<View, View[]>();
关于java - ViewHolder 模式在自定义 CursorAdapter 中正确实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4567969/
在我的应用程序中,我显示一些信息,并根据值更改 textView 的颜色。 public class DealsAdapter extends CursorAdapter { private
我正在尝试获取使用自定义 CursorAdapter 存储的数据,但到目前为止它默默地失败了。它只是加载一个空白 View 并且不打印任何内容。 这是主 fragment 的 onCreateView
好吧,因为我在搜索时没有找到执行此操作的一致方法,所以我决定将我的代码放在其他人的眼睛上可能会有所帮助。 我正在制作一款使用纸牌的游戏。根据不同的因素,这些卡可以被锁定或解锁。重要的是,我想检查 sq
我有一个 ChatActivity,它通过 CursorLoader 加载它的数据。 CursorLoader 返回一个带有两个寄存器的游标,但永远不会调用适配器中的 newView 和 bindVi
我不确定问题标题是否合适,但是在搜索了很多之后我问了这个。 在我的 SQLite 表中,我有列 1: _id 2: position 3: path position: the positio
SearchView 的方法 setSuggestionsAdapter 给我一个奇怪的错误: 我有一个扩展 CursorAdapter 的类 InformationAdapter,但它告诉我我不能将
我最近写了一个 StackOverflow 文档示例,说明了如何使用 SimpleCursorAdapter 从数据库中填充 ListView。 它被拒绝了,动机如下: Nobody should b
我正在制作一个 StudentManager 应用程序。 在MainActivity.java中,当我触摸 float 按钮时,它将切换到EditActivity.java。然后,在 EditActi
我读了这个post 但我无法解决我的问题。我在 ListView 中使用 CursorAdapter。 我在列表的每个项目中都有一个复选框。如果您选中该复选框并上下滚动。该复选框将被禁用。我无法修复它
我目前使用 CursorAdapter 和 Loader。 Loader 加载的数据包含一个位置。我想从最近到最远的顺序订购我的商品。我已经计算出显示它的距离,现在我需要找到一种方法对数据进行动态排序
CursorAdapter 有 3 个构造函数。让我们看看指南和引用。 1) CursorAdapter(Context context, Cursor c) This constructor is
我有一个 ListFragment,它显示使用 CursorLoader 和 CursorAdapter 获得的项目列表,按照处理列出。这本身工作正常,但我不确定如何使用相同的模式来显示所选项目的详细
我有一个 CursorAdapter,它是从数据库 Cursor 构建的,如果第一个项目中的 View 没有填写,它使用设置了 View 的第一个后续列表项中的 View 中出现的任何数据填充第一个项
我有一个数据库、一个 ListView 和一个扩展 CursorAdapter 的 CustomCursorAdapter。一个菜单按钮将一个项目添加到数据库中。我希望 ListView 更新并显示此
我有一个可以帮助用户组织处方的应用程序。我有一个显示药物的 ListView ,我使用 EditText 来允许用户过滤列表。我遇到的问题是每次方向改变时 CursorLoader 都会被替换。 据我
在 Ice Cream Sandwich 上,当我想恢复具有带 CursorAdapter 的 gridview 且已将 managedQuery 传递给 CursorAdapter 的应用程序时,我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
您好,我正在开发一个带有 CursorAdapter 的 Android 应用程序来加载 ListView 。 onItemClick ListView 项目,我正在尝试开始对话 Activity 。
我正在使用 SimpleCursorAdapter 来管理我对数据库的查询,然后在 ListView 中显示结果。 这是我的代码: database.open(); ListView listCont
我正在尝试用数据库查询填充 ListView 。我正在使用扩展游标适配器的自定义适配器。我不知道要使用什么构造函数。当我使用默认的时: public AdapterListview(Context c
我是一名优秀的程序员,十分优秀!