[英]Access LinearLayout Subclass Parameter from Button Click
I have a subclass called AuditQuestionEntry that extends LinearLayout. 我有一个名为AuditQuestionEntry的子类,该子类扩展了LinearLayout。 In addition to other fields, this layout contains a button. 除其他字段外,此布局还包含一个按钮。 On my main activity ("AuditActivity") I have multiple instances of AuditQuestionEntry. 在我的主要活动(“ AuditActivity”)上,我有多个AuditQuestionEntry实例。 From AuditActivity, I am setting an OnClickListener for the button in each of the AuditQuestionEntry layouts. 从AuditActivity中,我为每个AuditQuestionEntry布局中的按钮设置一个OnClickListener。 In the OnClick method I need to access a parameter that's in the AuditQuestionEntry associated with the button that was clicked. 在OnClick方法中,我需要访问与单击的按钮关联的AuditQuestionEntry中的参数。
In the OnClickListener I call getParent() and I can cast that to AuditQuestionEntry. 在OnClickListener中,我调用getParent()并将其强制转换为AuditQuestionEntry。 However, when I try to access a parameter from there it returns null (or 0 in my case because it's an int). 但是,当我尝试从那里访问参数时,它返回null(在我的情况下为0,因为它是一个int)。 Basically, it appears that getParent() is returning a new instance of the class and not the actual instance that contains the button. 基本上,似乎getParent()返回该类的新实例,而不是包含按钮的实际实例。
Layout - audit_question_entry.xml: 布局-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>
Here's the AuditQuestionEntry.java: 这是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() I have the following: 在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);
Where in the XML are you setting the attribute for the AuditQuestionNumber? 您在XML中的哪个位置设置AuditQuestionNumber的属性? The constructor is defaulting the value of the class attribute to 0 if the XML being used to inflate the class doesn't include the attribute: 如果用于膨胀类的XML不包含该属性,则构造函数会将class属性的值默认为0:
AuditQuestionNumber = attrs.getAttributeIntValue("http://schemas.android.com/apk/com.bc.workbench", "AuditQuestionNumber", 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.