2012-06-15 2 views
0

런타임에 버튼을 동적으로 생성하여 해당 click 이벤트가 datepickerdialog를 엽니 다. 이 같은 :Android DatePickerDialog 매개 변수

Button btnDate = new Button(this); 
btnDate.setText("Date"); 
btnDate.setTag(new UDAControlItemTag(q.getiQuestionID(),-1,f.getiSectionID(),f.getiGroup(),-1,"Date","")); 
btnDate.setOnClickListener(new View.OnClickListener() {       
    @Override 
    public void onClick(View v) { 
    Button tempBtn = (Button)v; 
    UDAControlItemTag tempQ = (UDAControlItemTag)tempBtn.getTag(); 
    showDialog(DATE_DIALOG_ID_Question);  
    //tempBtn.setText(Integer.toString(mTempMonth)); 
    } 
}); 

그럼 내가 가치와 물건을 얻을 수 있지만 동적으로 컨트롤을 만드는거야 때문에, 내가 다른 특성을 가진 ArrayList에 각 컨트롤의 이러한 값을 유지 청취자가 있습니다. 내가 가진 문제는 올바른 질문을 ArrayList에 넣기 위해 어떤 버튼을 클릭했는지 정확하게 판단해야하는 매개 변수를 얻는 방법입니다.

private DatePickerDialog.OnDateSetListener mDateSetListenerQuestion = 
    new DatePickerDialog.OnDateSetListener() { 
     public void onDateSet(DatePicker view, int year, 
      int monthOfYear, int dayOfMonth) { 
      mTempYear = year; 
      mTempMonth = monthOfYear; 
      mTempDay = dayOfMonth; 
     } 

}; 

그래서 거기에 내가 대화 상자의 값을 가지고,하지만 난 모든 해답의 ArrayList에 대한에 값을 넣어하기 위해, 사용자가 클릭 버튼에 연결된 QuestionID이 필요합니다 활동에 대한 모든 역동적 인 통제. 고마워요. 감사합니다.

답변

1

해당 청취자 대신 DatePickerDialog.OnDateSetListener 인터페이스를 구현하는 자신의 클래스를 사용할 수 있으며 intQuestionID을 생성자로 사용합니다. 이런 식으로 뭔가 : 이제

public class TheSpecialListener implements 
      DatePickerDialog.OnDateSetListener { 

     private int id; 

     public TheSpecialListener(int id) { 
      this.id = id; 
     } 

     public void onDateSet(DatePicker view, int year, int monthOfYear, 
       int dayOfMonth) { 
      mTempYear = year; 
      mTempMonth = monthOfYear; 
      mTempDay = dayOfMonth; 
      // id is the QuestionID so you now can identify the Button that started the dialog 
     } 

    }; 

당신이 Button 당신이 (당신이 구현 방법을 모른다)에 onCreateDialog 방법에 사용됩니다 Button의 QuestionID으로 필드를 업데이트 할 수 있습니다 클릭 :

//... 
Button tempBtn = (Button)v; 
UDAControlItemTag tempQ = (UDAControlItemTag)tempBtn.getTag(); 
questionId = //here pass the Button's id or whatever 
showDialog(DATE_DIALOG_ID_Question);  

onCreateDialog 방법 :

private int questionId; 

와 청취자의 Button 위의 특별한 리스너를 인스턴스화하고 그것에게이 버튼을 클릭 할 때마다 업데이트 될 예정으로, 식별자를 보유 할 questionId 필드 통과 : 톤 도움

DatePickerDialog spd = new DatePickerDialog(this, 
        new TheSpecialListener(questionId), 2012, 6, 16); 
+0

합니다. 정말 고맙습니다 – bflosabre91