-->

How to set the text at particular id in dynamic ge

2019-07-13 04:44发布

问题:

I have the following code. In this i have dynamically generated buttons.when click the button it shows the timepicker. My problem is to set the time for clicked button but it set the text at the last button how to set the value at clicked button...

public void addButton(int value) {
    list.removeAllViews();

    for (i=0 ; i < value; i++) {
        button = new Button(this);
        button.setText("Add Time");
        button.setId(i);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Calendar mCalendar = Calendar.getInstance();
                new TimePickerDialog(MedInfo.this, onTimeListener, mCalendar
                        .get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false)
                        .show();
                btn_cnt = v.getId();
                Toast.makeText(getApplicationContext(), ""+btn_cnt, Toast.LENGTH_SHORT).show();
            }
        });
        list.addView(button);
    }
}


TimePickerDialog.OnTimeSetListener onTimeListener = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String AM_PM;
        Calendar mCalendar = Calendar.getInstance();
        if (hourOfDay < 12) {
            AM_PM = "AM";

        } else {
            AM_PM = "PM";

        }

        long start_time = DateTimeUtils.getTimeInMillis(hourOfDay, minute, 0);
        Calendar calendar = Calendar.getInstance();

        long mseconds = DateTimeUtils.getFirstMillis(calendar.get(Calendar.DATE),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
        time = start_time + mseconds;

        button.settext(time);
        mCalendar.set(Calendar.HOUR, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);

    }
};

any help will be appreciable...

Edit :

error log:

10-29 15:07:32.478: E/AndroidRuntime(6401): FATAL EXCEPTION: main
10-29 15:07:32.478: E/AndroidRuntime(6401): java.lang.NullPointerException
10-29 15:07:32.478: E/AndroidRuntime(6401):     at com.example.medicationreminder.MedInfo$MyOnTimeSetListenter.onTimeSet(MedInfo.java:237)
10-29 15:07:32.478: E/AndroidRuntime(6401):     at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:130)
10-29 15:07:32.478: E/AndroidRuntime(6401):     at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)

回答1:

Try like to update the text of particular Button

Button b=(Button)list.getChildAt(btn_cnt);
b.settext(time);

As you are storing the Button Id in btn_cnt, so can get the view with getChildAt(position)

Where list is your Layout where you adding the Button dynamically.

public void addButton(int value) {
    list.removeAllViews();

    for (i=0 ; i < value; i++) {
        button = new Button(this);
        button.setText("Add Time");
        button.setId(i);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Calendar mCalendar = Calendar.getInstance();
                new TimePickerDialog(MedInfo.this, onTimeListener, mCalendar
                        .get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false)
                        .show();
                btn_cnt = v.getId();
                Toast.makeText(getApplicationContext(), ""+btn_cnt, Toast.LENGTH_SHORT).show();
            }
        });
        list.addView(button);
    }
}


TimePickerDialog.OnTimeSetListener onTimeListener = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String AM_PM;
        Calendar mCalendar = Calendar.getInstance();
        if (hourOfDay < 12) {
            AM_PM = "AM";

        } else {
            AM_PM = "PM";

        }

        long start_time = DateTimeUtils.getTimeInMillis(hourOfDay, minute, 0);
        Calendar calendar = Calendar.getInstance();

        long mseconds = DateTimeUtils.getFirstMillis(calendar.get(Calendar.DATE),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
        time = start_time + mseconds;

        Button b=(Button)list.getChildAt(btn_cnt);
        b.settext(time);

        mCalendar.set(Calendar.HOUR, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);

    }
};

Edit:- You can use List instead of Array for retrieving all the time from Button.

List<String> listOfDate=new ArrayList<String>();
        for(int i=0;i<list.getChildCount();i++)
        {
            Button b=(Button)list.getChildAt(i);
            listOfDate.add(b.getText().toString());

        }

Hope this will help you.



回答2:

One possible solution is to implement a custom OnTimeSetListener which can keep reference to the clicked button. Before you show the time picker, inform the custom listener which button was clicked. See code below:

public void addButton(int value) {
    list.removeAllViews();

    for (i=0 ; i < value; i++) {
        button = new Button(this);
        button.setText("Add Time");
        button.setId(i);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Calendar mCalendar = Calendar.getInstance();
                // set the button which was clicked
                onTimeListener.setClickedButton((Button)v);
                new TimePickerDialog(this, onTimeListener, mCalendar
                        .get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false)
                        .show();
                btn_cnt = v.getId();
                Toast.makeText(getApplicationContext(), ""+btn_cnt, Toast.LENGTH_SHORT).show();
            }
        });
        list.addView(button);
    }
}

TimePickerDialog.OnTimeSetListener onTimeListener = new MyOnTimeSetListenter();

class MyOnTimeSetListenter implements TimePickerDialog.OnTimeSetListener {

    private Button button;

    public void setClickedButton(Button button) {
        this.button = button;
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i2) {
        String AM_PM;
        Calendar mCalendar = Calendar.getInstance();
        if (hourOfDay < 12) {
            AM_PM = "AM";

        } else {
            AM_PM = "PM";

        }

        long start_time = DateTimeUtils.getTimeInMillis(hourOfDay, minute, 0);
        Calendar calendar = Calendar.getInstance();

        long mseconds = DateTimeUtils.getFirstMillis(calendar.get(Calendar.DATE),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
        time = start_time + mseconds;

        this.button.settext(time);
        mCalendar.set(Calendar.HOUR, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);
    }
}


回答3:

Simply create a global variable and set its value onTimeListener and whenever required setText(global_variable)