简体   繁体   English

使用bindview后如何获取视图对象?

[英]How do I get a view object after using bindview?

I have a custom cursor adapter with a loader. 我有一个带有加载程序的自定义光标适配器。 I want to get the view object in onLoadFinished() after it loads the data and creates the view with bindview so I can change the EditText view's properties (ig visibility). 我想在加载数据并使用bindview创建视图之后在onLoadFinished()中获取视图对象,以便可以更改EditText视图的属性(如ig可见性)。 But bindview does not return a reference to the view object. 但是bindview不会返回对视图对象的引用。

Can I just use.. 我可以用..

TextView myView = (TextView)this.findViewById(R.id.recipe_instructions);

in

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);
}

In this case I'm just creating 1 view for each id but what if I created many? 在这种情况下,我只为每个ID创建1个视图,但是如果创建了多个,该怎么办? How would I get a reference to the view objects so I could change the properties of just 1 in particular? 如何获得对视图对象的引用,以便可以特别更改1的属性?

your cursor adapter must be like : 您的光标适配器必须类似于:

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));
}
}


use this link : Populating-a-ListView-with-a-CursorAdapter 使用此链接:使用CursorAdapter填充一个ListView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM