我有一个名为AuditQuestionEntry的子类,它扩展了LinearLayout。除了其他字段,此布局还包含一个按钮。在我的主要活动(“AuditActivity”)上,我有多个AuditQuestionEntry实例。从AuditActivity,我在每个AuditQuestionEntry布局中为按钮设置了一个OnClickListener。在OnClick方法中,我需要访问与单击的按钮关联的AuditQuestionEntry中的参数。
在OnClickListener中,我调用了getParent(),我可以将它转换为AuditQuestionEntry。但是,当我尝试从那里访问一个参数时,它返回null(或者在我的情况下为0,因为它是一个int)。基本上,似乎getParent()返回的是该类的新实例,而不是包含该按钮的实际实例。
布局 - audit_question_entry.xml:
<TextView
android:id="@+id/auditQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audit Question..."
android:textSize="20dp" />
<Button
android:id="@+id/btnTakeAuditPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Photo" >
</Button>
这是AuditQuestionEntry.java:
public class AuditQuestionEntry extends LinearLayout {
// Define controls
private TextView auditQuestion;
private RatingBar auditRating;
private EditText auditComment;
private ImageView imageView;
private Button photoButton;
private static final int CAMERA_REQUEST = 1888;
private static final int REQUEST_IMAGE_CAPTURE = 1;
public int AuditQuestionNumber; // NEED TO GET THIS!
private Context _ctx;
public ClosetAuditQuestionObj AuditQuestion;
private String PhotoFolder;
private String FullPhotoPathAndName;
public AuditQuestionEntry(Context context) {
super(context);
// InflateView();
throw new RuntimeException("Valid AuditQuestionNumber must be passed to this class via the XML parameters: workbench:AuditQuestionNumber.");
}
public AuditQuestionEntry(Context context, AttributeSet attrs) {
super(context, attrs);
this._ctx = context;
initAttributes(attrs);
}
// Used to grab the AuditQuestionNumber attribute from the XML declaration
// for this layout
private void initAttributes(AttributeSet attrs) {
TypedArray a = _ctx.obtainStyledAttributes(attrs, R.styleable.AuditQuestionEntry);
AuditQuestionNumber = attrs.getAttributeIntValue("http://schemas.android.com/apk/com.bc.workbench", "AuditQuestionNumber", 0);
a.recycle();
}
Inside AuditActivity onCreate()我有以下内容:
OnClickListener btnPhotoListener = new OnClickListener() {
@Override
public void onClick(final View v) {
View viewParent = (View) v.getParent();
AuditQuestionEntry clickedAudit = (AuditQuestionEntry) viewParent;
// On the following line, clickedAudit.AuditQuestionNumber is always 0 even though it was initialized and
// displays properly on screen.
Toast.makeText(CurrentContext, "Audit Question #" + clickedAudit.AuditQuestionNumber, Toast.LENGTH_SHORT).show();
}
};
// Set event listener for photo buttons
// =============================================
((Button) AuditQuestion1.findViewById(R.id.btnTakeAuditPhoto)).setOnClickListener(btnPhotoListener);
((Button) AuditQuestion2.findViewById(R.id.btnTakeAuditPhoto)).setOnClickListener(btnPhotoListener);
答案 0 :(得分:0)
在XML中,您在哪里设置AuditQuestionNumber的属性?如果用于给类膨胀的XML不包含属性,则构造函数将class属性的值默认为0:
AuditQuestionNumber = attrs.getAttributeIntValue("http://schemas.android.com/apk/com.bc.workbench", "AuditQuestionNumber", 0);