gpt4 book ai didi

android - 使用bindview后如何获取 View 对象?

转载 作者:行者123 更新时间:2023-11-29 20:38:15 25 4
gpt4 key购买 nike

我有一个带有加载程序的自定义游标适配器。我想在加载数据并使用 bindview 创建 View 后在 onLoadFinished() 中获取 View 对象,这样我就可以更改 EditText View 的属性(例如可见性)。但是 bindview 不返回对 View 对象的引用。

我可以只使用..

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 个 View ,但如果我创建了很多呢?我将如何获得对 View 对象的引用,以便我可以特别更改 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));
}
}


使用此链接:Populating-a-ListView-with-a-CursorAdapter

关于android - 使用bindview后如何获取 View 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com