使用bindview后如何获取视图对象?
How do I get a view object after using bindview?
我有一个带有加载程序的自定义游标适配器。我想在加载数据并使用 bindview 创建视图后在 onLoadFinished() 中获取视图对象,这样我就可以更改 EditText 视图的属性(i.g. 可见性)。但是 bindview 没有 return 对视图对象的引用。
我可以只使用..
TextView myView = (TextView)this.findViewById(R.id.recipe_instructions);
在
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
dataAdapter.swapCursor(cursor);
dataAdapter.bindView(findViewById(R.id.recipe_name), this, cursor);
dataAdapter.bindView(findViewById(R.id.recipe_instructions), this, cursor);
TextView myView = (TextView)this.findViewById(R.id.recipe_instructions);
}
在这种情况下,我只是为每个 ID 创建 1 个视图,但是如果我创建了很多呢?我将如何获得对视图对象的引用,以便我可以特别更改 1 的属性?
您的光标适配器必须像:
public class TodoCursorAdapter extends CursorAdapter {
public TodoCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_todo, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView tvBody = (TextView) view.findViewById(R.id.tvBody);
TextView tvPriority = (TextView) view.findViewById(R.id.tvPriority);
// Extract properties from cursor
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
int priority = cursor.getInt(cursor.getColumnIndexOrThrow("priority"));
// Populate fields with extracted properties
tvBody.setText(body);
tvPriority.setText(String.valueOf(priority));
}
}
使用这个 link : Populating-a-ListView-with-a-CursorAdapter
我有一个带有加载程序的自定义游标适配器。我想在加载数据并使用 bindview 创建视图后在 onLoadFinished() 中获取视图对象,这样我就可以更改 EditText 视图的属性(i.g. 可见性)。但是 bindview 没有 return 对视图对象的引用。
我可以只使用..
TextView myView = (TextView)this.findViewById(R.id.recipe_instructions);
在
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
dataAdapter.swapCursor(cursor);
dataAdapter.bindView(findViewById(R.id.recipe_name), this, cursor);
dataAdapter.bindView(findViewById(R.id.recipe_instructions), this, cursor);
TextView myView = (TextView)this.findViewById(R.id.recipe_instructions);
}
在这种情况下,我只是为每个 ID 创建 1 个视图,但是如果我创建了很多呢?我将如何获得对视图对象的引用,以便我可以特别更改 1 的属性?
您的光标适配器必须像:
public class TodoCursorAdapter extends CursorAdapter {
public TodoCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_todo, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView tvBody = (TextView) view.findViewById(R.id.tvBody);
TextView tvPriority = (TextView) view.findViewById(R.id.tvPriority);
// Extract properties from cursor
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
int priority = cursor.getInt(cursor.getColumnIndexOrThrow("priority"));
// Populate fields with extracted properties
tvBody.setText(body);
tvPriority.setText(String.valueOf(priority));
}
}
使用这个 link : Populating-a-ListView-with-a-CursorAdapter